-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBinarySearchTree.java
164 lines (131 loc) · 3.57 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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
package DSImplementations;
import java.util.NoSuchElementException;
public class BinarySearchTree {
static class Node {
int data;
Node left, right;
public Node(int data) {
this.data = data;
this.left = this.right = null;
}
}
Node root;
void insert(int data) {
Node newNode = new Node(data);
if (root == null) {
root = newNode;
return;
}
Node current = root;
while (true) {
if (data < current.data) {
if (current.left == null) {
current.left = newNode;
return;
}
current = current.left;
} else {
if (current.right == null) {
current.right = newNode;
return;
}
current = current.right;
}
}
}
boolean delete(int target) {
Node current = root;
Node parent = null;
// find target's Node
while (current != null && current.data != target) {
parent = current;
if (current.data < target) {
current = current.right;
} else {
current = current.left;
}
}
// if target not found return false.
if (current == null) {
return false;
}
// Case 1: If node has no child, just delete it
if (current.left == null && current.right == null) {
if (current == root) {
root = null;
} else if (current == parent.left) {
parent.left = null;
} else {
parent.right = null;
}
}
// Case 2: If node has one child, replace the node with its child
else if (current.left == null || current.right == null) {
Node child = current.left == null ? current.right : current.left;
if (current == root) {
root = child;
} else if (parent == current.left) {
parent.left = child;
} else {
parent.right = child;
}
}
// Case 3: If node has two children, replace the node's value with the successor's value
else {
Node successor = findSuccessor(current);
current.data = successor.data;
delete(successor.data);
}
return true;
}
private Node findSuccessor(Node node) {
Node current = node.right;
while (current.left != null) {
current = current.left;
}
return current;
}
Node search(int target) {
Node current = root;
while (current != null) {
if (current.data == target) {
return current;
} else if (current.data < target) {
current = current.right;
} else {
current = current.left;
}
}
return null;
}
int findMax() {
if (root == null) {
throw new NoSuchElementException("Binary Search Tree is empty");
}
Node current = root;
while (current.right != null) {
current = current.right;
}
return current.data;
}
int findMin() {
if (root == null) {
throw new NoSuchElementException("Binary Search Tree is empty");
}
Node current = root;
while (current.left != null) {
current = current.left;
}
return current.data;
}
void inOrderTraversal() {
inOrderTraversal(root);
}
private void inOrderTraversal(Node node) {
if (node != null) {
inOrderTraversal(node.left);
System.out.print(node.data + " ");
inOrderTraversal(node.right);
}
}
}