-
Notifications
You must be signed in to change notification settings - Fork 0
/
7_Threaded_binaryTree.cpp
243 lines (219 loc) · 5.25 KB
/
7_Threaded_binaryTree.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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
/*AARTI RATHI
Topic: Threaded Binary Tree
My website - https://shinchancode.github.io/3d-react-portfolio/
*/
/*Problem Statement :
Implement In-order Threaded Binary Tree and traverse it in In-order and Pre-order*/
#include<bits/stdc++.h>
using namespace std;
//Structure of the Threaded Binary Tree
struct TBT_nd
{
int data;
TBT_nd *left;
TBT_nd *right;
int th_left;
int th_right;
} *head;
TBT_nd *insert(TBT_nd *root, TBT_nd *temp) //Inserting node in Threaded Binary Tree
{
if(head->left==head && head->right==head)
{
temp->left=head;
temp->right=head;
head->left=temp;
head->th_left=1;
root=temp;
return root;
}
TBT_nd *curr=root;
if(temp->data < curr->data)
{
if(curr->th_left==0)
{
temp->left=curr->left;
curr->left=temp;
temp->right=curr;
curr->th_left=1;
}
else
{
insert(curr->left,temp);
}
}
else if(temp->data > curr->data)
{
if(curr->th_right==0)
{
temp->right=curr->right;
curr->right=temp;
temp->left=curr;
curr->th_right=1;
}
else
{
insert(curr->right,temp);
}
}
return root;
}
TBT_nd *new_nd()
{
TBT_nd *temp=new TBT_nd;
cout<<"ENTER DATA : ";
cin>>temp->data;
temp->left=NULL;
temp->right=NULL;
temp->th_left=0;
temp->th_right=0;
return temp;
}
TBT_nd *create(TBT_nd *root) //Create the node in Threaded Binary Tree
{
int ch;
TBT_nd *temp;
if(root==NULL)
{
head=new TBT_nd;
head->data=9999;
head->left=head;
head->right=head;
head->th_right=1;
head->th_left=0;
root=head;
}
do
{
temp=new_nd();
if(root!=NULL)
{
root=insert(root,temp);
}
cout<<"do you want to insert more nodes?? (enter 1 to continue)\n";
cin>>ch;
}
while(ch==1);
return root;
}
TBT_nd *pre_suc(TBT_nd *t) //Predecessor of the node
{
if(t->th_left==1)
return(t->left);
while(t->th_right==0)
t=t->right;
return(t->right);
}
void TBTpreoder(TBT_nd *root) //Preoder Traversal in Threaded BT
{
TBT_nd *p=root;
while(p!=head)
{
cout<<p->data<<"\t";
p=pre_suc(p);
}
}
TBT_nd *in_suc(TBT_nd *t) //Successor of the node
{
if(t->th_right==0)
return (t->right);
t=t->right;
while(t->th_left==1)
t=t->left;
return(t);
}
void TBTinoder(TBT_nd *root) //Inoder Traversal in Threaded BT
{
TBT_nd *p=root;
while(p->th_left==1)
{
p=p->left;
}
while(p!=head)
{
cout<<p->data<<"\t";
p=in_suc(p);
}
}
int main() //Main function
{
TBT_nd *root=NULL;
int n;
cout<<"INORDER THREADED BINARY TREE !";
do
{
cout<<"\n[1] CREATE (Or insert node in existing tree)?\n";
cout<<"[2] INORDER TRAVERSE?\n";
cout<<"[3] PREORDER TRAVERSE?\n";
cout<<"\nEnter your choice: (enter 0 to exit)";
cin>>n;
if(n==1)
{
root=create(root);
}
else if(n==2)
{
cout<<"INORDER TRAVERSAL: \n";
TBTinoder(root);
}
else if(n==3)
{
cout<<"PREORDER TRAVERSAL: \n";
TBTpreoder(root);
}
}
while(n!=0);
cout<<"\nOPERATION COMPLETED!! THANK YOU";
return 0;
}
/*
-----------------------------------------------------------------------------------------------------------------------------
OUTPUT :
INORDER THREADED BINARY TREE !
[1] CREATE (Or insert node in existing tree)?
[2] INORDER TRAVERSE?
[3] PREORDER TRAVERSE?
Enter your choice: (enter 0 to exit)1
ENTER DATA : 10
do you want to insert more nodes?? (enter 1 to continue)
1
ENTER DATA : 6
do you want to insert more nodes?? (enter 1 to continue)
1
ENTER DATA : 13
do you want to insert more nodes?? (enter 1 to continue)
1
ENTER DATA : 15
do you want to insert more nodes?? (enter 1 to continue)
1
ENTER DATA : 7
do you want to insert more nodes?? (enter 1 to continue)
1
ENTER DATA : 20
do you want to insert more nodes?? (enter 1 to continue)
1
ENTER DATA : 2
do you want to insert more nodes?? (enter 1 to continue)
1
ENTER DATA : 3
do you want to insert more nodes?? (enter 1 to continue)
0
[1] CREATE (Or insert node in existing tree)?
[2] INORDER TRAVERSE?
[3] PREORDER TRAVERSE?
Enter your choice: (enter 0 to exit)2
INORDER TRAVERSAL:
2 3 6 7 10 13 15 20
[1] CREATE (Or insert node in existing tree)?
[2] INORDER TRAVERSE?
[3] PREORDER TRAVERSE?
Enter your choice: (enter 0 to exit)3
PREORDER TRAVERSAL:
10 6 2 3 7 13 15 20
[1] CREATE (Or insert node in existing tree)?
[2] INORDER TRAVERSE?
[3] PREORDER TRAVERSE?
Enter your choice: (enter 0 to exit)0
OPERATION COMPLETED!! THANK YOU
...Program finished with exit code 0
Press ENTER to exit console.
*/