-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathScapegoatTree.java
More file actions
166 lines (149 loc) · 3.32 KB
/
ScapegoatTree.java
File metadata and controls
166 lines (149 loc) · 3.32 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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
// YOU SHOULD NOT MODIFY THIS FILE AT ALL.
// (This version will be used by the autograder; not yours!)
import java.lang.reflect.Array;
import java.util.Comparator;
public class ScapegoatTree<T>
extends BinarySearchTree<ScapegoatTree.Node<T>,T> {
/**
* An overestimate of n
*/
int q;
protected static class Node<T> extends BinarySearchTree.BSTNode<Node<T>,T> { }
public ScapegoatTree(Comparator<T> c) {
super(new Node<T>(), c);
}
public ScapegoatTree() {
this(new DefaultComparator<T>());
}
public boolean remove(T x) {
if (super.remove(x)) {
if (2*n < q) {
rebuild(r);
q = n;
}
return true;
}
return false;
}
/**
* Compute the ceiling of log_{3/2}(q)
* @param q
* @return the ceiling of log_{3/2}(q)
*/
protected static final int log32(int q) {
final double log23 = 2.4663034623764317;
return (int)Math.ceil(log23*Math.log(q));
}
/***
* Do a normal BinarySearchTree insertion, but return the depth
* of the newly inserted node.
* @param u - the new node to insert
* @return the depth of the newly inserted node, or -1 if the node
* was not inserted
*/
int addWithDepth(Node<T> u) {
Node<T> w = r;
if (w == nil) {
r = u;
n++; q++;
return 0;
}
boolean done = false;
int d = 0;
do {
int res = c.compare(u.x, w.x);
if (res < 0) {
if (w.left == nil) {
w.left = u;
u.parent = w;
done = true;
} else {
w = w.left;
}
} else if (res > 0) {
if (w.right == nil) {
w.right = u;
u.parent = w;
done = true;
}
w = w.right;
} else {
return -1;
}
d++;
} while (!done);
n++; q++;
return d;
}
public boolean add(T x) {
// first do basic insertion keeping track of depth
Node<T> u = newNode(x);
int d = addWithDepth(u);
if (d > log32(q)) {
// depth exceeded, find scapegoat
Node<T> w = u.parent;
while (3*size(w) <= 2*size(w.parent))
w = w.parent;
rebuild(w.parent);
}
return d >= 0;
}
public void rebuild() {
rebuild(r);
}
@SuppressWarnings("unchecked")
protected void rebuild(Node<T> u) {
int ns = size(u);
Node<T> p = u.parent;
Node<T>[] a = (Node<T>[]) Array.newInstance(Node.class, ns);
packIntoArray(u, a, 0);
if (p == nil) {
r = buildBalanced(a, 0, ns);
r.parent = nil;
} else if (p.right == u) {
p.right = buildBalanced(a, 0, ns);
p.right.parent = p;
} else {
p.left = buildBalanced(a, 0, ns);
p.left.parent = p;
}
}
/**
* A recursive helper that packs the subtree rooted at u into
* a[i],...,a[i+size(u)-1]
*
* @param u
* @param a
* @param i
* @return size(u)
*/
protected int packIntoArray(Node<T> u, Node<T>[] a, int i) {
if (u == nil) {
return i;
}
i = packIntoArray(u.left, a, i);
a[i++] = u;
return packIntoArray(u.right, a, i);
}
/**
* A recursive helper that builds a perfectly balanced subtree out of
* a[i],...,a[i+ns-1]
*
* @param a
* @param i
* @param ns
* @return the rooted of the newly created subtree
*/
protected Node<T> buildBalanced(Node<T>[] a, int i, int ns) {
if (ns == 0)
return nil;
int m = ns / 2;
a[i + m].left = buildBalanced(a, i, m);
if (a[i + m].left != nil)
a[i + m].left.parent = a[i + m];
a[i + m].right = buildBalanced(a, i + m + 1, ns - m - 1);
if (a[i + m].right != nil)
a[i + m].right.parent = a[i + m];
return a[i + m];
}
}