-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTree.java
More file actions
230 lines (213 loc) · 6.31 KB
/
Copy pathTree.java
File metadata and controls
230 lines (213 loc) · 6.31 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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
import java.util.*;
/**
* Created by menna on 10/23/16.
*/
public class Tree {
private static Node root;
public Tree(int num, int count, char s, Node p, String bcode) { // construct tree with p null!!
root = new Node(num, count, s, p, bcode);
}
public Node add(char c) {
// BFS to find NYT node
Queue<Node> q = new LinkedList<>();
q.add(this.root);
Boolean found = false;
Node t = new Node();
while (!q.isEmpty()) {
t = q.remove();
if (t.symbol == '!') { // if its nyt
found = true;
break;
}
else {
if (t.right != null)
q.add(t.right);
if (t.left != null)
q.add(t.left);
}
}
if (t != null && found) {
t.symbol = '?'; // not new nyt anymore
t.counter = 1;
Node newNYT = new Node(t.nodeNum - 2, 0, '!', t, t.binarycode + '0'); // old NYT count = 1
t.setLeft(newNYT);
Node newNode = new Node(t.nodeNum - 1, 1, c, t, t.binarycode + '1'); // symbol count = 1
t.setRight(newNode);
return t;
}
else {
return null;
}
}
public Node swapWith(Node x) {
Queue<Node> q = new LinkedList<>();
q.add(this.root);
Node t = new Node();
while (!q.isEmpty()) {
t = q.remove();
if (t.swapConditions(x)) {
return t;
} else {
if (t.right != null)
q.add(t.right);
if (t.left != null)
q.add(t.left);
}
}
return null;
}
public Node swap(Node x, Node y) {
Boolean xisleft = x.isLeft();
Boolean yisleft = y.isLeft();
Node parentofx = x.parent;
Node parentofy = y.parent;
if (xisleft) {
parentofx.setLeft(y);
x.parent = y.parent;
if (yisleft) {
parentofy.setLeft(x);
y.parent = parentofx;
}
else {
parentofy.setRight(x);
y.parent = parentofx;
}
}
else {
parentofx.setRight(y);
x.parent = y.parent;
if (yisleft) {
parentofy.setLeft(x);
y.parent = parentofx;
}
else {
parentofy.setRight(x);
y.parent = parentofx;
} }
fixTree();
return x;
}
public void updateTree(Character symbol, Boolean first) {
if (first) {
Node parent = add(symbol); // splits, sets counters
while (!parent.isRoot()) {
parent = parent.parent;
Node toSwap = swapWith(parent); // anything to swap with parent?
if (toSwap != null) { // YES!
parent = swap(parent, toSwap);
}
parent.counter++;
}
}
else {
Node node = findNode(symbol);
while (!node.isRoot()) {
Node toSwap = swapWith(node);
if (toSwap != null) {
node = swap(node, toSwap);
}
node.counter++;
node = node.parent;
if (node.isRoot()) {
node.counter++;
}
}
}
}
public void printTree() { // for debugging
Queue<Node> q = new LinkedList<>();
q.add(this.root);
Node t = new Node();
while (!q.isEmpty()) {
t = q.remove();
System.out.println(t.symbol + " " + t.binarycode + " " + t.counter + " " + t.nodeNum + " " + t + "parent is: " + t.parent);
if (t.right != null)
q.add(t.right);
if (t.left != null)
q.add(t.left);
}
}
public void fixTree() { // fix nodenumbers and node binary code after swapping
Queue<Node> q = new LinkedList<>();
q.add(this.root);
int num = 100;
Node n; n = q.peek();
n.nodeNum = (num--);
n.binarycode = "";
while (!q.isEmpty()) {
n = q.remove();
if (n.right != null) {
n.right.binarycode = n.binarycode + "1";
n.right.nodeNum = num--;
q.add(n.right);
}
if (n.left != null) {
n.left.binarycode = n.binarycode + "0";
n.left.nodeNum = num--;
q.add(n.left);
}
}
}
public Node findNode(char c) {
Queue<Node> q = new LinkedList<>();
q.add(this.root);
Node t;
while (!q.isEmpty()) {
t = q.remove();
if (t.symbol == c) {
return t;
}
else {
if (t.right != null)
q.add(t.right);
if (t.left != null)
q.add(t.left);
}
}
return null;
}
public Node findNYT() {
// BFS to find NYT node
Queue<Node> q = new LinkedList<>();
q.add(this.root);
Node t = new Node();
while (!q.isEmpty()) {
t = q.remove();
if (t.symbol == '!') {// if its nyt
return t;
}
else {
if (t.right != null)
q.add(t.right);
if (t.left != null)
q.add(t.left);
}
}
return null;
}
public Node findSymbol(String code) {
Queue<Node> q = new LinkedList<>();
q.add(this.root);
Node t;
int counter = 0;
while (!q.isEmpty()) {
t = q.remove();
if (t.binarycode.equals(code)) {
return t;
}
else {
if (code.charAt(counter) == '1') {
if (t.right != null)
q.add(t.right);
counter++;
}
else if (code.charAt(counter) == '0') {
if (t.left != null)
q.add(t.left);
counter++;
}
}
}
return null;
}
}