-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBST.java
More file actions
121 lines (100 loc) · 3.55 KB
/
Copy pathBST.java
File metadata and controls
121 lines (100 loc) · 3.55 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
import java.util.LinkedList;
import java.util.Queue;
public class BST {
public class BinarySearchTreeNode<T> implements Comparable<BinarySearchTreeNode<T>>{
T data;
BinarySearchTreeNode<T> leftNode;
BinarySearchTreeNode<T> rightNode;
BinarySearchTreeNode(){}
BinarySearchTreeNode(T data){
this.data = data;
}
@Override
public int compareTo(BinarySearchTreeNode<T> o) {
return Integer.compare((Integer)o.data, (Integer)this.data);
}
}
public static void main(String args[]){
BST bst = new BST();
bst.insert(15);
bst.insert(10);
bst.insert(5);
bst.insert(9);
bst.insert(18);
bst.insert(29);
bst.insert(3);
bst.insert(2);
bst.printTree();
}
BinarySearchTreeNode<Integer> root = null;
BST() {}
public void insert(int data) {
root = insert(root, data);
}
public BinarySearchTreeNode<Integer> insert(BinarySearchTreeNode<Integer> node, int data) {
if (node == null) {
node = new BinarySearchTreeNode<Integer>(data);
return node;
}
if (node.data > data) {
node.leftNode = insert(node.leftNode, data);
} else {
node.rightNode = insert(node.rightNode, data);
}
return node;
}
public void remove(int data) {
root = remove(root,data);
}
public BinarySearchTreeNode<Integer> remove(BinarySearchTreeNode<Integer> node, int data){
if(node == null) return null;
if(node.data > data){
node.leftNode = remove(node.leftNode, data);
return node;
}else if (node.data < data){
node.rightNode = remove(node.rightNode, data);
return node;
}else {
if(node.leftNode == null && node.rightNode == null){
//Leaf Node
return null;
} else if(node.leftNode == null || node.rightNode == null){
//Just one child
if(node.leftNode == null){
return node.rightNode;
}else{
return node.leftNode;
}
}else{
//Has both children
//Either take the maximum from the left subtree (inplace predecessor)
//or the minimum from the right subtree (inplace successor)
BinarySearchTreeNode<Integer> minNode = node.rightNode;
while(minNode.leftNode != null){
minNode = minNode.leftNode;
}
node.data = minNode.data;
remove(node.rightNode, minNode.data);
return node;
}
}
}
public void printTree() {
print(root);
}
public void print(BinarySearchTreeNode<Integer> node){
Queue<BinarySearchTreeNode<Integer>> queue = new LinkedList<BinarySearchTreeNode<Integer>>();
queue.add(node);
while(!queue.isEmpty()){
BinarySearchTreeNode<Integer> item = queue.remove();
int leftData = (item.leftNode == null) ? -1 : item.leftNode.data;
int rightData = (item.rightNode == null) ? -1 : item.rightNode.data;
System.out.print(item.data+":L:"+leftData+",R:"+rightData);
if(item.leftNode != null) queue.add(item.leftNode);
if(item.rightNode != null) queue.add(item.rightNode);
System.out.println();
}
}
// public boolean search(int data){
// }
}