#include "stdafx.h" #pragma once //----------------------------------------------------------------- // class Node // a single Node from a binary tree //----------------------------------------------------------------- template struct Node { public: Node * left; Node * right; T value; Node (T value); Node (T value, Node * left, Node * right); void Print(); }; // class Node implementation template Node::Node(T val) : value(val), left(NULL), right(NULL) { // no further initialization } template Node::Node(T val, Node * l, Node * r) : value(val), left(l), right(r) { } template void Node::Print() { cout << this->value; } //----------------------------------------------------------------- // class Tree Binary Trees // can process all nodes in Pre/In/Post order //----------------------------------------------------------------- template class Tree { protected: Node * root; public: Tree(); ~Tree(); int IsEmpty() const; void Clear() { Clear(root); root=NULL;} void PreOrder() { PreOrder(root); } void InOrder() { InOrder(root); } void PostOrder() { PostOrder(root); } virtual void Process(T val) {cout< * current); void PreOrder(Node * current); void InOrder(Node * current); void PostOrder(Node * current); void PrintLNR(Node *header,int count); void Print(Node * current); }; // class Tree implementation template Tree::Tree() // initialize tree { root = NULL; } template Tree::~Tree() // deallocate tree { if (root != NULL) Clear(root); } template int Tree::IsEmpty() const { return root==NULL; } template void Tree::Clear(Node * current) { if(current) { // Release memory associated with children if (current->left) Clear(current->left); if (current->right) Clear(current->right); delete current; } } // PreOrder Processing of tree rooted at current template void Tree::PreOrder(Node * current) { // visit Node, left child, right child if (current) { // Process current Node Process(current->value); // then visit children PreOrder(current->left); PreOrder(current->right); } } // InOrder Processing of tree rooted at current template void Tree::InOrder(Node * current) { // visit left child, Node, right child if (current) { InOrder(current->left); Process(current->value); InOrder(current->right); } } // PostOrder Processing of tree rooted at current template void Tree::PostOrder(Node * current) { // visit left child, right child, node if (current) { PostOrder(current->left); PostOrder(current->right); Process(current->value); } } //To FIX by YOU guys !!! template void Tree::PrintLNR(Node *header,int count) { if(header==NULL) return; PrintLNR(header->right,count+1); for (int i=1 ; i<3*count ; cout<<' ',i++); Process(*header); PrintLNR(header->left,count+1); } template void Tree::Print(Node * current) { if(current) { Print(current->left); current->Print(); cout << endl; Print(current->right); } }