-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLabEval2.cpp
More file actions
202 lines (188 loc) · 2.72 KB
/
LabEval2.cpp
File metadata and controls
202 lines (188 loc) · 2.72 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
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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
#include<iostream>
using namespace std;
struct node
{
int data;
node *left,*right;
}*root;
void ins(node *root,int val);
node *newNode(int v)
{
node *temp = new node;
temp->data=v;
temp->left=NULL;
temp->right=NULL;
return temp;
}
void create(int val)
{
if(!root)
{
root=newNode(val);
}
else
{
ins(root,val);
}
}
void ins(node *t,int val)
{
if(val>(t->data))
{
if(!(t->right))
{
t->right=newNode(val);
}
else
{
ins(t->right,val);
}
}
else if(val<t->data)
{
if(!(t->left))
{
t->left=newNode(val);
}
else
{
ins(t->left,val);
}
}
else if(val=t->data)
{
cout<<"Node Exist "<<t->data<<" "<<t<<endl;
}
}
void search(int val)
{
node *ptr=root;
node *par=NULL;
{
while(ptr!=NULL)
{
if(ptr->data==val)
{
cout<<"Found "<<val<<endl;
cout<<"Parent "<<par->data;
return;
}
else if(ptr->data>val)
{
par=ptr;
ptr=ptr->left;
}
else if(ptr->data<val)
{
par=ptr;
ptr=ptr->right;
}
}
}
if(ptr== NULL)
{
cout<<"NOT FOUND";
}
}
node *ser(int val)
{
int ab=val;
cout<<" \nab "<<ab;
node *ptr=root;
while(ptr!=NULL)
{
cout<<"\n p "<<ptr->data<<"\t ab "<<ab;
if((ptr->data)==ab)
{
cout<<"Found";
return ptr;
}
else if((ptr->data)>ab)
{
cout<<"\nHere l"<<ptr->data<<"\t"<<ab;
ptr=ptr->left;
}
else if((ptr->data)<ab)
{
cout<<"\nHere r"<<ptr->data<<"\t"<<ab;
ptr=ptr->right;
}
}
return 0;
}
void preOrder(node *t)
{
if(t)
{
cout<<t->data<<" ";
preOrder(t->left);
preOrder(t->right);
}
else
return;
}
node *inSuccessor(node *t)
{
node *par=t;
while(t->left!=NULL)
{
par=t;
t=t->left;
}
return par;
}
int main()
{
while(1)
{
cout<<"\n\tEnter Choice"<<endl;
cout<<"1.Ins 1 Node\n2.Ins Multiple nodes\n3.Search for a node\n4.Pre-Order Transversal \n5.In-Order Sucessor\n9.EXIT"<<endl;
int x;
cin>>x;
switch(x)
{
case 1: cout<<"Enter the Number ";
int n;
cin>>n;
create(n);
break;
case 2: cout<<"Enter 20 to end Inserstion";
while(1)
{
int n;
cin>>n;
if(n==20)
break;
create(n);
}
break;
case 3: cout<<"Enter the value to search :";
int v;
cin>>v;
search(v);
break;
case 4: cout<<"Pre-Order Transversal =";
preOrder(root);
break;
case 9: return 0;
break;
case 5: cout<<"Enter the number to find Successor of : "; //Works some TImes...
int z;
cin>>z;
cout<<"Finding "<<z<<endl;
node *temp =ser(z);
if(temp)
{
node *p= inSuccessor(temp->right);
p=p->left;
cout<<"Successor is "<<p->data<<endl;
}
else if(!temp)
{
cout<<"Node Does't Exist";
}
break;
}
}
return 0;
}