-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy paththreadedBST.cpp
More file actions
188 lines (159 loc) · 4.17 KB
/
threadedBST.cpp
File metadata and controls
188 lines (159 loc) · 4.17 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
#include <iostream>
#include <limits>
using namespace std;
class Node {
public:
int data;
Node* left;
Node* right;
bool isThreaded;
Node(int val) {
data = val;
left = nullptr;
right = nullptr;
isThreaded = false;
}
};
Node* searchNode(Node* root, int value) {
if(root==nullptr) {
return nullptr;
}
Node* current = root;
while(current->left!=nullptr) {
current = current->left;
}
while(current!=nullptr) {
if(current->data = value) {
return current;
}
else if(current->isThreaded) {
current = current->right;
}
else {
current = current->right;
while(current!=nullptr && current->left!=nullptr) {
current = current->left;
}
}
}
return nullptr;
}
void searchThis(Node* root, int value) {
if(searchNode(root, value)) {
cout<<"Node is present."<<endl;
return;
}
else cout<<"Node is not present."<<endl;
}
void addNode(Node* root, int value) {
if(searchNode(root, value)) {
cout<<"Node is already present! "<<endl;
return;
}
Node* newNode = new Node(value);
if(root==nullptr) {
root = newNode;
return;
}
Node* current = root;
Node* parent = nullptr;
while(current!=nullptr) {
parent = current;
if(current==nullptr || current->isThreaded) break;
if(current->data == value) return;
else if(current->data < value) {
current = current->right;
}
else current = current->left;
} //parent found here
//want to add newnode at left of parent
if(value < parent->data) {
parent->left = newNode;
newNode->right = parent;
newNode->isThreaded = true;
} //added to left
//want to add newnode at right of parent
else {
if(value > parent->data || parent->isThreaded) {
newNode->right = parent->right;
newNode->isThreaded = true;
parent->isThreaded = false;
}
parent->right = newNode;
} //added to right
}
void inOrder(Node* root) {
if(root==nullptr) {
cout<<"Tree is empty, cannot print! ";
return;
}
Node* current = root;
while(current->left!=nullptr) {
current = current->left;
}
while(current!=nullptr) {
cout<<current->data<<" ";
if(current->isThreaded) {
current = current->right;
}
else {
current = current->right;
while(current!=nullptr && current->left!=nullptr) {
current = current->left;
}
}
}
}
int getOnlyInteger() {
int x;
while(1) {
cin>>x;
if(cin.fail() || x<0) {
cin.clear();
cin.ignore(numeric_limits<streamsize>::max(), '\n');
cout<<"Please give positive integer. ";
}
else return x;
}
}
int main() {
int x;
cout<<"Enter data of root: ";
x = getOnlyInteger();
Node *root = new Node(x);
while (true) {
cout <<endl<<"Enter the operation: 1- Search, 2- Add, 3- inOrder, 0- Exit." <<endl;
int operation;
operation = getOnlyInteger();
if (operation == 0) {
break;
}
switch (operation) {
case 1: {
int value;
cout<<"Enter data to search: ";
value = getOnlyInteger();
searchThis(root, value);
break;
}
case 2: {
int noOfValues1, value1;
cout<<"How many nodes to be added ? ";
noOfValues1 = getOnlyInteger();
for (int i = 0; i < noOfValues1; i++) {
cout <<endl<<"Enter value to be added: ";
value1 = getOnlyInteger();
addNode(root, value1);
}
break;
}
case 3: {
inOrder(root);
break;
}
default:
cout<<"Invalid operation, Try again."<<endl;
}
}
return 0;
}