-
Notifications
You must be signed in to change notification settings - Fork 1
/
BST.java
175 lines (155 loc) · 4 KB
/
BST.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
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
public class BST <Key extends Comparable<Key> , Value> {
private class Node {
public Key key;
public Value value;
public Node left;
public Node right;
public int count;
public Node(Key key, Value value) {
this.key = key;
this.value = value;
this.count = 1;
}
}
private Node root;
public BST() {
root = null;
}
public int size() {
return size(root);
}
public Key floor(Key key) {
Node x = floor(root, key);
if (x == null) {
return null;
}
return x.key;
}
private Node floor(Node node, Key key) {
if (node == null) {
return null;
}
int cmp = key.compareTo(node.key);
if (cmp == 0) {
return node;
}
if (cmp < 0) {
return floor(node.left, key);
}
Node temp = floor(node.right, key);
if (temp != null) {
return temp;
} else {
return node;
}
}
private int size(Node node) {
if (node == null) {
return 0;
}
return node.count;
}
public Value get(Key key) {
Node ptr = root;
while (ptr != null) {
int cmp = key.compareTo(ptr.key);
if (cmp < 0) {
ptr = ptr.left;
} else if (cmp > 0) {
ptr = ptr.right;
} else {
return ptr.value;
}
}
return null;
}
public void delete(Key key) {
root = delete(root, key);
}
private Node delete(Node node, Key key) {
if (node == null) {
return null;
}
int cmp = key.compareTo(node.key);
if (cmp < 0) {
node.left = delete(node.left, key);
} else if (cmp > 0) {
node.right = delete(node.right, key);
} else {
if (node.left == null) {
return node.right;
}
if (node.right == null) {
return node.left;
}
Node temp = node;
node = min(temp.right);
node.right = deleteMin(temp.right);
node.left = temp.left;
}
node.count = 1 + size(node.left) + size(node.right);
return node;
}
private Node min(Node node) {
if (node == null) {
return null;
}
while (node.left != null) {
node = node.left;
}
return node;
}
public void deleteMin() {
if (root == null) {
return;
}
root = deleteMin(root);
}
private Node deleteMin(Node node) {
if (node.left == null) {
return node.right;
}
node.left = deleteMin(node.left);
node.count = 1 + size(node.left) + size(node.right);
return node;
}
private Key largest(Node node) {
if (node == null) {
return null;
}
while (node.right != null) {
node = node.right;
}
return node.key;
}
public Key largest() {
return largest(root);
}
public boolean isEmpty() {
return root == null;
}
private Node put(Node node, Key key, Value value) {
if (node == null) {
return new Node(key, value);
}
int cmp = key.compareTo(node.key);
if (cmp < 0) {
node.left = put(node.left, key, value);
} else if (cmp > 0) {
node.right = put(node.right, key, value);
} else {
node.value = value;
}
node.count = 1 + size(node.left) + size(node.right);
return node;
}
public void put(Key key, Value value) {
root = put(root, key, value);
}
public static void main(String[] args) {
BST<Integer, String> tree = new BST<>();
tree.put(4, "abcd");
System.out.println(tree.get(4));
System.out.println(tree.size());
}
}