-
-
Notifications
You must be signed in to change notification settings - Fork 5
/
TreeNode.java
96 lines (86 loc) · 2.21 KB
/
TreeNode.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
class TreeNode {
int val;
TreeNode left;
TreeNode right;
TreeNode(int x) {
val = x;
left = null;
right = null;
}
}
class BST {
private TreeNode root;
BST() {
root = null;
}
public void insert(int val) {
root = insertHelper(root, val);
}
private TreeNode insertHelper(TreeNode root, int val) {
if (root == null) {
return new TreeNode(val);
}
if (val < root.val) {
root.left = insertHelper(root.left, val);
} else if (val > root.val) {
root.right = insertHelper(root.right, val);
}
return root;
}
public void delete(int val) {
root = deleteHelper(root, val);
}
private TreeNode deleteHelper(TreeNode root, int val) {
if (root == null) {
return null;
}
if (val < root.val) {
root.left = deleteHelper(root.left, val);
} else if (val > root.val) {
root.right = deleteHelper(root.right, val);
} else {
if (root.left == null) {
return root.right;
} else if (root.right == null) {
return root.left;
}
root.val = findMin(root.right);
root.right = deleteHelper(root.right, root.val);
}
return root;
}
private int findMin(TreeNode root) {
while (root.left != null) {
root = root.left;
}
return root.val;
}
public void inOrderTraversal() {
inOrderHelper(root);
}
private void inOrderHelper(TreeNode root) {
if (root == null) {
return;
}
inOrderHelper(root.left);
System.out.print(root.val + " ");
inOrderHelper(root.right);
}
}
public class BinarySearchTree {
public static void main(String[] args) {
BST tree = new BST();
tree.insert(50);
tree.insert(30);
tree.insert(20);
tree.insert(40);
tree.insert(70);
tree.insert(60);
tree.insert(80);
tree.inOrderTraversal();
System.out.println();
tree.delete(20);
tree.delete(40);
tree.inOrderTraversal();
}
}