-
Notifications
You must be signed in to change notification settings - Fork 0
/
InorderSuccessorInBinarySearchTree.cpp
199 lines (185 loc) · 4.36 KB
/
InorderSuccessorInBinarySearchTree.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
// Implemented by Kritagya Kumra
#include <iostream>
using namespace std;
/* A binary tree node has data,
the pointer to left child
and a pointer to right child */
class Node
{
public:
int data;
Node *left = NULL;
Node *right = NULL;
Node *parent = NULL;
};
Node *minValue(Node *node);
Node *returnMaxValue(Node *root)
{
Node *temp = root;
while (temp->left != NULL)
{
temp = temp->left;
}
return temp;
}
Node *returnMinValue(Node *root)
{
Node *temp = root;
while (temp->right != NULL)
{
temp = temp->right;
}
return temp;
}
Node *inOrderSuccessor(Node *root, Node *element)
{
// Step 1 of the above algorithm
if (element->right != NULL)
{
return returnMinValue(element->right);
}
Node *successor = NULL;
// Start from root and search for
// successor down the tree
while (root->data != element->data)
{
if (root->data > element->data)
{
successor = root;
root = root->left;
}
else
{
root = root->right;
}
}
return successor;
}
Node *inOrderPredecessor(Node *root, Node *element)
{
// Step 1 of the above algorithm
if (element->left != NULL)
{
return (element->left);
}
Node *predecessor = NULL;
// Start from root and search for
// successor down the tree
while (root->data != element->data)
{
if (root->data > element->data)
{
root = root->left;
}
else
{
predecessor = root;
root = root->right;
}
}
return predecessor;
}
Node *inOrderSuccessor2(Node *root, Node *n)
{
// step 1 of the above algorithm
if (n->right != NULL)
return minValue(n->right);
// step 2 of the above algorithm
Node *p = n->parent;
while (p != NULL && n == p->right)
{
n = p;
p = p->parent;
}
return p;
}
/* Given a non-empty binary search tree,
return the minimum data
value found in that tree. Note that
the entire tree does not need
to be searched. */
Node *minValue(Node *node)
{
Node *current = node;
/* loop down to find the leftmost leaf */
while (current->left != NULL)
{
current = current->left;
}
return current;
}
/* Helper function that allocates a new
node with the given data and
NULL left and right pointers. */
Node *newNode(int data)
{
Node *node = (Node *)
malloc(sizeof(
Node));
node->data = data;
node->left = NULL;
node->right = NULL;
node->parent = NULL;
return (node);
}
/* Give a binary search tree and
a number, inserts a new node with
the given number in the correct
place in the tree. Returns the new
root pointer which the caller should
then use (the standard trick to
avoid using reference parameters). */
Node *insert(Node *node, int data)
{
/* 1. If the tree is empty, return a new,
single node */
if (node == NULL)
return (newNode(data));
else
{
Node *temp;
/* 2. Otherwise, recur down the tree */
if (data <= node->data)
{
temp = insert(node->left, data);
node->left = temp;
temp->parent = node;
}
else
{
temp = insert(node->right, data);
node->right = temp;
temp->parent = node;
}
/* return the (unchanged) node pointer */
return node;
}
}
/* Driver program to test above functions*/
int main()
{
Node *root = NULL, *temp, *succ, *min, *pre;
// creating the tree given in the above diagram
root = insert(root, 20);
root = insert(root, 8);
root = insert(root, 22);
root = insert(root, 4);
root = insert(root, 12);
root = insert(root, 10);
root = insert(root, 14);
temp = root->left->right->right;
succ = inOrderSuccessor(root, temp);
pre = inOrderPredecessor(root, temp);
if (succ != NULL)
cout << "\n Inorder Successor of " << temp->data << " is " << succ->data;
else
cout << "\n Inorder Successor doesn't exit";
cout << endl;
if (pre != NULL)
cout << "\n Inorder Predecessor of " << temp->data << " is " << pre->data;
else
cout << "\n Inorder Predecessor doesn't exit";
getchar();
return 0;
}
// Implemented by Kritagya Kumra