-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathBinarySearchTree.java
147 lines (120 loc) · 3.25 KB
/
BinarySearchTree.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
package Trees;
import java.util.*;
public class BinarySearchTree<T extends Comparable<T>> extends BinaryTree {
private TreeNode<T> root;
private int size = 0;
public BinarySearchTree() {
root = null;
size = 0;
}
public void add(T val) {
root = add(val, root);
}
private TreeNode add(T val, TreeNode node) {
if (node == null) {
size++;
return new TreeNode<>(val);
}
if (node.compareTo(val) > 0) {
node.left = add(val, node.left);
} else if (node.compareTo(val) < 0) {
node.right = add(val, node.right);
}
return node;
}
public List<Object> inOrder() {
return super.inOrder(root);
}
public List<Object> preOrder() {
return super.preOrder(root);
}
public List<Object> postOrder() {
return super.postOrder(root);
}
private static boolean that = false;
public void remove(T val) {
that = false;
root = remove(val, root);
if (that) {
size++;
}
}
private TreeNode remove(T val, TreeNode node) {
if (node == null) {
return null;
}
if (node.compareTo(val) > 0) {
node.left = remove(val, node.left);
} else if (node.compareTo(val) < 0) {
node.right = remove(val, node.right);
} else {
if (node.left == null && node.right != null) {
size--;
return node.right;
} else if (node.right == null && node.left != null) {
size--;
return node.left;
} else if (node.right != null && node.left != null) {
T temp = getMin(node.right);
node.val = temp;
node.right = remove(temp, node.right);
that = true;
size--;
return node;
} else {
size--;
return null;
}
}
return node;
}
private T getMin(TreeNode node) {
if (node.left == null) {
return (T) node.val;
}
return getMin(node.left);
}
public T getMin() {
return getMin(root);
}
public boolean contains(T val) {
return contains(val, root);
}
private boolean contains(T val, TreeNode node) {
if (node == null) {
return false;
}
if (node.compareTo(val) == 0) {
return true;
}
return contains(val, node.left) || contains(val, node.right);
}
public List<List<Object>> levelOrder() {
return super.levelOrder(root);
}
public List<Object> leftView() {
return super.leftView(root);
}
public List<Object> rightView() {
return super.rightView(root);
}
public int size() {
return size;
}
public List<Object> topView() {
return super.topView(root);
}
public List<Object> bottomView() {
return super.bottomView(root);
}
public int height() {
return super.height(root);
}
public void clear() {
root = null;
}
@Override
public String toString() {
return this.inOrder().toString();
}
}