#include <iostream>
using namespace std;
template <class T>
class BinaryTree
{
BinaryTree<T> *left;
T data;
BinaryTree<T> *right;
public :
BinaryTree()
{
left=right=NULL;
}
void create();
void preorder();
void inorder();
void postorder();
};
template <class T>
void BinaryTree<T> :: create()
{
char opt;
cout << "\n Enter the element : ";
cin >> data;
cout << "\n Is there left child for " << data << " [y/n] ? : ";
cin >> opt;
if (opt=='y')
{
left = new BinaryTree<T>;
left->create();
}
cout << "\n Is there right child for " << data << " [y/n] ? : ";
cin >> opt;
if (opt=='y')
{
right = new BinaryTree<T>;
right->create();
}
}
template <class T>
void BinaryTree<T> :: preorder()
{
if (this!=NULL)
{
cout << data << " ";
left->preorder();
right->preorder();
}
}
template <class T>
void BinaryTree<T> :: inorder()
{
if (this!=NULL)
{
left->inorder();
cout << data << " ";
right->inorder();
}
}
template <class T>
void BinaryTree<T> :: postorder()
{
if (this!=NULL)
{
left->postorder();
right->postorder();
cout << data << " ";
}
}
int main()
{
BinaryTree<char> *Root;
Root=NULL;
int ch;
do
{
cout << "Binary Tree Operation Menu\n";
cout << "1. Creation\n";
cout << "2. Preorder Traversal\n";
cout << "3. InOrder Traversal\n";
cout << "4. PostOrder Traversal\n";
cout << "5. Exit\n\n";
cout << "Enter Your Choice [1..5] ? : ";
cin >> ch;
switch(ch)
{
case 1 :
Root = new BinaryTree <char>;
Root->create();
break;
case 2 :
Root->preorder();
break;
case 3 :
Root->inorder();
break;
case 4 :
Root->postorder();
break;
case 5 :
break;
default :
cout << "Invalid Choice ....";
}
}while (ch!=5);
return 0;
}
using namespace std;
template <class T>
class BinaryTree
{
BinaryTree<T> *left;
T data;
BinaryTree<T> *right;
public :
BinaryTree()
{
left=right=NULL;
}
void create();
void preorder();
void inorder();
void postorder();
};
template <class T>
void BinaryTree<T> :: create()
{
char opt;
cout << "\n Enter the element : ";
cin >> data;
cout << "\n Is there left child for " << data << " [y/n] ? : ";
cin >> opt;
if (opt=='y')
{
left = new BinaryTree<T>;
left->create();
}
cout << "\n Is there right child for " << data << " [y/n] ? : ";
cin >> opt;
if (opt=='y')
{
right = new BinaryTree<T>;
right->create();
}
}
template <class T>
void BinaryTree<T> :: preorder()
{
if (this!=NULL)
{
cout << data << " ";
left->preorder();
right->preorder();
}
}
template <class T>
void BinaryTree<T> :: inorder()
{
if (this!=NULL)
{
left->inorder();
cout << data << " ";
right->inorder();
}
}
template <class T>
void BinaryTree<T> :: postorder()
{
if (this!=NULL)
{
left->postorder();
right->postorder();
cout << data << " ";
}
}
int main()
{
BinaryTree<char> *Root;
Root=NULL;
int ch;
do
{
cout << "Binary Tree Operation Menu\n";
cout << "1. Creation\n";
cout << "2. Preorder Traversal\n";
cout << "3. InOrder Traversal\n";
cout << "4. PostOrder Traversal\n";
cout << "5. Exit\n\n";
cout << "Enter Your Choice [1..5] ? : ";
cin >> ch;
switch(ch)
{
case 1 :
Root = new BinaryTree <char>;
Root->create();
break;
case 2 :
Root->preorder();
break;
case 3 :
Root->inorder();
break;
case 4 :
Root->postorder();
break;
case 5 :
break;
default :
cout << "Invalid Choice ....";
}
}while (ch!=5);
return 0;
}
No comments:
Post a Comment