forked from avinashbest/java-coding-ninjas
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBinarySearchTree.java
More file actions
145 lines (119 loc) · 4.37 KB
/
Copy pathBinarySearchTree.java
File metadata and controls
145 lines (119 loc) · 4.37 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
package binarySearchTree2;
/*
Implement the BST class which includes following functions -
1. search - Given an element, find if that is present in BST or not. Return true or false.
2. insert - Given an element, insert that element in the BST at the correct position. Assume unique elements will be given.
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 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.
* */
class BinarySearchTreeDeleteReturn {
TreeNode<Integer> root;
boolean deleted;
public BinarySearchTreeDeleteReturn(TreeNode<Integer> root, boolean deleted) {
this.root = root;
this.deleted = deleted;
}
}
public class BinarySearchTree {
private TreeNode<Integer> root;
private int size;
private static boolean isPresentHelper(TreeNode<Integer> node, int x) {
if (node == null) return false;
if (node.data == x) return true;
if (x < node.data) return isPresentHelper(node.left, x);
else return isPresentHelper(node.right, x);
}
/*
* O(height)
* */
public boolean isPresent(int data) {
return isPresentHelper(root, data);
}
private static TreeNode<Integer> insertHelper(TreeNode<Integer> node, int data) {
if (node == null) return new TreeNode<>(data);
if (data >= node.data) {
node.right = insertHelper(node.right, data);
} else {
node.left = insertHelper(node.left, data);
}
return node;
}
/*
* O(height)
* */
public void insert(int data) {
root = insertHelper(root, data);
size++;
}
private static BinarySearchTreeDeleteReturn deleteDataHelper(
TreeNode<Integer> node,
int x
) {
if (node == null) return new BinarySearchTreeDeleteReturn(null, false);
if (node.data < x) {
var right = deleteDataHelper(node.right, x);
node.right = right.root;
right.root = node;
return right;
}
if (node.data > x) {
var left = deleteDataHelper(node.left, x);
node.left = left.root;
left.root = node;
return left;
}
// 0 Children
if (node.left == null && node.right == null) return new BinarySearchTreeDeleteReturn(null, true);
// Only left Children
if (node.left != null && node.right == null) return new BinarySearchTreeDeleteReturn(node.left, true);
// Only right Children
if (node.left == null) return new BinarySearchTreeDeleteReturn(node.right, true);
// Both the Children are Present
int rightMinimum = minimum(node.right);
node.data = rightMinimum;
var outputRight = deleteDataHelper(node.right, rightMinimum);
node.right = outputRight.root;
return new BinarySearchTreeDeleteReturn(node, true);
}
private static int minimum(TreeNode<Integer> root) {
if (root == null) return Integer.MAX_VALUE;
int left = minimum(root.left);
int right = minimum(root.right);
return Math.min(root.data, Math.min(left, right));
}
/*
* O(2 * height)
* */
public boolean deleteData(int data) {
var output = deleteDataHelper(root, data);
root = output.root;
if (output.deleted) size--;
return output.deleted;
}
/*
* O(1)
* */
public int size() {
return size;
}
private static void printTreeHelper(TreeNode<Integer> node) {
if (node == null) return;
System.out.print(node.data + " : ");
if (node.left != null) System.out.print("Left -> " + node.left.data + ", ");
if (node.right != null) System.out.print("Right -> " + node.right.data);
System.out.println();
printTreeHelper(node.left);
printTreeHelper(node.right);
}
/*
* O(n)
* */
public void printTree() {
printTreeHelper(root);
}
}