8000 insert/delete · anmolpant/Coding-Ninjas-Java@acb590b · GitHub
[go: up one dir, main page]

Skip to content

Commit acb590b

Browse files
committed
insert/delete
1 parent 17d4271 commit acb590b

File tree

8 files changed

+513
-0
lines changed

8 files changed

+513
-0
lines changed
Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
2+
public class BinarySearchTree {
3+
4+
private BinaryTreeNode<Integer> root;
5+
6+
private BinaryTreeNode<Integer> insertData(int data, BinaryTreeNode<Integer> root) {
7+
if (root == null) {
8+
BinaryTreeNode<Integer> newNode = new BinaryTreeNode<Integer>(data);
9+
return newNode;
10+
}
11+
if (root.data > data) {
12+
root.left = insertData(data, root.left);
13+
} else {
14+
root.right = insertData(data, root.right);
15+
}
16+
return root;
17+
}
18+
19+
public void insertData(int data) {
20+
root = insertData(data, root);
21+
}
22+
23+
public void deleteData(int data) {
24+
root = deleteData(data, root);
25+
}
26+
27+
private BinaryTreeNode<Integer> deleteData(int data, BinaryTreeNode<Integer> root) {
28+
if (root == null) {
29+
return null;
30+
}
31+
if (data < root.data) {
32+
root.left = deleteData(data, root.left);
33+
return root;
34+
} else if (data > root.data) {
35+
root.right = deleteData(data, root.right);
36+
return root;
37+
} else {
38+
if (root.left == null && root.right == null) {
39+
return null;
40+
} else if (root.left == null) {
41+
return root.right;
42+
} else if (root.right == null) {
43+
return root.left;
44+
} else {
45+
BinaryTreeNode<Integer> minNode = root.right;
46+
while (minNode.left != null) {
47+
minNode = minNode.left;
48+
}
49+
root.data = minNode.data;
50+
root.right = deleteData(minNode.data, root.right);
51+
return root;
52+
}
53+
}
54+
55+
}
56+
57+
private void printTree(BinaryTreeNode<Integer> root) {
58+
if (root == null) {
59+
return;
60+
}
61+
String toBePrinted = root.data + "";
62+
if (root.left != null) {
63+
toBePrinted += "L:" + root.left.data + ",";
64+
}
65+
66+
if (root.right != null) {
67+
toBePrinted += "R:" + root.right.data;
68+
}
69+
System.out.println(toBePrinted);
70+
printTree(root.left);
71+
printTree(root.right);
72+
}
73+
74+
public void printTree() {
75+
printTree(root);
76+
}
77+
78+
private boolean hasDataHelper(int data, BinaryTreeNode<Integer> root) {
79+
if (root == null) {
80+
return false;
81+
}
82+
83+
if (root.data == data) {
84+
return true;
85+
} else if (data > root.data) {
86+
// call right
87+
return hasDataHelper(data, root.right);
88+
} else {
89+
// call left
90+
return hasDataHelper(data, root.left);
91+
}
92+
}
93+
94+
public boolean hasData(int data) {
95+
return hasDataHelper(data, root);
96+
}
97+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
2+
public class BinaryTreeNode<T> {
3+
public BinaryTreeNode(T data) {
4+
this.data = data;
5+
}
6+
public T data;
7+
public BinaryTreeNode<T> left;
8+
public BinaryTreeNode<T> right;
9+
}

0 commit comments

Comments
 (0)
0