-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathNonEmptyBST.java
142 lines (104 loc) · 3.31 KB
/
NonEmptyBST.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
package assn04;
import java.util.LinkedList;
import java.util.Queue;
public class NonEmptyBST<T extends Comparable<T>> implements BST<T> {
private T _element;
private BST<T> _left;
private BST<T> _right;
public NonEmptyBST(T element) {
_left = new EmptyBST<T>();
_right = new EmptyBST<T>();
_element = element;
}
// TODO: insert
@Override
public BST<T> insert(T element) {
if (element.compareTo(this._element) > 0) {
this._right = this.getRight().insert(element);
//System.out.println(element.toString() + " entered to right");
} else if (element.compareTo(this._element) < 0) {
this._left = this.getLeft().insert(element);
//System.out.println(element.toString() + " entered to left");
}
return this;
}
// TODO: remove
@Override
public BST<T> remove(T element) {
if (element.compareTo(this._element) > 0) {
this._right = _right.remove(element);
} else if (element.compareTo(this._element) < 0) {
this._left = _left.remove(element);
} else if (element.compareTo(this._element) == 0) {
if ((_left.isEmpty()) && (_right.isEmpty())) { //leaf
return new EmptyBST<>();
} else if (_left.isEmpty()) { //only right child
return _right;
} else if (_right.isEmpty()) { //only left child
return _left;
} else {
BST<T> temp = _right;
while(!temp.getLeft().isEmpty()){
temp = temp.getLeft();
}
T temp2 = temp.getElement();
this.remove(temp2);
this._element = temp2;
}
}
return this;
}
// TODO: printPreOrderTraversal
@Override
public void printPreOrderTraversal() {
}
// TODO: printPostOrderTraversal
@Override
public void printPostOrderTraversal() {
}
// TODO: printBreadthFirstTraversal
@Override
public void printBreadthFirstTraversal() {
Queue<BST<T>> q = new LinkedList<>();
printBreadthFirstTraversal2(q);
}
@Override
public void printBreadthFirstTraversal2(Queue<BST<T>> q) {
if (this._element == null) {
return;
} else {
System.out.print(this._element + " ");
if (this.getLeft().isEmpty() == false) {
q.add(this.getLeft());
}
if (this.getRight().isEmpty() == false) {
q.add(this.getRight());
}
if (q.size() != 0) {
q.poll().printBreadthFirstTraversal2(q);
} else {
return;
}
}
}
@Override
public int getHeight() {
return Math.max(_left.getHeight(), _right.getHeight()) + 1;
}
@Override
public BST<T> getLeft() {
return _left;
}
@Override
public BST<T> getRight() {
return _right;
}
@Override
public T getElement() {
return _element;
}
@Override
public boolean isEmpty() {
return false;
}
}