-
Notifications
You must be signed in to change notification settings - Fork 0
/
Rope.java
149 lines (131 loc) · 2.7 KB
/
Rope.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
package uva.eda;
import java.util.List;
import java.util.ArrayList;
import java.util.Stack;
/**
* Implements texts Strings as Ropes
*
* @author Luis Alberto Centeno Bragado, Ángel Posada García
*
*/
public class Rope {
private String subString;
private Rope left;
private Rope right;
private int length;
private int depth;
/**
* Initializes a new Rope with a String
*/
public Rope(String text) {
subString = text;
length = text.length();
depth = 0;
}
/**
* Initializes a new Rope joining two Ropes given
*/
public Rope(Rope left, Rope right) {
this.left = left;
this.right = right;
length = left.length() + right.length();
if (left.depth() > right.depth())
depth = left.depth() + 1;
else
depth = right.depth() + 1;
}
/**
* USE WHEN DEPTH=0!! Returns the substring of this Rope
*/
public String getSubString() {
return subString;
}
/**
* USE WHEN DEPTH>0!! Returns the left Rope of this Rope
*/
public Rope getLeft() {
return left;
}
/**
* USE WHEN DEPTH>0!! Returns the right Rope of this Rope
*/
public Rope getRight() {
return right;
}
/**
* Returns the length of this Rope
*/
public int length() {
return length;
}
/**
* Returns the depth of this Rope
*/
public int depth() {
return depth;
}
/**
* Checks if this rope has depth 0.
*/
public boolean isLeaf() {
return depth() == 0;
}
/**
* Concatenates two Ropes by putting the Rope given on the right of this
* Rope
*/
public Rope concatenate(Rope right) {
return new Rope(this, right);
}
/**
* Returns the character allocated in the position given
*/
public char getChar(int position) {
if (isLeaf())
return getSubString().charAt(position);
else {
if (position < getLeft().length())
return getLeft().getChar(position);
else {
position -= getLeft().length();
return getRight().getChar(position);
}
}
}
/**
* Returns the Rope perfectly balanced
*/
public Rope balance() {
Stack<Rope> myStack = new Stack<Rope>();
List<Rope> myList = new ArrayList<Rope>();
Rope tmp;
myStack.push(this);
while (!myStack.empty()) {
tmp = myStack.pop();
if (tmp.isLeaf())
myList.add(tmp);
else {
myStack.push(tmp.getRight());
myStack.push(tmp.getLeft());
}
}
return getTree(myList);
}
/**
* Returns this rope as String
*/
public String toString() {
if (isLeaf())
return getSubString();
else
return getLeft().toString() + getRight().toString();
}
private Rope getTree(List<Rope> original) {
if (original.size() == 1)
return original.get(0);
else {
return new Rope(getTree(original.subList(0, original.size() / 2)),
getTree(original.subList(original.size() / 2, original.size())));
}
}
}