-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBinary Search Trees.java
More file actions
137 lines (132 loc) · 3.28 KB
/
Binary Search Trees.java
File metadata and controls
137 lines (132 loc) · 3.28 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
//Binary Search Tree: java implementation
private class Node {
private Key key;
private Value val;
private Node left, right;
public Node(Key key, Value, val){
this.key = key;
this.val = val;
}
}
public Value get(Key key) {
Node x = root;
while (x!=null){
int cmp = key.compareTo(x.key);
if (cmp < 0) x = x.left;
else if (cmp > 0) x = x.right;
else return x.val;
}
return null;
}
public void put(Key key, Value val) {
root = put (root, key, val);
}
private Node put(Node x, Key key, Value val) {
if (x == null) return new Node(key, val);
int cmp = key.compareTo(x.key);
if (cmp < 0)
x.left = put(x.left, key, val);
else if (cmp > 0)
x.right = put(x.right, key, val);
else
x.val = val;
return x;
}
//computing the floor: largest key less than given key
public Key floor(Key key) {
Node x = floor (root , key);
if (x == null) return null;
return x.key;
}
private Node floor (Node x, Key key) {
if (x == null) return null;
int cmp = key.compareTo(x.key);
if(cmp == 0) return x;
if(cmp < 0) return floor(x.left, key);
Node t = floor(x.right, key);
if(t != null) return t;
else return x; //t == null right subtree 里面没有floor
}
//subtree counts
private class Node {
private Key key;
private Value val;
private Node left;
private Node right;
private int count;
}
public int size(){
return size(root);
}
private int size(Node x) {
if(x == null) return 0;
return x.count;
}
public void put(Key key, Value val) {
root = put (root, key, val);
}
private Node put(Node x, Key key, Value val) {
if (x == null) return new Node(key, val);
int cmp = key.compareTo(x.key);
if (cmp < 0)
x.left = put(x.left, key, val);
else if (cmp > 0)
x.right = put(x.right, key, val);
else
x.val = val;
x.count = 1 + size(x.left) + size.(x.right); // one more line
return x;
}
//Rank: how many keys < k?
public int rank (Key, key){
return rank(key, root);
}
private int rank(Key key, Node x) {
if (x == null) return 0;
int cmp = key.compareTo(x.key);
if (cmp < 0) return rank(key, x.left);
else if (cmp > 0) return 1 + size(x.left) + rank(key, x.right);
else if (cmp == 0) return size(x.left);
}
//Inorder traversal
public Iterable<Key> keys(){
Queue<Key> q = new Queue<Key>();
inorder(root, q);
return q;
}
private void inorder (Node x, Queue<Key> q) {
if(x == null) return;
inorder(x.left, q);
q.enqueue(x.key);
inorder(x.right, q);
}
//delete the minimum
public void deleteMin() {
root = deleteMin(root);
}
private Node deleteMin(Node x) {
if(x.left == null) return x.right;
x.left = deleteMin(x.left);
x.count = 1 +size(x.left) + size(x.right);
return x;
}
//Hibbard deletion:delete a node with key k, search for node t containing key k.Find successor x of t which is the minimum in t's right subtree. put x in t's spot.
public void delete(Key,key) {
root = delete(root, key);
}
private Node delete(Node x, Key, key) {
if (x == null) return null;
int cmp = key.compareTo(x.key);
if (cmp < 0) x.left = delete(x.left, key); //search for key
else if(cmp > 0) x.right = delete(x.right, key);
else {
if(x.right == null) return x.left; //no right child
if(x.left == null) return x.right; // no left child
Node t = x;
x = min(t.right);
x.right = deleteMin(t.right);
x.left = t.left;
}
x.count = size(x.left) + size(x.right) + 1;
return x;
}