-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathBSTExample.java
More file actions
145 lines (129 loc) · 3.92 KB
/
Copy pathBSTExample.java
File metadata and controls
145 lines (129 loc) · 3.92 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
class BSTNode<T>{
T data;
BSTNode left;
BSTNode right;
BSTNode(T data){
this.data = data;
}
}
class BSTOperations{
BSTNode<Integer> root; // root is null
void takeTreeValues(){
root = insert(root, 10);
BSTNode<Integer> t = insert(root, 20);
t = insert(root, 5);
t = insert(root, 7);
print(root);
BSTNode<Integer> node = search(root, 45);
System.out.println( node==null?"No Data Found":"Data Found "+node.data);
System.out.println("Min Value "+minValue(root));
System.out.println("Max Value "+maxValue(root));
}
BSTNode<Integer> insert(BSTNode<Integer> currentNode, int data){
// Check Root is Not Present
if(currentNode == null){
currentNode = new BSTNode<Integer>(data);
return currentNode;
}
// Root is Present
if(data<currentNode.data){
currentNode.left = insert(currentNode.left, data);
}
else if(data>currentNode.data){
currentNode.right = insert(currentNode.right, data);
}
return currentNode;
}
void print(BSTNode<Integer> currentNode){
if(currentNode!=null){
print(currentNode.left);
System.out.println(currentNode.data);
print(currentNode.right);
}
}
BSTNode<Integer> search(BSTNode<Integer> currentNode, int dataToSearch){
if(currentNode == null || currentNode.data == dataToSearch){
return currentNode;
}
if(currentNode.data>dataToSearch){
return search(currentNode.left, dataToSearch);
}
else{
return search(currentNode.right, dataToSearch);
}
}
int minValue(BSTNode<Integer> currentNode){
if(currentNode==null){
return Integer.MIN_VALUE;
}
int min = currentNode.data;
while(currentNode.left!=null){
min = (Integer) currentNode.left.data;
currentNode = currentNode.left;
}
return min;
}
int maxValue(BSTNode<Integer> currentNode){
if(currentNode==null){
return Integer.MAX_VALUE;
}
int max = currentNode.data;
while(currentNode.right!=null){
max = (Integer) currentNode.right.data;
currentNode = currentNode.right;
}
return max;
}
void remove(BSTNode<Integer> currentNode, BSTNode<Integer> parent,boolean isLeft, int data){
if(data>currentNode.data){
// lookup in right
remove(currentNode.right, currentNode, false, data);
}
else if(data<currentNode.data){
remove(currentNode.left, currentNode, true, data);
}
else{
// Data Found
// Case 1 - Leaf Node (No Left Child , No Right Child)
if(currentNode.left==null && currentNode.right==null){
if(isLeft){
parent.left =null;
}
else {
parent.right = null;
}
}
// case -2 currentNode right is not null , but left is null
if(currentNode.left==null && currentNode.right!=null){
if(isLeft){
parent.left = currentNode.right;
}
else{
parent.right = currentNode.right;
}
}
// case -3 currentNode left is not null , but right is null
if(currentNode.left!=null && currentNode.right==null){
if(isLeft){
parent.left = currentNode.left;
}
else{
parent.right = currentNode.left;
}
}
// case -4 currentNode left is not null and right is not null
if(currentNode.left!=null && currentNode.right!=null){
// Left Max
int max = maxValue(currentNode.left);
currentNode.data = max;
remove(currentNode.left, currentNode, true, max);
}
}
}
}
public class BSTExample {
public static void main(String[] args) {
BSTOperations opr = new BSTOperations();
opr.takeTreeValues();
}
}