Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Latest commit

 

History

History
History
208 lines (185 loc) · 6 KB

File metadata and controls

208 lines (185 loc) · 6 KB
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
public class ArrayBinaryTree<T> {
protected MyArray<T> tree;
public int getSize(){
int count = 0;
for(T ele : tree){
if(ele != null){
count++;
}
}
return count;
}
public ArrayBinaryTree(MyArray<T> initialArray){
tree = initialArray;
}
public MyArray<T> preOrderTraverse(){
MyArray<T> orderedArray = new MyArray<>(tree.getCapacity());
_preOrderTraverse(0, orderedArray);
return orderedArray;
}
private void _preOrderTraverse(int index, MyArray<T> array){
if(tree==null || index >= tree.getSize()){
return;
}
if(tree.getData(index)==null){
return;
}
array.insert(tree.getData(index));
int leftChildIndex = 2 * index + 1;
int rightChildIndex = 2 * index + 2;
_preOrderTraverse(leftChildIndex, array);
_preOrderTraverse(rightChildIndex, array);
}
public MyArray<T> preOrderTraverse_stack(){
MyArray<T> array = new MyArray<T>(tree.getSize());
LinkedListStack<Integer> stack = new LinkedListStack<>(null);
stack.push(0);
while (stack.getSize() > 0){
Integer index = stack.getData(stack.getSize() - 1);
stack.pop();
array.insert(tree.getData(index));
int leftChildIndex = 2 * index + 1;
int rightChildIndex = 2 * index + 2;
if(rightChildIndex < tree.getSize() && tree.getData(rightChildIndex) != null){
stack.push(rightChildIndex);
}
if(leftChildIndex < tree.getSize() && tree.getData(leftChildIndex) != null){
stack.push(leftChildIndex);
}
}
return array;
}
public String printTree(String option){
MyArray<T> array = null;
switch (option){
case "pre":
array = preOrderTraverse_stack();
break;
case "in":
array = inOrderTraverse();
break;
case "post":
array = postOrderTraverse();
break;
case "breadth":
array = breadthFirstTraverse();
}
if(array == null){
return "Failed traverse.";
}
return array.printArray();
}
public MyArray<T> breadthFirstTraverse() {
MyArray<T> array = new MyArray<>(tree.getCapacity());
for(T data: tree){
if(data != null){
array.insert(data);
}
}
return array;
}
protected String displayTree() {
return tree.printArray();
}
public MyArray<T> postOrderTraverse() {
MyArray<T> orderedArray = new MyArray<>(tree.getCapacity());
_postOrderTraverse(0, orderedArray);
return orderedArray;
}
private void _postOrderTraverse(int index, MyArray<T> array) {
if(tree==null || index >= tree.getSize()){
return;
}
if(tree.getData(index)==null){
return;
}
int leftChildIndex = 2 * index + 1;
int rightChildIndex = 2 * index + 2;
_postOrderTraverse(leftChildIndex, array);
_postOrderTraverse(rightChildIndex, array);
array.insert(tree.getData(index));
}
public MyArray<T> inOrderTraverse() {
MyArray<T> orderedArray = new MyArray<>(tree.getCapacity());
_inOrderTraverse(0, orderedArray);
return orderedArray;
}
private void _inOrderTraverse(int index, MyArray<T> array) {
if(tree==null || index >= tree.getSize()){
return;
}
if(tree.getData(index)==null){
return;
}
int leftChildIndex = 2 * index + 1;
int rightChildIndex = 2 * index + 2;
_inOrderTraverse(leftChildIndex, array);
array.insert(tree.getData(index));
_inOrderTraverse(rightChildIndex, array);
}
public int getLevels(){
return _getLevels(0);
}
private int _getLevels(int index) {
if(index >= tree.getSize()){
return 0;
}
int leftChildIndex = 2 * index + 1;
int rightChildIndex = 2 * index + 2;
int leftLevels = _getLevels(leftChildIndex);
int rightLevels = _getLevels(rightChildIndex);
return 1 + Math.max(rightLevels, leftLevels);
}
public int getLeaveNum(){
return _getLeaveNum(0);
}
private int _getLeaveNum(int index) {
if(index >= tree.getSize()){
return 0;
}
int leftChildIndex = 2 * index + 1;
int rightChildIndex = 2 * index + 2;
if(leftChildIndex >= tree.getSize() && rightChildIndex >= tree.getSize()){
return 1;
}
return _getLeaveNum(leftChildIndex) + _getLeaveNum(rightChildIndex);
}
public boolean isCompleteTree(){
boolean marked = false;
for(T data: tree){
if(marked){
if(data != null){
return false;
}
}
if(data == null){
marked = true;
}
}
return true;
}
protected int getParent(int childIndex){
if(childIndex<0 || childIndex > tree.getSize()){
return -2;
}
if(childIndex == 0){
return -1;
}
if(childIndex % 2 != 0){
return (childIndex - 1) / 2;
}else {
return (childIndex - 2) / 2;
}
}
public static void main(String[] args) {
MyArray<Integer> array = new MyArray<>(9);
array.buildArray(new Integer[]{1,2,3,4,5,null,7});
ArrayBinaryTree<Integer> tree = new ArrayBinaryTree<>(array);
System.out.print(tree.printTree("pre"));
System.out.print(tree.printTree("in"));
System.out.print(tree.printTree("post"));
System.out.print(tree.printTree("breadth"));
System.out.print("levels: " + tree.getLevels());
System.out.print("is complete: " + tree.isCompleteTree());
}
}
Morty Proxy This is a proxified and sanitized view of the page, visit original site.