Repository navigation
Expand file tree
/
Copy pathBSTSuccessor.java
More file actions
148 lines (122 loc) · 3.52 KB
/
Copy pathBSTSuccessor.java
File metadata and controls
148 lines (122 loc) · 3.52 KB
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
package Tree;
public class BSTSuccessor {
static class Node {
int key;
Node left, right, parent;
Node(int key) {
this.key = key;
}
}
private Node root;
private Node minimum(Node x) {
while (x.left != null) {
x = x.left;
}
return x;
}
private Node maximum(Node x) {
while (x.right != null) {
x = x.right;
}
return x;
}
// 노드 x의 successor : key[x]보다 크면서 가장 작은 키를 가진 노드
private Node successor(Node x) {
// 오른쪽 부트리가 있으면, 그 부트리의 최솟값
if (x.right != null) {
return minimum(x.right);
}
// 오른쪽 부트리가 없으면, 부모를 따라 올라가면서 처음으로 왼쪽 자식이 되는 노드의 부모를 찾음
Node y = x.parent;
while (y != null && x == y.right) {
x = y;
y = y.parent;
}
// Case 3: y가 null이면 x가 최대값이므로 successor 없음
return y;
}
// 노드 x의 predecessor: key[x]보다 작으면서 가장 큰 키를 가진 노드
private Node predecessor(Node x) {
// Case 1: 왼쪽 부트리가 있으면, 그 부트리의 최댓값
if (x.left != null) {
return maximum(x.left);
}
// Case 2: 왼쪽 부트리가 없으면,
// 부모를 따라 올라가면서 "처음으로 오른쪽 자식이 되는 노드"의 부모를 찾음
Node y = x.parent;
while (y != null && x == y.left) {
x = y;
y = y.parent;
}
// Case 3: y가 null이면 x가 최솟값이므로 predecessor 없음
return y;
}
private Node search(Node x, int key) {
while (x != null && x.key != key) {
if (x.key > key) {
x = x.left;
} else {
x = x.right;
}
}
return x;
}
public Integer successor(int key) {
Node x = search(root, key);
if (x == null) {
return null;
}
Node s = successor(x);
if (s == null) {
return null;
} else {
return s.key;
}
}
public Integer predecessor(int key) {
Node x = search(root, key);
if (x == null) {
return null;
}
Node p = predecessor(x);
if (p == null) {
return null;
} else {
return p.key;
}
}
public void insert(int key) {
Node z = new Node(key);
Node y = null;
Node x = root;
while (x != null) {
y = x;
if (key < x.key) {
x = x.left;
} else {
x = x.right;
}
}
z.parent = y;
if (y == null) {
root = z;
} else if (z.key < y.key) {
y.left = z;
} else {
y.right = z;
}
}
public static void main(String[] args) {
BSTSuccessor tree = new BSTSuccessor();
int[] keys = { 15, 6, 18, 3, 7, 17, 20, 13, 9 };
for (int key : keys) {
tree.insert(key);
}
System.out.println(tree.successor(6));
System.out.println(tree.successor(13));
System.out.println(tree.successor(20));
System.out.println(tree.predecessor(6));
System.out.println(tree.predecessor(15));
System.out.println(tree.predecessor(3));
}
}