forked from kbeathanabhotla/DSAAJ2-Answers
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Chap04.question.16.BstLazyDeletion.java
132 lines (117 loc) · 3.89 KB
/
Chap04.question.16.BstLazyDeletion.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
//Chap04.question.16.BstLazyDeletion.java
public class BstLazyDeletion<T extends Comparable<? super T>> {
private static final int MIN_SIZE = 100;
private BinaryTreeNode<T> root;
//size is # of nodes not deleted
private int size, deleteCount;
public void insert(T t) {
root = insert(t, root);
}
private BinaryTreeNode<T> insert(T t, BinaryTreeNode<T> node) {
if (node == null) {
size++;
return new BinaryTreeNode<>(t, null, null);
}
int compareResult = t.compareTo(node.data);
if (compareResult < 0)
node.left = insert(t, node.left);
else if (compareResult > 0)
node.right = insert(t, node.right);
else if (node.deleted) {
node.deleted = false;
size++;
deleteCount--;
}
return node;
}
public void remove(T t) {
root = remove(t, root);
if (size > MIN_SIZE && size < deleteCount) {
//trigger real delete
root = removeDeletedNodes(root);
}
}
private BinaryTreeNode<T> removeDeletedNodes(BinaryTreeNode<T> node) {
if (node == null) return null;
node.left = removeDeletedNodes(node.left);
node.right = removeDeletedNodes(node.right);
if (node.deleted) {
if (node.left != null && node.right != null) {
BinaryTreeNode<T> rightMinNode = node.right;
BinaryTreeNode<T> rightMinNodeParent = node;
while (rightMinNode.left != null) {
rightMinNodeParent = rightMinNode;
rightMinNode = rightMinNode.left;
}
node.data = rightMinNode.data;
node.deleted = false;
if (rightMinNodeParent == node)
rightMinNodeParent.right = null;
else
rightMinNodeParent.left = rightMinNode.right;
} else
node = node.left != null ? node.left : node.right;
}
return node;
}
private BinaryTreeNode<T> remove(T t, BinaryTreeNode<T> node) {
if (node == null) return null;
int compareResult = t.compareTo(node.data);
if (compareResult < 0)
node.left = remove(t, node.left);
else if (compareResult > 0)
node.right = remove(t, node.right);
else if (!node.deleted) {
node.deleted = true;
size--;
deleteCount++;
}
return node;
}
public T findMin() {
return findMin(root);
}
private T findMin(BinaryTreeNode<T> node) {
if (node == null) return null;
T leftMin = findMin(node.left);
if (leftMin != null)
return leftMin;
if (!node.deleted)
return node.data;
return findMin(node.right);
}
public T findMax() {
return findMax(root);
}
private T findMax(BinaryTreeNode<T> node) {
if (node == null) return null;
T rightMax = findMax(node.right);
if (rightMax != null) return rightMax;
if (!node.deleted) return node.data;
return findMax(node.left);
}
public boolean contains(T t) {
return contains(t, root);
}
private boolean contains(T t, BinaryTreeNode<T> node) {
if (node == null) return false;
int compareResult = t.compareTo(node.data);
if (compareResult < 0)
return contains(t, node.left);
else if (compareResult > 0)
return contains(t, node.right);
else
return !node.deleted;
}
private static class BinaryTreeNode<T> {
T data;
BinaryTreeNode<T> left, right;
boolean deleted;
BinaryTreeNode(T t, BinaryTreeNode<T> l, BinaryTreeNode<T> r) {
data = t;
left = l;
right = r;
deleted = false;
}
}
}