Repository navigation
Expand file tree
/
Copy pathCustomTree.java
More file actions
186 lines (162 loc) · 6.06 KB
/
Copy pathCustomTree.java
File metadata and controls
186 lines (162 loc) · 6.06 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
package com.javarush.task.task20.task2028;
import java.io.Serializable;
import java.util.*;
/*
Построй дерево(1)
*/
public class CustomTree extends AbstractList<String> implements Cloneable, Serializable {
Entry<String> root;
private int size;
public CustomTree() {
root = new Entry<>("root");
size = 0;
}
private void walkEntryTree(Entry entry, EntryVisitor visitor, String key) {
Queue<Entry> queue = new LinkedList<>();
do {
if (visitor.visitEntry(entry, key)) break;
if (entry.leftChild != null) queue.add(entry.leftChild);
if (entry.rightChild != null) queue.add(entry.rightChild);
} while ((entry = queue.poll()) != null);
}
@Override
public boolean add(String s) {
Add add = new Add();
walkEntryTree(root, add, s);
if (!add.result) {
ResetChild resetChild = new ResetChild();
walkEntryTree(root, resetChild, null);
walkEntryTree(root, add, s);
}
return add.result;
}
@Override
public int size() {
return size;
}
public String getParent(String s) {
GetParent getParent = new GetParent();
walkEntryTree(root, getParent, s);
return getParent.result;
}
@Override
public boolean remove (Object o) {
if (!(o instanceof String))
throw new UnsupportedOperationException();
Remove remove = new Remove();
walkEntryTree(root, remove, (String)o);
return remove.result;
}
// недоступны методы, принимающие в качестве параметра индекс элемента
@Override
public String get(int index) throws UnsupportedOperationException {
throw new UnsupportedOperationException("Index operations not supported");
}
@Override
public String set(int index, String element) throws UnsupportedOperationException {
throw new UnsupportedOperationException("Index operations not supported");
}
@Override
public void add(int index, String element) throws UnsupportedOperationException {
throw new UnsupportedOperationException("Index operations not supported");
}
@Override
public String remove(int index) throws UnsupportedOperationException {
throw new UnsupportedOperationException("Index operations not supported");
}
@Override
public List<String> subList(int fromIndex, int toIndex) throws UnsupportedOperationException {
throw new UnsupportedOperationException("Index operations not supported");
}
@Override
protected void removeRange(int fromIndex, int toIndex) throws UnsupportedOperationException {
throw new UnsupportedOperationException("Index operations not supported");
}
@Override
public boolean addAll(int index, Collection<? extends String> c) throws UnsupportedOperationException {
throw new UnsupportedOperationException("Index operations not supported");
}
static class Entry<T> implements Serializable {
String elementName;
boolean availableToAddLeftChildren, availableToAddRightChildren;
Entry<T> parent, leftChild, rightChild;
public Entry (String elementName) {
this.elementName = elementName;
availableToAddLeftChildren = true;
availableToAddRightChildren = true;
}
public boolean isAvailableToAddChildren () {
return availableToAddLeftChildren || availableToAddRightChildren;
}
}
interface EntryVisitor {
boolean visitEntry(Entry entry, String key); // if return true - walkEntryTree break
}
private class GetSize implements EntryVisitor {
private int result;
@Override
public boolean visitEntry(Entry entry, String key) {
result++;
return false;
}
}
private class ResetChild implements EntryVisitor {
@Override
public boolean visitEntry(Entry entry, String key) {
if (entry.leftChild == null) entry.availableToAddLeftChildren = true;
if (entry.rightChild == null) entry.availableToAddRightChildren = true;
return false;
}
}
private class Add implements EntryVisitor {
private boolean result;
@Override
public boolean visitEntry(Entry entry, String key) {
if (entry.isAvailableToAddChildren()) {
Entry newEntry = new Entry(key);
if (entry.availableToAddLeftChildren) {
entry.leftChild = newEntry;
entry.availableToAddLeftChildren = false;
} else {
entry.rightChild = newEntry;
entry.availableToAddRightChildren = false;
}
newEntry.parent = entry;
size++;
result = true;
}
return result;
}
}
private class GetParent implements EntryVisitor {
private String result;
@Override
public boolean visitEntry(Entry entry, String key) {
if (entry.elementName.equals(key)) {
result = entry.parent.elementName;
return true;
}
return false;
}
}
private class Remove implements EntryVisitor {
private boolean result;
@Override
public boolean visitEntry(Entry entry, String key) {
GetSize getSize = new GetSize();
if (entry.leftChild != null && entry.leftChild.elementName.equals(key)) {
walkEntryTree(entry.leftChild, getSize, null);
size -= getSize.result;
entry.leftChild = null;
result = true;
}
if (entry.rightChild != null && entry.rightChild.elementName.equals(key)) {
walkEntryTree(entry.rightChild, getSize, null);
size -= getSize.result;
entry.rightChild = null;
result = true;
}
return result;
}
}
}