You are on page 1of 7

template<class T>

class BinaryTree
{
public:
BinaryTree () {root = 0;};

~BinaryTree() {};

bool IsEmpty() const


{return ((if (root!=NULL)) ? false: true);}

bool Root(T& x) const;

void MakeTree
(const T& element,
BinaryTree<T>& left, BinaryTree<T>& right);

void BreakTree(T& element,


BinaryTree<T>& left, BinaryTree<T>& right);

void PreOrder()
{ PreOrder( root); }
void InOrder()
{ InOrder( root); }
void PostOrder();
{ PostOrder(root); }
Void LevelOrder();

private:
BinaryTreeNode<T> *root;

void PreOrder(BinaryTreeNode<T> *t);

void InOrder( BinaryTreeNode<T> *t);

void PostOrder(,BinaryTreeNode<T> *t);


};

template <class T>


void visit(binaryTreeNode<T> *x)
{// visit node *x, just output element field.
cout << x->element << ' ';
}

template <class T>


void preOrder(binaryTreeNode<T> *t)
{// Preorder traversal of *t.
if (t != NULL)
{
visit(t); // visit tree root
preOrder(t->leftChild); // do left subtree
preOrder(t->rightChild); // do right
subtree
}
}

template <class T>


void inOrder(binaryTreeNode<T> *t)
{// Inorder traversal of *t.
if (t != NULL)
{
inOrder(t->leftChild); // do left subtree
visit(t); // visit tree root
inOrder(t->rightChild); // do right
subtree
}
}

template <class T>


void postOrder(binaryTreeNode<T> *t)
{// Postorder traversal of *t.
if (t != NULL)
{
postOrder(t->leftChild); // do left
subtree
postOrder(t->rightChild); // do right
subtree
visit(t); // visit tree root
}
}
template <class T>
void levelOrder(binaryTreeNode<T> *t)
{// Level-order traversal of *t.
arrayQueue<binaryTreeNode<T>*> q;
while (t != NULL)
{
visit(t); // visit t

// put t's children on queue


if (t->leftChild != NULL)
q.push(t->leftChild);
if (t->rightChild != NULL)
q.push(t->rightChild);

// get next node to visit


try {t = q.front();}
catch (queueEmpty) {return;}
q.pop();
}
}

int main(void)
{
// create a binary tree with root x
binaryTreeNode<int> *x, *y, *z;
y = new binaryTreeNode<int> (2);
z = new binaryTreeNode<int> (3);
x = new binaryTreeNode<int> (1, y, z);

// traverse x in all ways


cout << "Inorder sequence is ";
inOrder(x);
cout << endl;
cout << "Preorder sequence is ";
preOrder(x);
cout << endl;
cout << "Postorder sequence is ";
postOrder(x);
cout << endl;
cout << "Level order sequence is ";
levelOrder(x);
cout << endl;

return 0;
}

You might also like