-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBinaryTree.java
More file actions
185 lines (167 loc) · 3.43 KB
/
Copy pathBinaryTree.java
File metadata and controls
185 lines (167 loc) · 3.43 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
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
185
package com.learn.tree;
public class BinaryTree {
public Tree root = null;
public Tree getRoot()
{
return root;
}
public static void main(String[] args) {
// TODO Auto-generated method stub
BinaryTree bt = new BinaryTree();
bt.insert(100);
bt.insert(23);
bt.insert(12);
bt.insert(34);
bt.insert(15);
bt.insert(3);
bt.insert(8);
bt.insert(56);
bt.insert(70);
bt.inorder(bt.getRoot());
bt.findandDelete(8);
System.out.println("New List : ");
bt.inorder(bt.getRoot());
bt.findandDelete(100);
System.out.println("New List : ");
bt.inorder(bt.getRoot());
bt.findandDelete(100);
System.out.println("New List : ");
bt.inorder(bt.getRoot());
}
public Tree createTree()
{
BinaryTree bt = new BinaryTree();
bt.insert(100);
bt.insert(23);
bt.insert(12);
bt.insert(34);
bt.insert(15);
bt.insert(3);
bt.insert(8);
bt.insert(56);
bt.insert(70);
return this.root;
}
public void insert(int x) {
Tree newnode = new Tree(x);
newnode.left = null;
newnode.right = null;
if (this.root == null) {
this.root = newnode;
} else {
Tree temp = this.root;
Tree parent = temp;
while (temp != null) {
parent = temp;
if (x < temp.value)
temp = temp.left;
else
temp = temp.right;
}
if (x < parent.value) {
parent.left = newnode;
} else {
parent.right = newnode;
}
}
}
public void findandDelete(int x) {
Tree temp = root;
Tree parent = null;
if (x == root.value)
delete(root, null);
else {
while (temp != null && temp.value != x) {
parent = temp;
if (x > temp.value)
temp = temp.right;
else if (x < temp.value)
temp = temp.left;
}
if (temp == null) {
System.out.println("Item not found");
} else {
delete(temp, parent);
}
}
}
public Tree findSucessor(Tree node) {
Tree temp = node;
temp = temp.right;
while (temp.left != null) {
temp = temp.left;
}
return temp;
}
public void delete(Tree node, Tree parent) {
// Case 1 : node is leaf node
if (node.right == null && node.left == null) {
if (parent == null) {
root = null;
} else {
if (parent.right == node) {
parent.right = null;
} else {
parent.left = null;
}
}
} else
// Case 2 : node has left child
if (node.right == null) {
if (parent == null) {
root = root.left;
} else {
if (parent.right == node) {
parent.right = node.left;
} else {
parent.left = node.left;
}
}
} else
// Case 4 : node has right subchild
if (node.left == null) {
if (parent == null) {
root = root.right;
} else {
if (parent.right == node) {
parent.right = node.right;
} else {
parent.left = node.right;
}
}
}
// Case 5 : node has right and lef subchild
else {
Tree successor = findSucessor(node);
Tree successorParent = findParent(node);
node.value = successor.value;
delete(successor, successorParent);
}
}
public Tree findParent(Tree node) {
Tree temp = root;
Tree parent = null;
if (temp.value == node.value) {
return null;
} else {
while (node.value != temp.value) {
parent = temp;
if (node.value > temp.value) {
temp = temp.right;
} else if (node.value < temp.value) {
temp = temp.left;
}
}
return parent;
}
}
public void inorder(Tree node) {
if (node == null) {
return;
} else {
inorder(node.left);
System.out.println(node.value);
inorder(node.right);
}
}
}