-
Notifications
You must be signed in to change notification settings - Fork 0
/
inorder_predSuc_iterative.cpp.cpp
165 lines (150 loc) · 2.76 KB
/
inorder_predSuc_iterative.cpp.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
//inorder predecessor and successor of a given key in binary search tree
//perform search and insertion operation in a binary search tree
#include <bits/stdc++.h>
using namespace std;
class node{
public:
int data;
node *left;
node *right;
node(int x){
data = x;
left = NULL;
right = NULL;
}
};
node* insert(node *root, int x){
if(root == NULL){
node *temp = new node(x);
return temp;
}
if(x < root->data){
root->left = insert(root->left, x);
}
else if(x > root->data){
root->right = insert(root->right, x);
}
return root;
}
node *search(node *root, int key){
if(root == NULL) return NULL;
if(root->data == key) return root;
if(key < root->data){
return search(root->left, key);
}
return search(root->right, key);
}
void inorder(node *root){
if(root == NULL) return;
inorder(root->left);
cout << root->data << " ";
inorder(root->right);
}
node *findMaximum(node *root){
if(root == NULL) return NULL;
while(root->right != NULL){
root=root->right;
}
return root;
}
node *findMin(node *root){
if(root == NULL) return NULL;
while(root->left != NULL){
root = root->left;
}
return root;
}
node* findPredecessor(node *root, int key){
if(root == NULL) return NULL;
node *pred = NULL;
while(1){
if(key < root->data){
root = root->left;
}
else if(key > root->data){
pred = root;
root = root->right;
}
else{
if(root->left != NULL){
pred = findMaximum(root->left);
}
break;
}
if(root == NULL) return NULL;
}
return pred;
}
node* findSuccessor(node *root, int key){
if(root == NULL) return NULL;
node *suc = NULL;
while(1){
if(key < root->data){
suc = root;
root = root->left;
}
else if(key > root->data){
root = root->right;
}
else{
if(root->right != NULL){
suc = findMin(root->right);
}
break;
}
if(root == NULL) return NULL;
}
return suc;
}
int main(){
int n,x;
node *root = NULL;
do{
cout << "1. Insert 2. Search 3. Show 4. Predecessor 5. Successor 6. Exit\n";
cin >> n;
switch(n){
case 1:{
cout << "Enter node: ";
cin >> x;
root = insert(root, x);
inorder(root);
cout << endl;
break;
}
case 2:{
cout << "Enter key to search: ";
cin >> x;
node *key = search(root, x);
if(key == NULL){
cout << "Not FOUND\n";
}
else cout << "FOUND\n";
break;
}
case 3:{
inorder(root);
cout << endl;
break;
}
case 4:{
cout << "Enter key: ";
cin >> x;
node *pre = findPredecessor(root, x);
if(pre)
cout << pre->data << endl;
else cout << -1 << endl;
break;
}
case 5:{
cout << "Enter key: ";
cin >> x;
node *suc = findSuccessor(root, x);
if(suc)
cout << suc->data << endl;
else cout << -1 << endl;
break;
}
}
}while(n != 6);
return 0;
}