-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBST.java
229 lines (205 loc) · 5.02 KB
/
BST.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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
import java.io.*;
/**
* This is implementation of Binary Search Tree
*/
public class BST implements Serializable
{
BSTNode root;
int size = 0;
/**
* This no argument Constructor, which creates an object of BST
* @param Nothing.
* @return Nothing.
*/
public BST()
{
root = null;
}
/**
* This calls the recurssive method insert()
* It increases the size of BST by 1
* @param AccountData data .
* @return Nothing.
*/
void insert(AccountData key)
{
root = insertKey(root, key);
size++;
}
/**
* This Inserts AccountData into the tree
* @param key AccountData
* @param BSTNode node
* @return BSTNode node.
*/
BSTNode insertKey(BSTNode root, AccountData key)
{
// base case
if (root == null){
root = new BSTNode(key);
return root;
}
// else, recur down the tree
if (key.compareTo(root.key) < 0)
root.left = insertKey(root.left, key);
else if (key.compareTo(root.key) > 0)
root.right = insertKey(root.right, key);
// else return the "unchanged" node pointer
return root;
}
/**
* This just calls recursive deleteKey()
* decreases size by 1
* @param key String
* @return BSTNode node.
*/
public BSTNode delete(String key)
{
root = deleteKey(root, key);
size--;
return root;
}
/**
* This deletes the node that have data equals to given key
* @param key String
* @param root BSTNode
* @return BSTNode node.
*/
BSTNode deleteKey(BSTNode root, String key)
{
// Base Case
if (root == null)
return root;
// else recur down the tree
if (key.compareTo(root.key.ticker) < 0)
root.left = deleteKey(root.left, key);
else if (key.compareTo(root.key.ticker) > 0)
root.right = deleteKey(root.right, key);
// if key is same as root, then this node is to delete
else
{
// node with 1 or no Child
if (root.left == null)
return root.right;
else if (root.right == null)
return root.left;
// 2 childrn then get succser
root.key = minValue(root.right);
// Delete the inorder successor
root.right = deleteKey(root.right, root.key.ticker);
}
return root;
}
/**
* This gives the successor of given node
* @param root BSTNode
* @return BSTNode node.
*/
AccountData minValue(BSTNode root)
{
AccountData minv = root.key;
while (root.left != null)
{
minv = root.left.key;
root = root.left;
}
return minv;
}
/**
* This tells the size of tree
* @param nothing
* @return int size.
*/
public int getSize()
{
return size;
}
/**
* Just calls height()
* @param nothing
* @return int height.
*/
public int getHeight()
{
return height(root);
}
/**
* calculates the height of the tree
* @param BSTNode root
* @return int height.
*/
public int height(BSTNode root)
{
//base case OR when tree is empty
if (root == null)
return -1;
int Lheight = height(root.left);
int Rheight = height(root.right);
// return largest hieght
return (Lheight > Rheight) ? Lheight+1 : Rheight+1;
}
/**
* just calls searchHelper()
* @param String key
* @return boolean key found or not
*/
public boolean search(String key)
{
return searchHelper(key, root);
}
/**
* Search tree for a given key
* @param String key
* @param BSTNode current
* @return boolean key found or not
*/
public boolean searchHelper(String key, BSTNode current)
{
// base case
if(current == null)
return false;
if(key.compareTo(current.key.ticker) == 0 ){
System.out.printf("\nKey Found ! \n" + current.key);
return true;
}
boolean left = searchHelper(key, current.left);
boolean right = searchHelper(key,current.right);
return left || right;
}
/**
* just calls balance()
* @param nothing.
* @return float balance
*/
public float getBalance()
{
return balance(root);
}
/**
* calculates the balance of the tree
* @param BSTNode root
* @return float balance
*/
public float balance(BSTNode root)
{
float leftNodesP = ((float)countNodes(root.left) / getSize()) * 100 ;
float rightNodesP = 100 - leftNodesP;
return 100 - Math.abs(leftNodesP - rightNodesP);
}
/**
* This counts the number of all nodes below a given node
* @param BSTNode node
* @return int count.
*/
private int countNodes(BSTNode node)
{
if (node == null)
return 0;
else{
int count = 1;
count += countNodes(node.left);
count += countNodes(node.right);
return count;
}
}
}