Praktikum S.Data ke-10


POHON

#include<cstdlib>
#include<iostream>

using namespace std;

templateclass BinaryTree;

template

class BinaryTreeNode{

friend void Visit(BinaryTreeNode *);

friend void InOrder(BinaryTreeNode *);

friend void PreOrder(BinaryTreeNode *);

friend void PostOrder(BinaryTreeNode *);

friend void LevelOrder(BinaryTreeNode *);

friend void main(void);

public:

BinaryTreeNode(){LeftChild=RightChild=0;}

BinaryTreeNode(const T& e)

{data = e;

LeftChild=RightChild=0;}

BinaryTreeNode(const T& e, BinaryTreeNode *|,

BinaryTreeNode *r)

{data = e;

LeftChild=|;

RightChild=r;}

private:

T data;

binaryTreeNode *LeftChild,

*RightChild;

};

//implementasi penelusuran node

//#include

//#include

//#include “lqueue.h”

//#include “btnode.h”

//#include “xcept.h”

//using namespace std;

template

void Visit(BinaryTreeNode *x){

cout<data<<' ';

}

template

void PreOrder(BinaryTreeNode *t){

if(t){

Visit(t);

PreOrder(t->LeftChild);

PreOrder(t->RightChild);

}

}

template

void InOrder(BinaryTreeNode *t){

if(t){

InOrder(t->LeftChild);

Visit(t);

InOrder(t->RightChild);

}

}

template

void PostOrder(BinaryTreeNode *t){

if(t){

PostOrder(t->LeftChild);

PostOrder(t->RightChild);

Visit(t);

}

}

template

void LevelOrder(BinaryTreeNode *t){

LinkedQueue Q;

while(t);{

Visit(t);

if(t->LeftChild) Q.Add(t->LeftChild);

if(t->RightChild) Q.Add(t->RightChild);

try{Q.Delete(t;}

catch(OutOfBounds)

{

return;}

}

}

int main(int argc, char *argv[])

{

BinaryTreeNodex,y,z;

x.data=1;

y.data=2;

z.data=3;

x.LeftChild=&y;

x.RightChild=&z;

y.LeftChild=y.RightChild=z.LeftChild=z.RightChild=0;

cout<<"Kunjungan Inorder : ";

InOrder(&x);

cout<<endl;

cout<<"Kunjungan Preorder : ";

PreOrder(&x);

cout<<endl;

cout<<"Kunjungan Postorder : ";

PostOrder(&x);

cout<<endl;

cout<<"Kunjungan Level Order : ";

LevelOrder(&x);

cout<<endl;

system("PAUSE");

return EXIT_SUCCESS;

}

//kelas bantu linked queue

//#include

//#include

//#include “node.h”

//#include “xcept.h”

//using namespace std;

template

class LinkedQueue{

public:

LinkedQueue(){front = rear = 0;}

~LinkedQueue();

int IsEmpty()const

{return((front)?0:1);}

int IsFull()const;

T First()const;

T Last()const;

LinkedQueue& Add(const T& x);

LinkedQueue& Delete(T& x);

private:

Node *front;

Node *rear;

};

template

LinkedQueue::!LinkedQueue(){

Node *next;

while(front){

next=front->link;

delete front;

front=next;

}

}

template

int LinkedQueue::IsFull() const{

Node *p;

try(p=new Node;

delete p;

return 0;}

catch(NoMen){return 1;}

}

template

T LinkedQueue::First() const{

if(IsEmpty())throw OutOfBounds();

return front->data;

}

template

T LinkedQueue::Last() const{

if(IsEmpty())throw OutOfBounds();

return rear->data;

}

template

LinkedQueue& LinkedQueue::Add(const T& x){

Node *p=new Node;

p->data=x;

p->link=0;

if(front)rear->link=p;

else front=p;

rear=p;

return *this;

}

template

LinkedQueue& Linkedqueue::Delete(T& x){

if(IsEmpty())throw OutOfBounds();

x=front->data;

Node *p=front;

front=front->link;

delete p;

return *this;

}

int main(int argc, char *argv[])

{

system(“PAUSE”);

return EXIT_SUCCESS;

}

About Sintata
Seek and Share Knowledge

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: