-
Notifications
You must be signed in to change notification settings - Fork 2
/
BinaryTree.java
184 lines (155 loc) · 4.89 KB
/
BinaryTree.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
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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
import java.util.Queue;
import java.util.LinkedList;
public class BinaryTree {
Node root;
class Node {
int value;
Node right, left;
public Node(int value) {
this.value = value;
}
}
public void createBinaryTree() {
root = null;
}
public void insert(int value) {
Node newNode = new Node(value);
if (root == null) {
root = newNode;
} else {
Queue<Node> queue = new LinkedList<Node>();
queue.add(root);
while (!queue.isEmpty()) {
Node currNode = queue.remove();
if (currNode.left == null) {
currNode.left = newNode;
break;
} else if (currNode.right == null) {
currNode.right = newNode;
break;
} else {
queue.add(currNode.left);
queue.add(currNode.right);
}
}
}
}
public boolean search(int value) {
if (root == null)
return false;
Queue<Node> queue = new LinkedList<Node>();
queue.add(root);
while (!queue.isEmpty()) {
Node currNode = queue.remove();
if (currNode.value == value)
return true;
else {
if (currNode.left != null)
queue.add(currNode.left);
else if (currNode.right != null)
queue.add(currNode.right);
}
}
return false;
}
public Node getDeepestNode() {
Node currNode = root;
Queue<Node> queue = new LinkedList<Node>();
queue.add(root);
while (!queue.isEmpty()) {
currNode = queue.remove();
if (currNode.left != null)
queue.add(currNode.left);
if (currNode.right != null)
queue.add(currNode.right);
}
return currNode;
}
public void deleteDeepestNode() {
if (root == null) {
System.out.println("Tree is empty!");
return;
} else {
Node currNode, prevNode;
currNode = prevNode = null;
Queue<Node> queue = new LinkedList<Node>();
queue.add(root);
while (!queue.isEmpty()) {
prevNode = currNode;
currNode = queue.remove();
if(currNode.left == null) {
prevNode.right = null;
return;
}
else if(currNode.left == null) {
currNode.left = null;
return;
}
queue.add(currNode.left);
queue.add(currNode.right);
}
}
}
public void deleteNodeValue(int value) {
if (root == null) {
System.out.println("Tree is empty");
return;
} else {
Queue<Node> queue = new LinkedList<Node>();
queue.add(root);
while (!queue.isEmpty()) {
Node currNode = queue.remove();
if (currNode.value == value) {
System.out.println(currNode.value + " Deleted");
currNode.value = getDeepestNode().value;
deleteDeepestNode();
break;
} else {
if (currNode.left != null) {
queue.add(currNode.left);
}
if (currNode.right != null) {
queue.add(currNode.right);
}
}
}
}
}
public void inorder(Node root) {
if (root == null)
return;
inorder(root.left);
System.out.print(root.value + " ");
inorder(root.right);
}
public void deleteBinaryTree() {
root = null;
}
public static void main(String[] args) {
BinaryTree bTree = new BinaryTree();
bTree.createBinaryTree();
bTree.insert(25);
bTree.insert(26);
bTree.insert(27);
bTree.insert(28);
bTree.insert(29);
// System.out.println(bTree.root.value);
System.out.print("Inorder : ");
bTree.inorder(bTree.root);
System.out.println();
int value = 26;
boolean s_value = bTree.search(value);
if (s_value == true)
System.out.println(value + " Found! in the Tree");
else
System.out.println(value + " Not found in the Tree");
bTree.deleteNodeValue(25);
System.out.print("Inorder Traversing : ");
bTree.inorder(bTree.root);
System.out.println();
// System.out.println(bTree.root.value);
bTree.deleteBinaryTree();
bTree.deleteNodeValue(26);
System.out.println("Tree was Deleted!");
}
}