-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBinarySearchTree.java
132 lines (118 loc) · 4.11 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
import static java.awt.geom.Path2D.contains;
public class BinarySearchTree <T extends Comparable <T>> {
private int nodeCount = 0; //Tracks the number of nodes in this BST.
private Node root = null; //The BST is a rooted tree, so we maintain a handle on the root node.
//Internal Node containing node references and the actual node data:
private class Node{
T data;
Node left, right;
public Node(Node left, Node right, T elem){
this .data = elem;
this.left = left;
this.right = right;
}
}
//Check if the binary tree is empty:
public boolean isEmpty(){
return size() == 0;
}
//Getting the number of nodes in the tree:
public int size(){
return nodeCount;
}
//Add an element to the tree and return true if we successfully perform an insertion.
public boolean add(T elem){
//Checking if the value already exists in the tree and ignoring it if so:
if(contains(elem)){
return false;
//Otherwise add the element to the tree:
}else{
root = add(root, elem);
nodeCount++;
return true;
}
}
//A private method to recursively add an element to the tree
private Node add(Node node, T elem){
//Base case: Found a leaf Node:
if(node == null){
node = new Node(null, null, elem);
}else{
if(elem.compareTo(node.data) < 0){
node.left = add(node.left, elem);
}else{
node.right = add(node.right, elem);
}
}
return node;
}
//Remove a value from the binary tree if it exists:
public boolean remove(T elem){
if(contains(elem)){
root = remove(root, elem);
nodeCount--;
return true;
}
return false;
}
private Node remove(Node node, T elem){
if(node == null){return null;}
int cmp = elem.compareTo(node.data);
if (cmp < 0){
node.left = remove (node.left, elem);
}else if(cmp > 0){
node.right = remove(node.right, elem);
//This is the case with only a right subtree or none at all
//In this case just swap the node we wish to remove with its right child.
}else{
if(node.left == null){
Node rightChild = node.right;
node.data = null;
node = null;
return rightChild;
//This is the case with only a left subtree or none at all
//In this case just swap the node we wish to remove with its left child.
}else if(node.right == null){
Node leftChild = node.left;
node.data = null;
node = null;
return leftChild;
/*When removing a node from a binary tree with two links, the successor can either be
the largest child from the left or the smallest child from the right. In this case we'll
look into the smallest child from the right and so go to the furthest left node in the right
subtree.
*/
}else{
Node tmp = digLeft(node.right);
node.data = tmp.data;
node.right = remove(node.right, tmp.data);
}
}
return node;
}
private Node digLeft(Node node){
Node cur = node;
while(cur.left != null){
cur = cur.left;
return cur;
}
return cur;
}
private boolean contains(T elem){
return contains(root, elem);
}
private boolean contains(Node node, T elem){
if (node == null) return false;
int cmp = elem.compareTo(node.data);
if (cmp < 0) return contains (node.left, elem);
else if(cmp > 0) return contains(node.right, elem);
else return true;
}
public int height(){
return height(root);
}
private int height(Node node){
if (node == null) return 0;
return Math.max(height(node.left), height(node.right)) + 1;
}
}