-
Notifications
You must be signed in to change notification settings - Fork 0
/
BinarySearchTree.java
89 lines (74 loc) · 1.77 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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
package ds;
public class BinarySearchTree {
TNode root;
BinarySearchTree(){
root=null;
}
TNode insert(TNode root, int key) {
if(root==null) {
root=new TNode(key);
return root;
}
if(key<root.key)
root.left= insert(root.left,key);
else if(key>root.key)
root.right= insert(root.right, key);
return root;
}
void inorder(TNode root) {
if(root!=null) {
inorder(root.left);
System.out.println(root.key);
inorder(root.right);
}
}
public static TNode search(TNode root, int key) {
if(root==null||root.key==key)
return root;
if(root.key>key)
return search(root.left,key);
return search(root.right, key);
}
TNode delete(TNode root, int key) {
if(root==null)
return root;
if(key<root.key)
root.left=delete(root.left, key);
else if(key>root.key)
root.right=delete(root.right, key);
else {
if(root.left==null)
return root.right;
else if(root.right==null)
return root.left;
root.key=minValue(root.right);
root.right= delete(root.right, root.key);
}
return root;
}
int minValue(TNode root) {
int minValue=root.key;
while(root.left!=null)
{
minValue= root.left.key;
root=root.left;
}
return minValue;
}
public static void main(String []args) {
BinarySearchTree tree= new BinarySearchTree();
tree.root= tree.insert(tree.root, 1);
tree.root= tree.insert(tree.root, 6);
tree.root= tree.insert(tree.root, 3);
tree.root= tree.insert(tree.root, 9);
tree.root= tree.insert(tree.root, 2);
tree.root= tree.insert(tree.root, 7);
TNode present=search(tree.root, 6);
if(present!=null)
System.out.println("The number is present in the tree");
else
System.out.println("Number not present");
tree.delete(tree.root, 6);
tree.inorder(tree.root);
}
}