-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathThreaded_Binary_Tree.cpp
More file actions
230 lines (222 loc) · 4.32 KB
/
Threaded_Binary_Tree.cpp
File metadata and controls
230 lines (222 loc) · 4.32 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
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
/*
Name: Chirantan Joshi.
Div: G
Batch: G1
Roll No.: PG13
THREADED BINARY TREE OPERATIONS
*/
#include <iostream>
#include<stdlib.h>
using namespace std;
class treenode
{
int data;
bool rbit,lbit;
treenode *right,*left;
friend class tree;
public:
treenode();
};
treenode::treenode() //make lbit and rbit of a treenode as 1 initially
{
lbit=1;
rbit=1;
}
class tree
{
treenode *root;
treenode *head; //make dummy node and attach root to left of head
public:
tree()
{
head=new treenode;
head->rbit=0;
head->left=head;
head->right=head;
}
void create()
{
char choice,choice1;
root=new treenode;
cout<<"Enter the data at root: ";
cin>>root->data;
head->lbit=0;
root->left=head;
root->right=head;
head->left=root;
do
{
int flag=0;
treenode *curr,*temp=root;
curr=new treenode;
cout<<"Enter the data: ";
cin>>curr->data;
while(flag==0)
{
cout<<"At which side of "<<temp->data<<" you want to add node(l/r): ";
cin>>choice1;
if(choice1=='l')
{
if(temp->lbit==1) //attach new node to right or left of existing node
{
curr->right=temp;
curr->left=temp->left;
temp->left=curr;//make thread from new node's left to existing node's left
temp->lbit=0; //if thread is made put that side bit as 1 else put 0
flag=1;
}
else
temp=temp->left;
}
else
{
if(temp->rbit==1)
{
curr->left=temp;
curr->right=temp->right;
temp->right=curr;//make thread from new node's right to existing node's right
temp->rbit=0;
flag=1;
}
else
temp=temp->right;
}
}
cout<<"Do you want to continue(y/n): ";
cin>>choice;
}while(choice=='y');
}
void inorder() //inorder traversal
{
cout<<endl<<"Inorder traversal of tree is:"<<endl;
treenode *temp=head;
while(1)
{
temp=inordersucc(temp);
if(temp==head)
break;
cout<<temp->data<<endl;
}
}
treenode *inordersucc(treenode *temp) //traverse tree in direction of links not threads
{
treenode *x=temp->right;
if(temp->rbit==0)
{
while(x->lbit==0)
{
x=x->left;
}
}
return x;
}
void preorder() //preorder traversal
{
cout<<endl<<"Preorder traversal of tree is:"<<endl;
treenode *temp=head->left;
while(temp!=head)
{
cout<<temp->data<<endl; //print node
while(temp->lbit!=1) //traverse left links
{
temp=temp->left;
cout<<temp->data<<endl;
}
while(temp->rbit==1) //traverse right threads
{
temp=temp->right;
}
temp=temp->right;
}
}
};
int main()
{
tree tbt;
int choice;
while(1)
{
cout<<"\n***********MENU***********\n1.Create\n2.Inorder.\n3.Preorder.\n4.Exit.\nEnter the choice: ";
cin>>choice;
switch(choice)
{
case 1: tbt.create();
break;
case 2: tbt.inorder();
break;
case 3: tbt.preorder();
break;
case 4: exit(1);
break;
default:cout<<"Enter the correct choice!!";
}
}
return 0;
}
/*
OUTPUT:
***********MENU***********
1.Create
2.Inorder.
3.Preorder.
4.Exit.
Enter the choice: 1
Enter the data at root: 12
Enter the data: 6
At which side of 12 you want to add node(l/r): l
Do you want to continue(y/n): y
Enter the data: 8
At which side of 12 you want to add node(l/r): r
Do you want to continue(y/n): y
Enter the data: 2
At which side of 12 you want to add node(l/r): l
At which side of 6 you want to add node(l/r): l
Do you want to continue(y/n): y
Enter the data: 4
At which side of 12 you want to add node(l/r): l
At which side of 6 you want to add node(l/r): r
Do you want to continue(y/n): y
Enter the data: 1
At which side of 12 you want to add node(l/r): l
At which side of 6 you want to add node(l/r): r
At which side of 4 you want to add node(l/r): l
Do you want to continue(y/n): y
Enter the data: 18
At which side of 12 you want to add node(l/r): r
At which side of 8 you want to add node(l/r): r
Do you want to continue(y/n): n
***********MENU***********
1.Create
2.Inorder.
3.Preorder.
4.Exit.
Enter the choice: 2
Inorder traversal of tree is:
2
6
1
4
12
8
18
***********MENU***********
1.Create
2.Inorder.
3.Preorder.
4.Exit.
Enter the choice: 3
Preorder traversal of tree is:
12
6
2
4
1
8
18
***********MENU***********
1.Create
2.Inorder.
3.Preorder.
4.Exit.
Enter the choice: 4
*/