forked from sachuverma/DataStructures-Algorithms
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Predecessor and Successor.cpp
80 lines (68 loc) · 1.63 KB
/
Predecessor and Successor.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
/*
Predecessor and Successor
=========================
There is BST given with root node with key part as integer only. You need to find the inorder successor and predecessor of a given key. In case, if the either of predecessor or successor is not found print -1.
Input:
The first line of input contains an integer T denoting the number of test cases. Then T test cases follow. Each test case contains n denoting the number of edges of the BST. The next line contains the edges of the BST. The last line contains the key.
Output:
Print the predecessor followed by successor for the given key. If the predecessor or successor is not found print -1.
Constraints:
1<=T<=100
1<=n<=100
1<=data of node<=100
1<=key<=100
Example:
Input:
2
6
50 30 L 30 20 L 30 40 R 50 70 R 70 60 L 70 80 R
65
6
50 30 L 30 20 L 30 40 R 50 70 R 70 60 L 70 80 R
100
Output:
60 70
80 -1
*/
/* BST Node
struct Node
{
int key;
struct Node *left, *right;
};
*/
// This function finds predecessor and successor of key in BST.
// It sets pre and suc as predecessor and successor respectively
void findPreSuc(Node *root, Node *&pre, Node *&suc, int key)
{
if (!root)
return;
if (root->key == key)
{
if (root->left)
{
auto rt_max = root->left;
while (rt_max->right)
rt_max = rt_max->right;
pre = rt_max;
}
if (root->right)
{
auto rt_max = root->right;
while (rt_max->left)
rt_max = rt_max->left;
suc = rt_max;
}
return;
}
else if (root->key > key)
{
suc = root;
findPreSuc(root->left, pre, suc, key);
}
else
{
pre = root;
findPreSuc(root->right, pre, suc, key);
}
}