Skip to content

Postes Praktikum Sdata X

January 16, 2011

#include <iostream.h>

#ifndef Xcept_
#define Xcept_

#include <except.h>
#include <new.h>
class BadInitializers {
public:
BadInitializers() {}
};
class NoMem {
public:
NoMem() {}
};


void my_new_handler()
{
throw NoMem();
};

new_handler Old_Handler_ = set_new_handler(my_new_handler);
class OutOfBounds {
public:
OutOfBounds() {}
};
class SizeMismatch {
public:
SizeMismatch() {}
};
class MustBeZero {
public:
MustBeZero() {}
};
class BadInput {
public:
BadInput() {}
};

#endif

#ifndef Node_
#define Node_

template <class T> class LinkedStack;
template <class T> class LinkedQueue;

template <class T>
class Node {
friend LinkedStack<T>;
friend LinkedQueue<T>;
private:
T data;
Node<T> *link;
};

#endif

#ifndef LinkedQueue_
#define LinkedQueue_

template<class T>
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<T>& Add(const T& x);
LinkedQueue<T>& Delete(T& x);
private:
Node<T> *front;
Node<T> *rear;
};

template<class T>
LinkedQueue<T>::~LinkedQueue()
{// Queue destructor.  Delete all nodes.
Node<T> *next;
while (front) {
next = front->link;
delete front;
front = next;
}
}

template<class T>
int LinkedQueue<T>::IsFull() const
{
Node<T> *p;
try {p = new Node<T>;
delete p;
return 0;} //false;}
catch (NoMem) {return 1;}
}

template<class T>
T LinkedQueue<T>::First() const
{

if (IsEmpty()) throw OutOfBounds();
return front->data;
}

template<class T>
T LinkedQueue<T>::Last() const
{
if (IsEmpty()) throw OutOfBounds();
return rear->data;
}

template<class T>
LinkedQueue<T>& LinkedQueue<T>::Add(const T& x)
{
Node<T> *p = new Node<T>;
p->data = x;
p->link = 0;
if (front) rear->link = p;
else front = p;
rear = p;

return *this;
}

template<class T>
LinkedQueue<T>& LinkedQueue<T>::Delete(T& x)
{

if (IsEmpty()) throw OutOfBounds();
x = front->data;
Node<T> *p = front;
front = front->link;
delete p;

return *this;
}

template<class T> class BinaryTree;
template <class T>
class BinaryTreeNode{
friend void Visit(BinaryTreeNode<T>*);
friend void InOrder(BinaryTreeNode<T>*);
friend void PreOrder(BinaryTreeNode<T>*);
friend void PostOrder(BinaryTreeNode<T>*);
friend void LevelOrder(BinaryTreeNode<T>*);
friend void main(void);
public :
BinaryTreeNode(){LeftChild=RightChild=0;}
BinaryTreeNode(const T& e){
data=e;
LeftChild=RightChild=0;
}
BinaryTreeNode(const T& e, BinaryTreeNode *l, BinaryTreeNode *r){
data=e;
LeftChild=l;
RightChild=r;
}
private :
T data;
BinaryTreeNode<T> *LeftChild, *RightChild;
};

template<class T>
void Visit(BinaryTreeNode<T> *x){
cout<<x->data<<” “;
}

template<class T>
void PreOrder(BinaryTreeNode<T> *t){
if(t){
Visit(t);
PreOrder(t->LeftChild);
PreOrder(t->RightChild);
}
}

template<class T>
void InOrder(BinaryTreeNode<T> *t){
if(t){
InOrder(t->LeftChild);
Visit(t);
InOrder(t->RightChild);
}
}

template<class T>
void PostOrder(BinaryTreeNode<T> *t){
if(t){
PostOrder(t->LeftChild);
PostOrder(t->RightChild);
Visit(t);
}
}

template<class T>
void LevelOrder(BinaryTreeNode<T> *t){
LinkedQueue<BinaryTreeNode<T>*> 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;}
}
}

void main(void){
BinaryTreeNode<int> x,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 In order : “;
InOrder(&x);
cout<<endl;
cout<<”Kunjungan Pre order : “;
PreOrder(&x);
cout<<endl;
cout<<”Kunjungan Post order : “;
PostOrder(&x);
cout<<endl;
cout<<”Kunjungan Level order : “;
LevelOrder(&x);
cout<<endl;

}

Leave a Comment

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: