-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBinarySearchTree.java
More file actions
140 lines (130 loc) · 5.04 KB
/
BinarySearchTree.java
File metadata and controls
140 lines (130 loc) · 5.04 KB
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
public class BinarySearchTree {
public BinarySearchTree left;
public Integer key;
public BinarySearchTree right;
//constructor, to create an empty BST
public BinarySearchTree() {
this.key = null;
this.left = null;
this.right = null;
}
//constructor to create a new node, to be able to insert new keys into the tree
public BinarySearchTree(int node) {
this.key = node;
this.left = null;
this.right = null;
}
/**
* insert new keys into the BST
* @param key
*/
public void insert(int key) {
if (this.key == null) { //if the current key is null (tree is empty)
this.key = key; //insert a root key into the BST
this.left = null; //initialize the left child to null
this.right = null; //initialize the right child to null
}
else if (key < this.key) { //if the subsequent key is less than the root key
if (this.left == null) {
this.left = new BinarySearchTree(key); //create and insert the new subsequent key in the left child of the root key
}
else {
this.left.insert(key); //recursively insert the key on the left child node
}
}
else if (key > this.key) { //if the subsequent key is greater than the root key
if (this.right == null) {
this.right = new BinarySearchTree(key); //create and insert the subsequent key in the right child of the root key
}
else {
this.right.insert(key); //recursively insert the key on the right child node
}
}
}
/**
* Delete a key from the tree
* @param key
* @return
*/
public BinarySearchTree delete(int key) {
if (this.key == null) { //checks if the tree is empty
return null;
}
else if (key < this.key) { //if the key to be deleted is less than the root key
if (this.left != null) {
this.left = this.left.delete(key); //recursively calling the delete method on the left child
}
}
else if (key > this.key) { //if the key to be deleted is greater than the root key
if (this.right != null) {
this.right = this.right.delete(key); //recursively calling the delete method on the right child
}
}
//if the key to be deleted is equal to the root key
else if (key == this.key) {
if (this.left == null) {
return this.right; //if the left child is null, then return the right child
}
else if (this.right == null) {
return this.left; //if the right child is null, then return the left child
}
//if the left and right children are not null
else {
BinarySearchTree root = this; //root is the root node
BinarySearchTree rootChild = this.right; //rootChild the right child of the root node
while (rootChild.left != null) {
root = rootChild;
rootChild = rootChild.left;
}
if (root != this) {
root.left = rootChild.right;
}
else {
root.right = rootChild.right;
}
this.key = rootChild.key;
}
}
return this;
}
/**
* Find a key from the tree
* @param root
* @param searchKey
* @return
*/
public static BinarySearchTree find(BinarySearchTree root, int searchKey) {
BinarySearchTree current = root; //the root key
while (current != null && current.key != searchKey) {
if (current.key > searchKey){ //if the root key is greater than the key to be searched
current = current.left; //the left child of the root key becomes the new root key
}
else {
current = current.right; //the right child of the root key becomes the new root key
}
}
return current; //return the found node or null if the key is not found
}
//inorder Traversal
public void inOrder() {
printTree(this);
System.out.println(); //move to the next line after printing the tree
}
/**
* helper method to print the tree in in-order traversal
* @param node
*/
private void printTree(BinarySearchTree node) {
if (node != null) {
if (node.left != null) {
printTree(node.left);
System.out.print(" -> "); //print arrow after left child
}
if (node.right != null) {
printTree(node.right);
System.out.print(" -> "); //print arrow before right child
}
System.out.print(node.key); //print the current node keyy
}
}
}