-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy paththreaded-binarytree.cpp
More file actions
237 lines (227 loc) · 4.72 KB
/
threaded-binarytree.cpp
File metadata and controls
237 lines (227 loc) · 4.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
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
#include<iostream>
using namespace std;
struct node{
int info;
node *left;
node *right;
int left_thread;
int right_thread;
};
class thread{
private:
node *temp , *ptr ,*temp1, *dummy , *current , *temp2;
public:
node *root;
int key , number , swap;
thread()
{
temp1 = temp = ptr = root = current = temp2= NULL;
key = 0;
number = 0;
swap = 0;
dummy = new node;
dummy->right = dummy;
dummy->left = dummy;
temp = root;
}
~thread(){
delete root;
delete temp;
}
void insert(node *temp){
temp = root;
while(temp != NULL){
if(temp->info == number)
{
cout << "Number is there in tree plz enter a unique number"<<endl;
break;
}
if(temp->info < number)
{
if(temp->right_thread == 0) // thread = 1 means temprary value is there
temp = temp->right;
else
break;
}
else
{
if(temp->left_thread == 0)
temp = temp->left;
else
break;
}
}
ptr = new node;
ptr -> info = number;
ptr->left_thread = 1;
ptr->right_thread = 1;
if(temp == NULL){
ptr->right = ptr->left = dummy;
root = ptr;
dummy->left = root;
if(ptr->left == dummy){
cout <<"current set"<<endl;
current = ptr;
}
return;
}
if (temp->info < number){
ptr->right = temp->right;
temp->right = ptr;
temp->right_thread = 0;
ptr->left = temp;
return;
}
if (temp->info > number){
ptr->left = temp->left;
temp->left = ptr;
temp->left_thread = 0;
ptr->right = temp;
if(ptr->left == dummy){
cout <<"current set"<<endl;
current = ptr;
}
return;
}
}
node* nextInorder(node *temp)
{
if(temp->right_thread == 1)
return temp -> right;
else {
temp = temp->right;
while (temp->left_thread == 0)
temp = temp->left;
return temp;
}
}
void fastInorder()
{
if (root == NULL)
{
cout <<"Tree is empty right now"<<endl;
return;
}
temp = current;
cout <<temp->info <<" ";
while( (temp = nextInorder(temp) ) != dummy )
cout << temp->info <<" ";
}
void delet(node *temp){
temp = root;
while(temp != dummy){
cout << "temp: " << temp->info << endl;
if (temp->info == key){
// Case #1
if(temp->left_thread == 1 && temp->right_thread == 1) // when temp have no child
{
if (temp == root)
{
delete root;
root = NULL;
dummy->left = dummy;
return;
}
ptr = temp->right;
if(temp->left == dummy || ptr->info > temp->info) // if value is in left
{
ptr->left = temp->left;
if(temp == current)
current = ptr;
// ptr ->right_thread = 1;
delete temp;
temp = NULL;
ptr ->left_thread = 1;
return;
}
ptr = temp->left;
if (temp->right == dummy || ptr->info < temp->info) //if value is in right
{
ptr->right = temp->right;
// ptr ->left_thread = 1;
delete temp;
temp = NULL;
ptr ->right_thread = 1;
temp = ptr;
return;
}
}
//case#2
if(temp->left_thread == 1 && temp->right_thread == 0)
{
cout << "ok right"<<endl;
ptr = (nextInorder(temp));
ptr->left = temp->left;
if(temp = current)
current = ptr;
if (temp == root){
root = temp->right;
current = root;
ptr->left->left = ptr->right;
}
// ptr = temp->right->left;
ptr->left->right = temp->right;
delete temp;
temp = NULL;
return;
}
if( temp->left_thread == 0 && temp->right_thread == 1)
{
if(temp == root)
root = temp->left;
ptr->right = temp->right;
ptr->right->left = temp->left;
delete temp;
temp = NULL;
return;
}
// // Case#3
if(temp->left_thread == 0 && temp->right_thread == 0){
ptr = temp->left;
while(ptr->right_thread != 1){
ptr = ptr->right;
}
swap = temp->info;
temp->info = ptr->info;
ptr->info = swap;
if(ptr->left_thread == 0){
temp1 = ptr;
while(temp1->left_thread != 1){
temp1 = temp1->left;
}
ptr->left->right = ptr->right;
temp1->left->right = ptr->left;
delete ptr;
ptr = NULL;
return;
}
if(ptr == current){
current = ptr->right;
}
ptr->left->right_thread = 1;
ptr->left->right = ptr->right;
delete ptr;
ptr = NULL;
return;
}
}
temp = nextInorder(temp);
}
cout << "Key is not in tree" <<endl;
return;
}
};
int main(){
thread t;
for(int i = 0 ; i < 8 ; i++){
cout <<"Enter number to insert in new node" <<endl;
cin >> t.number;
t.insert(t.root);
}
t.fastInorder();
cout << endl;
cout << "Enter key to delete from thread binery tree" <<endl;
cin >> t.key;
t.delet(t.root);
t.fastInorder();
}