-
Notifications
You must be signed in to change notification settings - Fork 36
/
c++ prog bst
145 lines (135 loc) · 2.21 KB
/
c++ prog bst
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
#include<iostream>
#define SPACE 10
using namespace std;
class treenode
{
public:
int value;//data
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 insertnode(treenode *newnode)
{
if(root==NULL)
{
root=newnode;
cout<<"value inserted as root node"<<endl;
}
else
{
treenode *temp=root;
while(temp!=NULL)
{
if(newnode->value==temp->value)
{
cout<<"value alreasy exists , insert another"<<endl;
return;
}
else if((newnode->value<temp->value)&&(temp->left==NULL))
{
temp->left=newnode;
cout<<"value inserted in left";
break;
}
else if(newnode->value<temp->value)
{
temp=temp->left;
}
else if((newnode->value>temp->value)&&(temp->right==NULL))
{
temp->right=newnode;
cout<<"value inserted in right";
break;
}
else if(newnode->value>temp->value)
{
temp=temp->right;
}
}
}
}
void searchnode()
{
}
void print2d(treenode *r,int space)
{
if(r==NULL)
return;
space +=SPACE;
print2d(r->right,space);
cout<<endl;
for(int i=SPACE;i<space;i++)
cout<<" ";
cout<<r->value<<"\n";
print2d(r->left,space);
}
};
int main()
{
int option,val;
bst obj;
do
{ cout<<"\n";
cout<<"SELECT THE OPTION NUMBER"<<endl;
cout<<"1.INSERT NODE"<<endl;
cout<<"2.SEARCH NODE"<<endl;
cout<<"3.DELETE NODE"<<endl;
cout<<"4.PRINT BST VALUES"<<endl;
cout<<"0.EXIT"<<endl;
cin>>option;
treenode *newnode= new treenode();//we used heap memory
switch(option)
{
case 0:
break;
case 1: cout<<"insert"<<endl;
cout<<"enter the value to insert in bst";
cin>>val;
newnode->value=val;
obj.insertnode(newnode);
cout<<endl;
break;
case 2: cout<<"search"<<endl;
break;
case 3: cout<<"delete"<<endl;
break;
case 4: cout<<"print value"<<endl;
obj.print2d(obj.root,5);
break;
default:cout<<"enter correct option"<<endl;
}
}
while(option!=0);
return 0;
}