-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBinary search tree using recursion
More file actions
150 lines (108 loc) · 3.05 KB
/
Binary search tree using recursion
File metadata and controls
150 lines (108 loc) · 3.05 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
public class main {
public static void main(String[] args) {
binarySearchTree tree=new binarySearchTree();
tree.insert(5);
tree.insert(4);
tree.insert(6);
tree.insert(2);
tree.insert(8);
tree.insert(9);
tree.insert(7);
tree.insert(1);
tree.insert(3);
tree.remove(5);
tree.display();
// System.out.println(tree.search(1));
}
public class binarySearchTree {
Node root;
public void insert(int data) {
root = insertHelper(root, new Node(data));
}
private Node insertHelper(Node root, Node node) {
int data = node.data;
if (root == null) {
root = node;
return root;
} else if (data < root.data) {
root.left = insertHelper(root.left, node);
} else {
root.right = insertHelper(root.right, node);
}
return root;
}
public void display() {
displayHelper(root);
}
private void displayHelper(Node root) {
if (root != null) {
displayHelper(root.left);
System.out.println(root.data);
displayHelper(root.right);
}
}
public boolean search(int data) {
return searchHelper(root, data);
}
private boolean searchHelper(Node root, int data) {
if (root == null) {
return false;
} else if (data == root.data) {
return true;
} else if (root.data > data) {
return searchHelper(root.left, data);
} else {
return searchHelper(root.right, data);
}
}
public void remove(int data) {
if (search(data)) {
removeHelper(root, data);
} else {
System.out.println(data + " Could not found in the data");
}
}
private Node removeHelper(Node root, int data) {
if (root == null) {
return root;
} else if (data < root.data) {
root.left = removeHelper(root.left, data);
} else if (data > root.data) {
root.right = removeHelper(root.right, data);
} else { // Node found
if (root.left == null && root.right == null) {
root = null;
} else if (root.right != null) {
root.data = successor(root);
root.right = removeHelper(root.right, root.data);
} else {
root.data = predecessor(root);
root.left = removeHelper(root.left, root.data);
}
}
return root;
}
private int successor(Node root) {
root = root.right;
while (root.left != null) {
root = root.left;
}
return root.data;
}
private int predecessor(Node root) {
root = root.left;
while (root.right != null) {
root = root.right;
}
return root.data;
}
}
public class Node {
int data;
Node left;
Node right;
Node(int data){
this.data=data;
}
}
}