forked from sanketpatil02/Code-Overflow
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Binary Search Tree
128 lines (102 loc) · 1.68 KB
/
Binary Search Tree
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
#include<iostream>
using namespace std;
class treenode{
public:
int value;
treenode* left;
treenode* right;
treenode()
{
value=0;
left=NULL;
right=NULL;
}
treenode(int v)
{
value =v;
left = NULL;
right =NULL;
}
};
class bst{
public:
treenode* root;
bst()
{
root =NULL;
}
bool isEmpty(){
if(root == NULL)
return true;
else
return false;
}
void insert(treenode* new_node)
{
if(root == NULL)
root = new_node;
else{
treenode* temp =root;
while(temp!=NULL)
{
if(new_node->value == temp->value)
return;
else if((new_node->value <temp->value)&& (temp->left==NULL))
{
temp->left = new_node;
break;
}
else if((new_node->value > temp->value) && (temp->right ==NULL))
{
temp->right =new_node;
break;
}
else{
temp =temp->right;
}
}
}
}
void disp(treenode* root){
if(root ==NULL)
return;
cout<<root->value<<endl;
disp(root->left);
disp(root->right);
}
};
int main(){
bst obj;
int option,val;
do{
cout<<"Selet operation: (press 0 t exit)"<<endl;
cout<<"1. insert"<<endl;
cout<<"2. print BST values"<<endl;
cout<<"3. clearscreen"<<endl;
cout<<"0. exit"<<endl;
cin>>option;
treenode *new_node = new treenode();
switch(option){
case 0:
break;
case 1:
cout<<"insert"<<endl;
cout<<"enter the value:";
cin>>val;
new_node->value=val;
obj.insert(new_node);
cout<<endl;
break;
case 2:
cout<<"print"<<endl;
obj.disp(obj.root);
break;
case 3:
cout<<"clearscreen"<<endl;
break;
default:
cout<<"wrong chice";
}
}while(option!=0);
return 0;
}