-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBinarySearchTrees.java
129 lines (114 loc) · 2.46 KB
/
BinarySearchTrees.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
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
//Author : Hyder Nabi
//TODO : Implementation of BST
class Node
{
int key;
Node left;
Node right;
}
class BST
{
public Node root;
public BST() {
root = null;
}
public BST(int arr[]){
for(int i = 0;i<arr.length;i++)
{
this.root = insert(this.root,arr[i]);
}
}
public void create(int arr[]){
for(int i = 0;i<arr.length;i++)
{
this.root = insert(this.root,arr[i]);
}
}
public Node insert(Node root,int elem)
{
if(root == null)
{
root = new Node();
root.key = elem;
root.left = null;
root.right = null;
}else {
if(elem>=root.key) {
root.right = insert(root.right,elem);
}else {
root.left = insert(root.left,elem);
}
}
return root;
}
public Node delete(Node root, int key) {
if(root == null){
System.out.println("Node Not found");
return null;
}else if(key > root.key){
root.right = delete(root.right,key);
return root;
}else if(key<root.key) {
root.left = delete(root.left,key);
return root;
}else {
//when key is equal to the root.key
if(root.left != null && root.right != null)
{
Node largestNode = findLargest(root.left);
root.key = largestNode.key;
root.left = delete(root.left,largestNode.key);
return root;
}else if(root.left == null && root.right == null){
return null;
}else if(root.left != null){
Node temp = root.left;
root = null;
return temp;
}else {
Node temp = root.right;
root = null;
return temp;
}
}
}
public Node findLargest(Node root)
{
if(root == null || root.right == null){
return root;
}else {
return findLargest(root.right);
}
}
public void inOrder(Node root) {
if(root.left != null)
inOrder(root.left);
System.out.println(root.key);
if(root.right != null)
inOrder(root.right);
}
public void postOrder(Node root)
{
if(root == null)
{
return;
}else {
postOrder(root.left);
postOrder(root.right);
System.out.print(root.key+" ");
}
}
}
public class BinarySearchTrees {
public static void main(String[] args) {
// int arr[] = new int[] {45, 39, 56, 12, 34, 78, 32, 10, 89, 54, 67, 81};
int arr[] = new int[] {5,8,1,89,23,34,45,98,102,4,3};
BST bst = new BST();
bst.create(arr);
bst.inOrder(bst.root);
System.out.println();
bst.delete(bst.root, 89);
bst.inOrder(bst.root);
bst.postOrder(bst.root);
}
}