-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathInorderSuccwParent.cpp
162 lines (137 loc) · 3.69 KB
/
InorderSuccwParent.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
#include <stdio.h>
#include <stdlib.h>
using namespace std;
/* A binary tree node has data, pointer to left child
and a pointer to right child */
struct node
{
int data;
struct node* left;
struct node* right;
struct node* parent;
};
struct node * minValue(struct node* node);
struct node * inOrderSuccessor(struct node *root, struct node *n)
{
if(n->right!=NULL)
return minValue(n->right);
else
{
//then we have to look at the first ancestor travelling up from which we entered into the left subtree.
//in other words we traverse up and return the parent of a node for which the following condition holds true.
//i.e n == p->left
struct node* p = n->parent;
while(p!=NULL)
{
if(n == p->left)
break;
n = p;
p = p->parent;
}
return p;
}
}
struct node* inOrderSuccessorwoparent(struct node*root,struct node *n)
{
if(n->right != NULL)
return minValue(n->right);
else
{
//search from the root. Keep track of the last time you entered a left subtree.
struct node* succ = NULL;
while(root)
{
if(root->data > n->data)
{
succ = root; //update last succ seen
root = root->left;
}
else if(root->data < n->data)
{
root = root->right;
}
else
{
break;
}
}
return succ;
}
}
/* 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. */
struct node * minValue(struct node* root)
{
struct node* temp = root;
if(temp == NULL)
return NULL;
while(temp->left != NULL)
{
temp = temp->left;
}
return temp;
}
/* Helper function that allocates a new node with the given data and
NULL left and right pointers. */
struct node* newNode(int data)
{
struct node* node = (struct node*)malloc(sizeof(struct 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). */
struct node* insert(struct node* node, int data)
{
/* 1. If the tree is empty, return a new,
single node */
if (node == NULL)
return(newNode(data));
else
{
struct 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()
{
struct node* root = NULL, *temp, *succ;
//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);
succ = inOrderSuccessorwoparent(root,temp);
if(succ != NULL)
printf("\n Inorder Successor of %d is %d ", temp->data, succ->data);
else
printf("\n Inorder Successor doesn't exit");
getchar();
return 0;
}