-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRope.java
More file actions
304 lines (280 loc) · 9.5 KB
/
Rope.java
File metadata and controls
304 lines (280 loc) · 9.5 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
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
import java.util.ArrayList;
import java.util.Stack;
public class Rope {
private Node root;
public Rope(String str) { // 'new'
String[] words = str.split("(?<=\\G.{5})"); //splits by length of 5.
createRope(words, 0, words.length - 1, null, ' ');
//first=first index of array and last=last index of array
}
public Rope(Node root) {
this.root = root;
}
public boolean isEmpty() {
return root == null;
}
private void createRope(String[] words, int first, int last, Node par, char childType) {
if (first < last) {
Node p = new Node();
if (par == null) {
root = p;
} else {
if (childType == 'l') {
par.setLeft(p);
} else {
par.setRight(p);
}
}
int count = last - first + 1;//number of all the leaves
int leftCount = count - count / 2;//number of the leaves on the left side
int len = 0;
for (int i = first; i < first + leftCount; i++) {
len += words[i].length();
}
p.setLen(len);
createRope(words, first, (first + leftCount - 1), p, 'l');
createRope(words, (first + leftCount), last, p, 'r');
} else { //first == last
Leaf p = new Leaf(words[first]); //or words[last] doesn't matter
if (par != null) {
if (childType == 'l') {
par.setLeft(p);
} else {
par.setRight(p);
}
} else {
root = new Node(p.getLen());
root.setLeft(p);
}
}
}
public void show() { // 'status'
System.out.println(getString());
}
public String getString() { //inorder traversal
Node p = root;
StringBuffer buffer = new StringBuffer();
if (p != null) {
Stack<Node> s = new Stack<>();
do {
while (p != null) {
s.push(p);
p = p.getLeft();
}
if (!s.isEmpty()) {
p = s.pop();
//code for showing string
if (p instanceof Leaf) {
Leaf tmp = (Leaf) p;
buffer.append(tmp.getData());
}
//end code for showing string
p = p.getRight();
}
} while (!s.isEmpty() || p != null);
}
return buffer.toString();
}
public static Rope getRope(String str, ArrayList<Rope> ropes) {
for (Rope rope : ropes) {
if (rope.getString().equals(str))
return rope;
}
return null;
}
public char index(int index) {
return findIndex(root, index);
}
public char findIndex(Node node, int index) {
if (node.getLen() <= index && node.getRight() != null)
return findIndex(node.getRight(), index - node.getLen());
if (node.getLeft() != null)
return findIndex(node.getLeft(), index);
if (node instanceof Leaf) {
Leaf leaf = (Leaf) node;
if (index < leaf.getLen())
return leaf.getData().charAt(index);
}
System.out.println("a problem has occurred.");
return ' ';
}
public void concat(Rope rope) {
Node newRoot = new Node();
newRoot.setLeft(root);
newRoot.setRight(rope.root);
int len = root.getLen();
if (root.getRight() != null)
len += root.getRight().getLen();
newRoot.setLen(len);
root = newRoot;
rope.root = null;
correctLengths();
}
public void insert(Rope rope, int index) {
if (index > getString().length()) {
System.out.println("index out of bounds.");
return;
}
if (index == getString().length() - 1) {
this.concat(rope);
return;
}
Rope lastRope = split(index);
rope.concat(lastRope);
this.concat(rope);
correctLengths();
}
public void delete(int i, int j) {
if (i >= 0 && i < j && j < getString().length()) {
Rope lastRope = split(i);
lastRope = lastRope.split(j - i);
this.concat(lastRope);
correctLengths();
return;
}
System.out.println("invalid argument.");
}
public Rope split(int index) {
if (root == null)
return null;
Rope otherRope;//second part of the rope
Node root = splitByIndex(index);
if (root instanceof Leaf) {
Leaf l = (Leaf) root;
Node temp = new Node(l.getLen());
temp.setLeft(l);
root = temp;
}
if (index >= this.root.getLen()) { //returned root is root of other rope
otherRope = new Rope(root);
} else { //this.root should be replaced with returned root
Node temp = this.root;
this.root = root;
otherRope = new Rope(temp);
}
correctLengths();
otherRope.correctLengths();
return otherRope;
}
public Node splitByIndex(int index) {
Node node = root;
Node parent = null;
Node rChild = null, lChild = null;
boolean lChildFirst = false;
index++;
while (true) {
if (node == null)
break;
else if (node instanceof Leaf) {
Leaf l = (Leaf) node;
Leaf left, right;
if (parent.getLeft() == node) {
left = new Leaf(l.getData().substring(0, index));
l.setData(l.getData().substring(index));
lChild = left;
lChildFirst = true;
} else {
right = new Leaf(l.getData().substring(index));
l.setData(l.getData().substring(0, index));
rChild = right;
}
break;
} else {
int len = node.getLen();
if (index < len) {
parent = node;
node = node.getLeft();
} else if (index > len) {
index -= len;
parent = node;
node = node.getRight();
} else {
parent = node;
rChild = node.getRight();
node.setRight(null);
break;
}
}
}//end while
Node rParent = null, lParent = null;
if (lChildFirst)
lParent = parent;
else
rParent = parent;
Node root = (lChildFirst) ? lChild : rChild;
while (true) {
if (lChildFirst) {
lChildFirst = false;
rParent = nearestRight(this.root, lParent, null);
if (rParent == null)
break;
root = rChild = rParent.getRight();
rParent.setRight(lChild);
lParent = nearestLeft(this.root, rParent, null);
if (lParent == null)
break;
root = lChild = lParent.getLeft();
lParent.setLeft(rChild);
} else {
lParent = nearestLeft(this.root, rParent, null);
if (lParent == null)
break;
root = lChild = lParent.getLeft();
lParent.setLeft(rChild);
rParent = nearestRight(this.root, lParent, null);
if (rParent == null)
break;
root = rChild = rParent.getRight();
rParent.setRight(lChild);
}
}
return root;
}
public Node nearestRight(Node root, Node node, Node rParent) {
if (root == null)
return null;
if (root == node)
return rParent;
Node right = nearestRight(root.getRight(), node, root);
if (right != null)
return right;
return nearestRight(root.getLeft(), node, rParent);
}
public Node nearestLeft(Node root, Node node, Node lParent) {
if (root == null)
return null;
if (root == node)
return lParent;
Node left = nearestLeft(root.getLeft(), node, root);
if (left != null)
return left;
return nearestLeft(root.getRight(), node, lParent);
}
public void correctLengths() {
Node p = root;
if (p != null) {
Stack<Node> s = new Stack<>();
do {
while (p != null) {
s.push(p);
p = p.getLeft();
}
if (!s.isEmpty()) {
p = s.pop();
//code for correcting numbers
if (!(p instanceof Leaf)) {
int len = 0;
if (p.getLeft() != null) {
len += p.getLeft().getLen();
if (p.getLeft().getRight() != null)
len += p.getLeft().getRight().getLen();
}
p.setLen(len);
}
//end code for correcting numbers
p = p.getRight();
}
} while (!s.isEmpty() || p != null);
}
}
}