-
Notifications
You must be signed in to change notification settings - Fork 0
/
BST.java
227 lines (207 loc) · 6.09 KB
/
BST.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
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
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
import java.lang.Integer;
import java.lang.System;
import java.util.ArrayList;
import java.util.Stack;
class BST{
private class TreeNode{
public TreeNode left = null;
public TreeNode right = null;
public int val = 0;
public TreeNode(){}
public TreeNode(int init){
val = init;
}
}
private TreeNode root = null;
public BST(){
root = new TreeNode();
}
public void preorder(TreeNode node, ArrayList<Integer> result){
if (node != null){
System.out.printf("%d ",node.val);
result.add(node.val);
preorder(node.left,result);
preorder(node.right,result);
}
}
public void inorder(TreeNode node, ArrayList<Integer> result){
if (node != null){
inorder(node.left,result);
System.out.printf("%d ", node.val);
result.add(node.val);
inorder(node.right,result);
}
}
public void postorder(TreeNode node, ArrayList<Integer> result){
if (node != null){
postorder(node.left,result);
postorder(node.right, result);
System.out.printf("%d ", node.val);
result.add(node.val);
}
}
public TreeNode search(TreeNode node, int val){
if(node != null){
if(val == node.val){
return node;
}else if (val<node.val){
return search(node.left, val);
}else {
return search(node.right, val);
}
}
return null;
}
public TreeNode searchI(TreeNode node, int val){
TreeNode ptr = node;
while (ptr != null){
if (val == ptr.val){
return ptr;
}else if (val < ptr.val){
ptr = ptr.left;
}else {
ptr = ptr.right;
}
}
return null;
}
public void insert(TreeNode node, int val){
if (node == null){
node = new TreeNode(val);
}else if(val < node.val){
insert(node.left,val);
}else {
insert(node.right, val);
}
}
public boolean delete(TreeNode node, TreeNode par, int val){
if (node == null){
return false;
}
if (par == null){
par = new TreeNode();
par.left = node;
}
TreeNode parent = par;
TreeNode ptr = node;
while (ptr != null && ptr.val != val){
if (val < ptr.val){
parent = ptr;
ptr = ptr.left;
}else {
parent = ptr;
ptr = ptr.right;
}
}
if (ptr == null){
return false;
}else {
if (ptr.left == ptr.right && ptr.left == null){
if (parent.left == ptr){
parent.left = null;
}else {
parent.right = null;
}
}else if(ptr.left == null){
if (parent.left == ptr){
parent.left = ptr.right;
}else {
parent.right = ptr.right;
}
}else if (ptr.right == null){
if (parent.left == ptr){
parent.left = ptr.left;
}else {
parent.right = ptr.left;
}
}else {
if (parent.left == ptr){
TreeNode p = ptr;
while (p.right != null){
p = p.right;
}
ptr.val = p.val;
return delete(ptr.right,ptr,p.val);
}else {
TreeNode p = ptr;
while (p.left != null){
p = p.left;
}
ptr.val = p.val;
return delete(ptr.left,ptr,p.val);
}
}
return true;
}
}
public ArrayList<Integer> preorderI(TreeNode node){
Stack<TreeNode> st = new Stack<TreeNode>();
ArrayList<Integer> result = new ArrayList<Integer>();
if (node == null){
return result;
}
st.push(node);
TreeNode ptr = node;
while (!st.isEmpty()){
result.add(ptr.val);
if (ptr.left != null){
st.push(ptr.left);
ptr = ptr.left;
}else {
ptr = st.pop();
st.push(ptr.right);
ptr = ptr.right;
}
}
return result;
}
public ArrayList<Integer> inorderI(TreeNode node){
Stack<TreeNode> st = new Stack<TreeNode>();
ArrayList<Integer> result = new ArrayList<Integer>();
if (node == null){
return result;
}
st.push(node);
TreeNode ptr = node;
while (!st.isEmpty()){
if (ptr.left != null){
st.push(ptr.left);
ptr = ptr.left;
}else {
ptr = st.pop();
result.add(ptr.val);
st.push(ptr.right);
ptr = ptr.right;
}
}
return result;
}
public ArrayList<Integer> postorderI(TreeNode node){
Stack<TreeNode> st = new Stack<TreeNode>();
ArrayList<Integer> result = new ArrayList<Integer>();
if (node == null){
return result;
}
st.push(node);
TreeNode pre = null;
while (!st.isEmpty()){
TreeNode ptr = st.peek();
if (pre == null || pre.left == ptr || pre.right == ptr){
if (ptr.left != null){
st.push(ptr.left);
}else {
st.push(ptr.right);
}
}else if (ptr.left == pre){
if (ptr.right != null){
st.push(ptr.right);
}
}else {
result.add(ptr.val);
st.pop();
}
pre = ptr;
}
return result;
}
}