-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBST2-BST Class
164 lines (138 loc) · 3.78 KB
/
BST2-BST Class
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
Implement the BST class which includes following functions -
1. insert -
Given an element, insert that element in the BST at the correct position. If element is equal to the data of the node, insert it in the left subtree.
2. search
Given an element, find if that is present in BST or not. Return true or false.
3. delete -
Given an element, remove that element from the BST. If the element which is to be deleted has both children, replace that with the minimum element from right sub-tree.
4. printTree (recursive) -
Print the BST in in the following format -
For printing a node with data N, you need to follow the exact format -
N:L:x,R:y
where, N is data of any node present in the binary tree. x and y are the values of left and right child of node N. Print the children only if it is not null.
There is no space in between.
You need to print all nodes in the recursive format in different lines.
******************************Solution*********************************
class BSTDeleteReturn {
BinaryTreeNode<Integer> root;
boolean deleted;
public BSTDeleteReturn(BinaryTreeNode<Integer> root, boolean deleted)
{
this.root = root;
this.deleted = deleted;
}
}
public class BinarySearchTree {
private BinaryTreeNode<Integer> root;
private int size;
public void insert(int data) {
root= insertHelper(root,data);
size++;
}
private static BinaryTreeNode<Integer> insertHelper(BinaryTreeNode<Integer> root, int data)
{
if (root==null)
{
BinaryTreeNode<Integer> newRoot = new BinaryTreeNode<Integer>(data);
return newRoot;
}
if (data>root.data)
{
root.right= insertHelper(root.right,data);
}
else
{
root.left= insertHelper(root.left,data);
}
return root;
}
public void remove(int data) {
BSTDeleteReturn output = removeHelper(root, data);
root = output.root;
if (output.deleted)
{
size--;
}
}
private static BSTDeleteReturn removeHelper(BinaryTreeNode<Integer> root, int data)
{
if(root==null)
{
return new BSTDeleteReturn(null, false);
}
if(root.data<data)
{
BSTDeleteReturn ansRight= removeHelper(root.right, data);
root.right=ansRight.root;
ansRight.root=root;
return ansRight;
}
if(root.data>data)
{
BSTDeleteReturn ansLeft=removeHelper(root.left, data);
root.left=ansLeft.root;
ansLeft.root=root;
return ansLeft;
}
if(root.left==null && root.right==null)
return new BSTDeleteReturn(null, true);
if(root.left!=null && root.right==null)
return new BSTDeleteReturn(root.left, true);
if(root.left==null && root.right!=null )
return new BSTDeleteReturn(root.right, true);
int rightsmall=minimum(root.right);
root.data=rightsmall;
BSTDeleteReturn ansRight= removeHelper(root.right, rightsmall);
root.right=ansRight.root;
return new BSTDeleteReturn(root, true);
}
private static int minimum(BinaryTreeNode<Integer> root)
{
if(root==null)
{
return Integer.MAX_VALUE;
}
int leftMin=minimum(root.left);
int rightMin=minimum(root.right);
return Math.min(root.data, Math.min(leftMin, rightMin));
}
public void printTree() {
printHelper(root);
}
private static void printHelper(BinaryTreeNode<Integer> root)
{
if (root==null)
{
return;
}
System.out.print(root.data+":");
if (root.left!=null)
{
System.out.print("L:"+root.left.data+",");
}
if (root.right!=null)
{
System.out.print("R:"+root.right.data);
}
System.out.println();
printHelper(root.left);
printHelper(root.right);
}
public boolean search(int data) {
return searchHelper(root, data);
}
private static boolean searchHelper(BinaryTreeNode<Integer> root, int data)
{
if(root==null)
return false;
if(root.data==data)
return true;
if(data<root.data)
{
return searchHelper(root.left, data);
}
else{
return searchHelper(root.right, data);
}
}
}