-
Notifications
You must be signed in to change notification settings - Fork 0
/
BinarySearchTree.java
73 lines (58 loc) · 1.7 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
public class BinarySearchTree <Key extends Comparable, Value>{
BSTNode root;
private class BSTNode{
Key key;
Value value;
BSTNode left;
BSTNode right;
BSTNode(Key k, Value v){
key = k;
value = v;
}
}
//insertion
public void insert(Key key, Value value) {
root = insert(root, key, value);
}
private BSTNode insert(BSTNode x, Key key, Value value){
if (x == null) return new BSTNode(key, value);
int cmp = x.key.compareTo(key);
if (cmp < 0)
x.left = insert(x.left, key, value);
else if (cmp >0)
x.right = insert(x.right, key, value);
else
x.value = value;
return x;
}
//deletion
public void delete(Key key){
root = delete(root, key);
}
private BSTNode delete(BSTNode node, Key key){
if (node == null) return null;
if (key == null) return node;
int cmp = key.compareTo(node.key);
if (cmp < 0) node.left = delete(node.left, key);
else if (cmp > 0) node.right = delete(node.right, key);
else{
if (node.left == null) return node.right;
if (node.right == null) return node.left;
BSTNode temp = node;
node = min(node.right);
node.right = delMin(temp.right);
node.left =temp.left;
}
return node;
}
private BSTNode min(BSTNode n){
if (n.left == null) return n;
return min(n.left);
}
private BSTNode delMin(BSTNode node){
if (node.left == null) return node.right;
node.left = delMin(node.left);
return node;
}
//search
}