-
Notifications
You must be signed in to change notification settings - Fork 0
/
TreeSet.java
160 lines (137 loc) · 4.66 KB
/
TreeSet.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
import java.util.Comparator;
import java.util.ArrayList;
public class TreeSet<K> implements PriorityQueue<K> {
protected LinkedBinaryTree<K> tree = new LinkedBinaryTree<>();
private Comparator<K> comp;
public TreeSet() {
this(new DefaultComparator<K>());
}
public TreeSet(Comparator<K> comp) {
this.comp = comp;
tree.addRoot(null);
}
public int size() {
return (tree.size() - 1) / 2;
}
/** Utility used when inserting a new entry at a leaf of the tree */
private void expandExternal(Position<K> p, K key) {
tree.set(p, key);
tree.addLeft(p, null);
tree.addRight(p, null);
}
protected int compare(K a, K b) {
return comp.compare(a, b);
}
protected Position<K> root() { return tree.root(); }
protected Position<K> parent(Position<K> p) { return tree.parent(p); }
protected Position<K> left(Position<K> p) { return tree.left(p); }
protected Position<K> right(Position<K> p) { return tree.right(p); }
protected Position<K> sibling(Position<K> p) { return tree.sibling(p); }
protected boolean isRoot(Position<K> p) { return tree.isRoot(p); }
protected boolean isExternal(Position<K> p) { return tree.isExternal(p); }
protected boolean isInternal(Position<K> p) { return !isExternal(p); }
protected void set(Position<K> p, K e) { tree.set(p, e); }
protected K remove(Position<K> p) { return tree.remove(p); }
protected void rotate(Position<K> p) { tree.rotate(p); }
protected Position<K> restructure(Position<K> x) { return tree.restructure(x); }
public BinaryTree<K> getTree(){
return tree;
}
private Position<K> treeSearch(Position<K> p, K key) {
if (isExternal(p))
return p;
int comp = compare(key, p.getElement());
if (comp == 0)
return p;
else if (comp < 0)
return treeSearch(left(p), key);
else
return treeSearch(right(p), key);
}
protected Position<K> treeMin(Position<K> p) {
Position<K> walk = p;
while (isInternal(walk))
walk = left(walk);
return parent(walk);
}
protected Position<K> treeMax(Position<K> p) {
Position<K> walk = p;
while (isInternal(walk))
walk = right(walk);
return parent(walk);
}
public boolean contains(K key) throws IllegalArgumentException {
Position<K> p = treeSearch(root(), key);
rebalanceAccess(p);
return !isExternal(p);
}
public boolean put(K key) throws IllegalArgumentException {
Position<K> p = treeSearch(root(), key);
if (isExternal(p)) {
expandExternal(p, key);
rebalanceInsert(p);
return true;
}
return false;
}
public K remove(K key) throws IllegalArgumentException {
Position<K> p = treeSearch(root(), key);
if (isExternal(p)){
rebalanceAccess(p);
return null;
}
else {
K old = p.getElement();
if (isInternal(left(p)) && isInternal(right(p))) {
Position<K> replacement = treeMax(left(p));
set(p, replacement.getElement());
p = replacement;
}
Position<K> leaf = (isExternal(left(p)) ? left(p) : right(p));
Position<K> sib = sibling(leaf);
remove(leaf);
remove(p);
rebalanceDelete(sib);
return old;
}
}
public void insertList(ArrayList<K> list){
for (K key : list) put(key);
}
protected void rebalanceInsert(Position<K> p){}
protected void rebalanceDelete(Position<K> p){}
protected void rebalanceAccess(Position<K> p){}
public ArrayList<K> inOrder(){
ArrayList<K> list = new ArrayList<>();
placeInOrder(list, root());
return list;
}
protected void placeInOrder(ArrayList<K> output, Position<K> node){
if (!isExternal(left(node))){
placeInOrder(output, left(node));
}
output.add(node.getElement());
if (!isExternal(right(node))){
placeInOrder(output, right(node));
}
}
public K min(){
if(isEmpty()){return null;}
Position<K> min = treeMin(root());
K answer = min.getElement();
return answer;
}
public K removeMin(){
if(isEmpty()){return null;}
Position<K> min = treeMin(root());
K answer = min.getElement();
rebalanceDelete(min);
return remove(answer);
}
public void insert(K key){
Position<K> x = treeSearch(root(), key);
expandExternal(x, key);
rebalanceInsert(x);
}
public boolean isEmpty(){ return (size()==0);}
}