-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBinarySearchTree.java
More file actions
148 lines (131 loc) · 4.68 KB
/
BinarySearchTree.java
File metadata and controls
148 lines (131 loc) · 4.68 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
public class BinarySearchTree {
private BSTNode root;
// Constructor: Initializes an empty binary search tree
public BinarySearchTree() {
this.root = null;
}
// Returns the root node of the binary search tree
public BSTNode getRoot() {
return root;
}
// Recursively retrieves a node with the specified key from the tree
public BSTNode get(BSTNode r, Key k) {
if (r == null || r.getRecord() == null) {
return null;
}
int compareResult = k.compareTo(r.getRecord().getKey());
if (compareResult < 0) {
return get(r.getLeftChild(), k);
} else if (compareResult > 0) {
return get(r.getRightChild(), k);
} else {
return r;
}
}
// Inserts a new record into the tree, throwing an exception if the key already exists
public void insert(BSTNode r, Record d) throws DictionaryException {
if (root == null) {
root = new BSTNode(d);
return;
}
int compareResult = d.getKey().compareTo(r.getRecord().getKey());
if (compareResult < 0) {
if (r.getLeftChild() == null) {
r.setLeftChild(new BSTNode(d));
} else {
insert(r.getLeftChild(), d);
}
} else if (compareResult > 0) {
if (r.getRightChild() == null) {
r.setRightChild(new BSTNode(d));
} else {
insert(r.getRightChild(), d);
}
} else {
throw new DictionaryException("Record with the same key already exists");
}
}
// Removes a node with the specified key from the tree
public void remove(BSTNode root, Key k) throws DictionaryException {
this.root = removeRecursive(this.root, k);
}
// Helper method for recursively removing a node from the tree
private BSTNode removeRecursive(BSTNode node, Key k) throws DictionaryException {
if (node == null) {
throw new DictionaryException("Key not found in the dictionary");
}
int compareResult = k.compareTo(node.getRecord().getKey());
if (compareResult < 0) {
node.setLeftChild(removeRecursive(node.getLeftChild(), k));
} else if (compareResult > 0) {
node.setRightChild(removeRecursive(node.getRightChild(), k));
} else {
// Case 1: No child or one child
if (node.getLeftChild() == null) {
return node.getRightChild();
} else if (node.getRightChild() == null) {
return node.getLeftChild();
}
// Case 2: Two children
// Find the smallest node in the right subtree
BSTNode smallestNode = smallest(node.getRightChild());
node.setRecord(smallestNode.getRecord());
node.setRightChild(removeRecursive(node.getRightChild(), smallestNode.getRecord().getKey()));
}
return node;
}
// Finds the successor (next higher key) of a given key in the tree
public BSTNode successor(BSTNode r, Key k) {
BSTNode current = get(r, k);
if (current == null) {
return null;
}
if (current.getRightChild() != null) {
return smallest(current.getRightChild());
} else {
BSTNode parent = current.getParent();
while (parent != null && current == parent.getRightChild()) {
current = parent;
parent = parent.getParent();
}
return parent;
}
}
// Finds the predecessor (next lower key) of a given key in the tree
public BSTNode predecessor(BSTNode r, Key k) {
BSTNode current = get(r, k);
if (current == null) {
return null;
}
if (current.getLeftChild() != null) {
return largest(current.getLeftChild());
} else {
BSTNode parent = current.getParent();
while (parent != null && current == parent.getLeftChild()) {
current = parent;
parent = parent.getParent();
}
return parent;
}
}
// Finds the smallest key node in a subtree
public BSTNode smallest(BSTNode r) {
if (r == null) {
return null;
}
while (r.getLeftChild() != null) {
r = r.getLeftChild();
}
return r;
}
// Finds the largest key node in a subtree
public BSTNode largest(BSTNode r) {
if (r == null) {
return null;
}
while (r.getRightChild() != null) {
r = r.getRightChild();
}
return r;
}
}