-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathInorderPredecessor.java
84 lines (76 loc) · 2.51 KB
/
InorderPredecessor.java
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
package com.anudev.ds.trees;
public class InorderPredecessor {
public static void printInorderPredecessor(Node rootNode, int key) {
Node node = findNode(rootNode, key);
if (node == null) {
System.out.println("Invalid key");
return;
}
// if the left node is not null then the node which is having
// largest value in the left subtree is the inorder predecessor
// of the node value
Node predecessorFound;
if (node.getLeftNode() != null) {
predecessorFound = findLargestValueOfTree(node.getLeftNode());
}
// if the left node is not present then the node where we
// have taken last right while reaching the key node is the
// inorder predecessor
else {
predecessorFound = findLastRightNode(rootNode, key);
}
if (predecessorFound != null) {
System.out.print("Predecessor of node " + key + " is " + predecessorFound.getValue());
} else {
System.out.print("No Predecessor of node ");
}
}
private static Node findNode(Node rootNode, int key) {
Node node = rootNode;
Node keyNode = null;
while (true) {
if (node == null) {
break;
} else if (node.getValue() == key) {
keyNode = node;
break;
} else if (node.getValue() > key) {
node = node.getLeftNode();
} else {
node = node.getRightNode();
}
}
return keyNode;
}
// find the largest value in subtree
private static Node findLargestValueOfTree(Node node) {
while (node.getRightNode() != null) {
node = node.getRightNode();
}
return node;
}
// find last right node
private static Node findLastRightNode(Node rootNode, int key) {
Node node = rootNode;
boolean found = false;
Node lastRightNode = null;
while (true) {
if (node == null) {
break;
} else if (node.getValue() == key) {
found = true;
break;
} else if (node.getValue() > key) {
node = node.getLeftNode();
} else {
lastRightNode = node;
node = node.getRightNode();
}
}
// if the element is found then
if (found) {
return lastRightNode;
}
return null;
}
}