-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathA009BST.java
More file actions
176 lines (158 loc) · 5.32 KB
/
A009BST.java
File metadata and controls
176 lines (158 loc) · 5.32 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
176
import java.util.LinkedList;
import java.util.Queue;
public class A009BST{
private BSTNode root = null ;
public BSTNode insert(BSTNode root , int data ){
if(root == null){
root = new BSTNode(data);
}else if(data < root.data){
root.left = insert(root.left, data);
}else{
root.right = insert(root.right, data);
}
return root;
}
public boolean search(BSTNode root , int data){
if(root == null){
return false;
}else if(root.data == data){
return true;
}else if(data < root.data ){
return search(root.left , data);
}else {
return search(root.right , data );
}
}
public int findMin(BSTNode root){
if(root == null){
return -1 ;
}else {
while(root.left != null){
root = root.left;
}
return root.data ;
}
}
public int findMax(BSTNode root){
if(root == null ){
return -1 ;
}else {
while (root.right != null){
root = root.right;
}
return root.data;
}
}
public int findHeight(BSTNode root){
if(root == null){
return -1 ;
}else{
return max(findHeight(root.left) , findHeight(root.right))+ 1 ;
}
}
public int max(int o1 , int o2 ){
return o1 >= o2 ? o1 : o2 ;
}
public void breathFirstTraversal(BSTNode root){
Queue<BSTNode> q = new LinkedList<>();
if(root == null){
return;
}else{
System.out.print(root.data+"-> ");
q.add(root.left );
q.add(root.right);
while(!q.isEmpty()){
breathFirstTraversal(q.remove());
}
}
}
public void preOrderTraversal(BSTNode root){
if(root == null) return;
else{
System.out.print(root.data+"->");
preOrderTraversal(root.left);
preOrderTraversal(root.right);
}
}
public void inOrderTraversal(BSTNode root){
if(root == null){return;}
else{
inOrderTraversal(root.left);
System.out.print(root.data+"->");
inOrderTraversal(root.right);
}
}
public void postOrderTraversal(BSTNode root){
if(root == null ){return;}
else{
postOrderTraversal(root.left);
postOrderTraversal(root.right);
System.out.print(root.data+"->");
}
}
public BSTNode delete(BSTNode root , int data){
if(root == null) return root;
if(data < root.data){
root.left = delete(root.left , data);
}else if(data > root.data ){
root.right = delete(root.right, data);
}else{
//found the element now delete it
//Case 1: no child , delete the node
if(root.left == null && root.right == null){
root = null;
//Case 2 : single child is present
}else if(root.left == null){
root = root.right;
}else if (root.right == null ){
root = root.left;
}else{
//Case 3: both the childern are present
//so find the min in right tree / find max in left tree
//copy it in the current node and call delete in the selected tree
int newMin = findMin(root.right);
root.data = newMin;
root.right = delete(root.right, newMin);
}
}
return root;
}
// java A009BST 1
public static void main(String[] args) {
A009BST bst = new A009BST();
bst.root = bst.insert(bst.root, 15);
bst.root = bst.insert(bst.root, 10);
bst.root = bst.insert(bst.root, 20);
bst.root = bst.insert(bst.root, 40);bst.root = bst.insert(bst.root, 30);bst.root = bst.insert(bst.root, 50);
int s = Integer.parseInt(args[0]);
System.out.println(s+" is Present? "+bst.search(bst.root, s));
System.out.println("Minimum in BST: "+bst.findMin(bst.root));
System.out.println("Maximum in BST: "+bst.findMax(bst.root));
System.out.println("Height of BST : "+bst.findHeight(bst.root));
System.out.println("the tree traversed in BREATH IS : ");
bst.breathFirstTraversal(bst.root);
System.out.println("\nthe tree traversed in PREORDER IS : ");
bst.preOrderTraversal(bst.root);
int deleteValue = 15 ;
bst.delete(bst.root, deleteValue);
System.out.println("\nthe tree traversed in INORDER IS : ");
bst.inOrderTraversal(bst.root);
System.out.println("\nthe tree traversed in POSTORDER IS : ");
bst.postOrderTraversal(bst.root);
}
private class BSTNode {
int data ;
BSTNode left ;
BSTNode right ;
BSTNode(){
this.data = 0;
this.left = null;
this.right = null;
}
BSTNode(int data){
this.data = data;
this.left = null;
this.right = null;
}
}
}