-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBST.java
More file actions
175 lines (153 loc) · 2.88 KB
/
BST.java
File metadata and controls
175 lines (153 loc) · 2.88 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
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
//Complete implementation of Binary Search Tree (BST)
//Structure of a node in BST
class Node{
int value;
Node left;
Node right;
Node p;
Node(int value){
this.value = value;
this.left = null;
this.right = null;
this.p = null;
}
}
public class BST{
private static Node root;
public BST(){
root = null;
}
//Insert a node with input value in BST
public void insert(int v){
Node node = new Node(v);
if(root == null){
root = node;
}
else{
Node x = root;
Node y = null;
while(x != null){
y = x;
if(v < x.value){
x = x.left;
}
else if(v > x.value){
x = x.right;
}
}
node.p = y;
if(v > y.value)
y.right = node;
else
y.left = node;
}
}
//Return a node in BST with given value
public Node getNode(int v){
if(root == null)
return null;
else{
Node x = root;
while(x != null && x.value != v){
if(v > x.value)
x = x.right;
else
x = x.left;
}
return x;
}
}
//Recursive inorder traversal
public void inorder(Node root){
if(root != null){
inorder(root.left);
System.out.print(root.value + " ");
inorder(root.right);
}
}
public void transplant(Node x, Node y){
if(x.p == null)
root = y;
else if(x == x.p.right)
x.p.right = y;
else
x.p.left = y;
if(y != null)
y.p = x.p;
}
//Minimum value node in tree rooted at r
public Node tree_minimum(Node r){
Node x = r;
while(x.left != null){
x = x.left;
}
return x;
}
//return successor of a node in BST
public Node successor(Node x){
if(x.right != null)
return tree_minimum(x.right);
Node y = x.p;
while(y != null && x == y.right){
x = y;
y = y.p;
}
return y;
}
//Delete a node from BST
public void delete(int v){
Node z = getNode(v);
if(z == null)
System.out.println("Node does not exists");
else if(z.left == null)
transplant(z, z.right);
else if(z.right == null)
transplant(z, z.left);
else{
Node y = successor(z);
if(z == y.p){
transplant(z,y);
y.left = z.left;
z.left.p = y;
}
else if(z != y.p){
transplant(y, y.right);
y.right = z.right;
y.right.p = y;
transplant(z,y);
y.left = z.left;
y.left.p = y;
}
}
}
public static void main(String[] args){
BST t = new BST();
System.out.println("Inserting 15 6 18 3 7 17 20 2 4 13 9");
t.insert(15);
t.insert(6);
t.insert(18);
t.insert(3);
t.insert(7);
t.insert(17);
t.insert(20);
t.insert(2);
t.insert(4);
t.insert(13);
t.insert(9);
System.out.println("Inorder Traversal");
t.inorder(root);
System.out.println();
System.out.println("Delete node 7");
t.delete(7);
System.out.println("Delete node 25");
t.delete(25);
System.out.println("inorder");
t.inorder(root);
System.out.println();
System.out.println("Delete node 6");
t.delete(6);
System.out.println("inorder");
t.inorder(root);
System.out.println();
}
}