Repository navigation
Expand file tree
/
Copy pathCustomTree.java
More file actions
184 lines (160 loc) · 5.02 KB
/
Copy pathCustomTree.java
File metadata and controls
184 lines (160 loc) · 5.02 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
package com.javarush.task.task20.task2028;
import java.io.Serializable;
import java.util.AbstractList;
import java.util.Collection;
import java.util.LinkedList;
import java.util.List;
/*
Построй дерево(1)
*/
public class CustomTree extends AbstractList<String> implements Cloneable, Serializable {
Entry<String> root;
private int size;
public CustomTree() {
root = new Entry<>("admin");
}
@Override
public int size() {
size = 0;
if (root.leftChild != null) {
sizeToEntry(root.leftChild);
}
if (root.rightChild != null) {
sizeToEntry(root.rightChild);
}
return size;
}
public void sizeToEntry(Entry<String> entry) {
size++;
if (entry.leftChild != null) {
sizeToEntry(entry.leftChild);
}
if (entry.rightChild != null) {
sizeToEntry(entry.rightChild);
}
}
@Override
public String get(int index) {
throw new UnsupportedOperationException();
}
@Override
public String set(int index, String element) {
throw new UnsupportedOperationException();
}
@Override
public void add(int index, String element) {
throw new UnsupportedOperationException();
}
@Override
public String remove(int index) {
throw new UnsupportedOperationException();
}
@Override
public List<String> subList(int fromIndex, int toIndex) {
throw new UnsupportedOperationException();
}
@Override
protected void removeRange(int fromIndex, int toIndex) {
throw new UnsupportedOperationException();
}
@Override
public boolean addAll(int index, Collection<? extends String> c) {
throw new UnsupportedOperationException();
}
@Override
public boolean add(String s) {
LinkedList<Entry<String>> queue = new LinkedList<>();
queue.add(root);
while (!queue.isEmpty()) {
Entry<String> temp = queue.remove();
if (addToEntry(s, temp, queue)) {
return true;
}
}
return false;
}
public boolean addToEntry(String s, Entry<String> parent, LinkedList<Entry<String>> queue) {
if (parent.leftChild == null) {
parent.leftChild = new Entry<>(s);
parent.leftChild.parent = parent;
return true;
}
if (parent.rightChild == null) {
parent.rightChild = new Entry<>(s);
parent.rightChild.parent = parent;
return true;
}
queue.add(parent.leftChild);
queue.add(parent.rightChild);
return false;
}
public String getParent(String s) {
LinkedList<Entry<String>> queue = new LinkedList<>();
String result = null;
queue.add(root);
while (!queue.isEmpty()) {
Entry<String> temp = queue.remove();
if ( (result = getParentToEntry(s, temp, queue)) != null) {
return result;
}
}
return null;
}
public String getParentToEntry(String s, Entry<String> element, LinkedList<Entry<String>> queue) {
if (element.elementName.equals(s)) {
return element.parent.elementName;
}
if (element.leftChild != null) {
queue.add(element.leftChild);
}
if (element.rightChild != null) {
queue.add(element.rightChild);
}
return null;
}
public boolean remove(Object o) {
if (!(o instanceof String)) {
throw new UnsupportedOperationException();
}
String s = (String) o;
LinkedList<Entry<String>> queue = new LinkedList<>();
queue.add(root);
while (!queue.isEmpty()) {
Entry<String> temp = queue.remove();
if (removeEntry(s, temp, queue)) {
return true;
}
}
return false;
}
public boolean removeEntry(String s, Entry<String> parent, LinkedList<Entry<String>> queue) {
if (parent.leftChild != null) {
if (parent.leftChild.elementName.equals(s)) {
parent.leftChild = null;
return true;
}
queue.add(parent.leftChild);
}
if (parent.rightChild != null) {
if (parent.rightChild.elementName.equals(s)) {
parent.rightChild = null;
return true;
}
queue.add(parent.rightChild);
}
return false;
}
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;
}
}
}