-
Notifications
You must be signed in to change notification settings - Fork 2
/
BINARY_SEARCH_TREE.cpp
185 lines (185 loc) · 2.97 KB
/
BINARY_SEARCH_TREE.cpp
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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
//Binary Search Tree Traversal
#include<iostream.h>
#include<conio.h>
#include<stdlib.h>
typedef struct BSTnode
{
int data;
BSTnode *left,*right;
};
class BinarySearchTree
{
BSTnode *Root;
public:
BinarySearchTree()
{
Root=NULL;
}
~BinarySearchTree()
{
delete Root;
}
BSTnode *GetRoot(void)
{
return Root;
}
void SetRoot(BSTnode *root)
{
Root=root;
}
BSTnode *CreateNode(void);
void Insert(BSTnode *root, BSTnode *New);
void Preorder(BSTnode *temp);
void Inorder(BSTnode *temp);
void Postorder(BSTnode *temp);
void Display(BSTnode *temp, int row, int low, int high);
};
/*Create node*/
BSTnode *BinarySearchTree::CreateNode()
{
BSTnode *temp;
temp=new BSTnode;
temp->left=NULL;
temp->right=NULL;
return temp;
}
/*Insert Function*/
void BinarySearchTree::Insert(BSTnode *root, BSTnode *New)
{
if(New->data < root->data)
{
if(root->left==NULL)
root->left=New;
else
Insert(root->left, New);
}
if(New->data >= root->data)
{
if(root->right==NULL)
root->right=New;
else
Insert(root->right, New);
}
}
/*Preorder Traversals*/
void BinarySearchTree::Preorder(BSTnode *temp)
{
if(temp!=NULL)
{
cout<<"-> "<<temp->data<<" ";
Preorder(temp->left);
Preorder(temp->right);
}
}
/*Inorder Traversals*/
void BinarySearchTree::Inorder(BSTnode *temp)
{
if(temp!=NULL)
{
Inorder(temp->left);
cout<<"-> "<<temp->data<<" ";
Inorder(temp->right);
}
}
/*Postorder Traversals*/
void BinarySearchTree::Postorder(BSTnode *temp)
{
if(temp!=NULL)
{
Postorder(temp->left);
Postorder(temp->right);
cout<<"-> "<<temp->data<<" ";
}
}
/*display of binary search tree in tree format*/
void BinarySearchTree::Display(BSTnode *temp, int row, int low, int high)
{
if(temp!=NULL)
{
Display(temp->left,row+2,low,(low+high)/2);
gotoxy((low+high)/2,row);
cout<<temp->data;
Display(temp->right,row+2,(low+high)/2,high);
}
}
int main()
{
BinarySearchTree B;
int ch;
BSTnode *New=NULL,*root=NULL;
while(1)
{
clrscr();
cout<<"\n\t\t\t Binary Search Tree";
cout<<"\n\t\t\t *****************";
cout<<"\n 1.Create";
cout<<"\n 2.Preorder";
cout<<"\n 3.Inorder";
cout<<"\n 4.Postorder";
cout<<"\n 5.Display";
cout<<"\n 6.Exit\n";
cout<<"\n Enter your choice : ";
cin>>ch;
root=B.GetRoot();
switch(ch)
{
case 1:
cout<<"\nEnter elements 1 by 1: (0 to stop entering)\n";
do
{
New=B.CreateNode();
cin>>New->data;
if(New->data==0)
break;
if(root==NULL)
root=New;
else
B.Insert(root,New);
}while(1);
B.SetRoot(root);
break;
case 2:
if(root==NULL)
cout<<"\n No element in Tree\n";
else
{
cout<<"\n***Preorder Traversal***\n\nThe Tree is:\n";
B.Preorder(root);
}
getch();
break;
case 3:
if(root==NULL)
cout<<"\n No element in Tree\n";
else
{
cout<<"\n***Inorder Traversal***\n\nThe Tree is:\n";
B.Inorder(root);
}
getch();
break;
case 4:
if(root==NULL)
cout<<"\n No element in Tree\n";
else
{
cout<<"\n***Postorder Traversal***\n\nThe Tree is:\n";
B.Postorder(root);
}
getch();
break;
case 5:
clrscr();
B.Display(root,1,1,80);
getch();
break;
case 6:
cout<<"\n***Exit***\n";
getch();
exit(0);
default:
cout<<"\n Enter between 1 to 5\n\n";
break;
}
}
}