-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBinarySearchTree.java
More file actions
151 lines (140 loc) · 3.14 KB
/
BinarySearchTree.java
File metadata and controls
151 lines (140 loc) · 3.14 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
// AUTHOR: Soel Micheletti
// Class for each node of the binary search tree
class Node {
Node left;
Node right;
Node parent;
int value;
public Node(int v) {
value = v;
}
}
// Binary tree with methods to add, search and delete nodes. Moreover the utility method print is useful to see the current tree
class BinaryTree {
Node root;
public BinaryTree() {
}
public void add(int x) {
if (root == null)
root = new Node(x);
else {
Node current = root;
while (current != null) {
if (x <= current.value && current.left != null)
current = current.left;
else if (x <= current.value) {
Node n = new Node(x);
n.parent = current;
current.left = n;
current = null;
} else if (x > current.value && current.right != null)
current = current.right;
else {
Node n = new Node(x);
n.parent = current;
current.right = n;
current = null;
}
}
}
}
public Node search(Node n) {
if (root == null)
return null;
else {
Node current = root;
while (current != n && current != null) {
if (n.value == current.value)
return current;
else if (n.value < current.value)
current = current.left;
else
current = current.right;
}
return null;
}
}
public void delete(int x) {
if (root == null || search(new Node(x)) == null)
System.out.println("Not found");
else if (root.value == x) {
if (root.left == null)
root = root.right;
else if (root.right == null)
root = root.left;
else {
if (root.left.right == null) {
root.left.right = root.right;
root = root.left;
} else {
Node t = root.left.right;
while (t != null)
t = t.right;
t.parent.right = null;
t.parent = null;
t.left = root.left;
t.right = root.right;
}
}
} else {
Node c = search(new Node(x));
if (c.parent.left.equals(c)) {
if (c.left == null && c.right == null)
c.parent.left = null;
else if (c.left == null) {
c.parent.left = c.right;
c.right.parent = c.parent;
} else if (c.right == null) {
c.parent.left = c.left;
c.left.parent = c.parent;
} else {
Node t = c.left;
while (t.right != null)
t = t.right;
if (t.equals(c.left)) {
t.parent = c.parent;
c.parent.left = t;
} else {
t.parent.right = null;
t.parent = c.parent;
c.parent.left = t;
t.left = c.left;
t.right = c.right;
}
}
} else {
if (c.left == null && c.right == null)
c.parent.right = null;
else if (c.left == null) {
c.parent.right = c.right;
c.right.parent = c.parent;
} else if (c.right == null) {
c.parent.right = c.left;
c.left.parent = c.parent;
} else {
Node t = c.left;
while (t.right != null)
t = t.right;
if (t.equals(c.left)) {
t.parent = c.parent;
c.parent.right = t;
} else {
t.parent.right = null;
t.parent = c.parent;
c.parent.right = t;
t.left = c.left;
t.right = c.right;
}
}
}
}
}
public void print(Node n) {
if (n == null) {
} else {
System.out.println(n.value);
print(n.left);
print(n.right);
}
}
}