-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBinaryTree.cpp
More file actions
131 lines (118 loc) · 3.42 KB
/
BinaryTree.cpp
File metadata and controls
131 lines (118 loc) · 3.42 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
#include<iostream>
#include<stdlib.h>
using namespace std;
template <class temp>
class Node
{
public :
temp data;
Node *left, *right;
Node(temp d)
{
data = d;
left = NULL;
right = NULL;
}
};
template <class temp>
class BinaryTree
{
public :
Node<temp> *root, *ptr;
BinaryTree()
{
root = NULL;
}
void formTree()
{
root = new Node<temp>('-');
root->left = new Node<temp>('-');
root->left->left = new Node<temp>('+');
root->left->left->left = new Node<temp>('x');
root->left->left->right = new Node<temp>('y');
root->left->right = new Node<temp>('*');
root->left->right->left = new Node<temp>('2');
root->left->right->right = new Node<temp>('x');
root->right = new Node<temp>('+');
root->right->left = new Node<temp>('*');
root->right->right = new Node<temp>('/');
root->right->left->left = new Node<temp>('x');
root->right->left->right = new Node<temp>('z');
root->right->right->left = new Node<temp>('y');
root->right->right->right = new Node<temp>('z');
}
void preorder(Node<temp> *ptr)
{
if(ptr!=NULL)
{
cout<<ptr->data<<'\t';
preorder(ptr->left);
preorder(ptr->right);
}
return;
}
void inorder(Node<temp> *ptr)
{
if(ptr!=NULL)
{
inorder(ptr->left);
cout<<ptr->data<<'\t';
inorder(ptr->right);
}
return;
}
void postorder(Node<temp> *ptr)
{
if(ptr!=NULL)
{
postorder(ptr->left);
postorder(ptr->right);
cout<<ptr->data<<'\t';
}
return;
}
void printOptions()
{
cout<<"The avalible options are : "<<endl;
cout<<"1) In-Order traversal of the tree."<<endl;
cout<<"2) Pre-Order traversal of the tree."<<endl;
cout<<"3) Post-Order traversal of the tree."<<endl;
cout<<"4) Exit"<<endl;
}
};
int main()
{
cout<<"-------------------------------------------------------------------------------------------------"<<endl;
cout<<"\t\t\t\t\tWelcome\n";
cout<<"-------------------------------------------------------------------------------------------------"<<endl;
BinaryTree<char> tree;
int cho, num;
tree.formTree();
do
{
tree.printOptions();
cout<<"Enter your option : ";
cin>>cho;
switch(cho)
{
case 1 :
tree.inorder(tree.root);
cout<<endl;
break;
case 2 :
tree.preorder(tree.root);
cout<<endl;
break;
case 3 :
tree.postorder(tree.root);
cout<<endl;
break;
case 4 :
cout<<"End of program."<<endl;
break;
default :
cout<<"Invalid Option"<<endl;
}
cout<<"-------------------------------------------------------------------------------------------------"<<endl;
} while (cho!=4);
}