-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBinarySearchTree.java
148 lines (133 loc) · 3.63 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
148
public class BinarySearchTree {
private TreeNode root;
public TreeNode find(Integer data) {
if (root != null)
return root.find(data);
return null;
}
public Integer largest() {
if (this.root != null)
return root.largest();
return null;
}
public Integer smallest() {
if (this.root != null)
return root.smallest();
return null;
}
public void insert(Integer data) {
if (root == null)
this.root = new TreeNode(data);
else
root.insert(data);
}
public int height() {
if (this.root == null) return 0;
return this.root.height();
}
public void delete(Integer data) {
TreeNode current = this.root;
TreeNode parent = this.root;
boolean isLeftChild = false;
if (current == null)
return; // tree is empty
while (current != null && current.getData() != data) {
parent = current;
if (data < current.getData()) {
current = current.getLeftChild();
isLeftChild = true;
} else {
current = current.getRightChild();
isLeftChild = false;
}
}
if (current == null)
return; // node to be deleted not found
// this is case 1
if (current.getLeftChild() == null && current.getRightChild() == null) {
if (current == root) {
root = null; // no elements in tree now
} else {
if (isLeftChild)
parent.setLeftChild(null);
else
parent.setRightChild(null);
}
}
// This is case 2 broken down further into 2 separate cases
else if (current.getRightChild() == null) {// current has left child
if (current == root) {
root = current.getLeftChild();
} else if (isLeftChild) {
parent.setLeftChild(current.getLeftChild());
} else {
parent.setRightChild(current.getLeftChild());
}
} else if (current.getLeftChild() == null) {// current has right child
if (current == root) {
root = current.getRightChild();
} else if (isLeftChild) {
parent.setLeftChild(current.getRightChild());
} else {
parent.setRightChild(current.getRightChild());
}
}
// This is case 3 - Most complicated (node to be deleted has 2 children)
else {
TreeNode successor = getSuccessor(current);
if (current == root)
root = successor;
else if (isLeftChild) {
parent.setLeftChild(successor);
} else {
parent.setRightChild(successor);
}
successor.setLeftChild(current.getLeftChild());
}
}
private TreeNode getSuccessor(TreeNode node) {
TreeNode parentOfSuccessor = node;
TreeNode successor = node;
TreeNode current = node.getRightChild();
while (current != null) {
parentOfSuccessor = successor;
successor = current;
current = current.getLeftChild();
}
if (successor != node.getRightChild()) {
parentOfSuccessor.setLeftChild(successor.getRightChild());
successor.setRightChild(node.getRightChild());
}
return successor;
}
public void traverseInOrder() {
if (this.root != null)
this.root.traverseInOrder();
System.out.println();
}
public int numOfLeafNodes() {
if (this.root == null) return 0;
return this.root.numOfLeafNodes();
}
public static BinarySearchTree createFromSortedArray(int[] data) {
BinarySearchTree bst = new BinarySearchTree();
if (data != null && data.length > 0) {
bst.root = TreeNode.addSorted(data, 0, data.length-1);
}
return bst;
}
public static void main(String[] args) {
int[] sample = { 212, 580, 6, 7, 28, 84, 112, 434};
BinarySearchTree bst = new BinarySearchTree();
for (int x : sample) {
bst.insert(x);
}
System.out.println(bst.find(65));
System.out.println(bst.smallest());
System.out.println(bst.largest());
// bst.delete(84);
System.out.println(bst.numOfLeafNodes());
System.out.println(bst.height());
bst.traverseInOrder();
}
}