-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBinarySearchTree1.java
More file actions
104 lines (87 loc) · 3.21 KB
/
BinarySearchTree1.java
File metadata and controls
104 lines (87 loc) · 3.21 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
/*
<COSC 2007>
<Rajin Santos Gajadhar>
<239479650>
<Assignment 2>
*/
public class BinarySearchTree1<T extends Comparable<T>> {
private TreeNode<T> root;
public BinarySearchTree1() {
root = null;
} // end default constructor
public BinarySearchTree1(T rootItem) {
root = new TreeNode<T>(rootItem, null, null);
} // end constructor
public void insert(T newItem) {
root = insertItem(root, newItem);
} // end insert
public T retrieve(T searchKey) {
return retrieveItem(root, searchKey);
} // end retrieve
public void delete(T searchKey) {
root = deleteItem(root, searchKey);
} // end delete
// Method to get the root of the tree
public TreeNode<T> getRoot() {
return root;
}
// Recursive helper methods for insert, retrieve, and delete operations
private TreeNode<T> insertItem(TreeNode<T> rootNode, T newItem) {
if (rootNode == null) {
rootNode = new TreeNode<T>(newItem);
return rootNode;
}
int comparison = newItem.compareTo(rootNode.getItem());
if (comparison < 0) {
rootNode.setLeftChild(insertItem(rootNode.getLeftChild(), newItem));
} else if (comparison > 0) {
rootNode.setRightChild(insertItem(rootNode.getRightChild(), newItem));
}
return rootNode;
}
private T retrieveItem(TreeNode<T> rootNode, T searchKey) {
if (rootNode == null) {
return null;
}
int comparison = searchKey.compareTo(rootNode.getItem());
if (comparison == 0) {
return rootNode.getItem();
} else if (comparison < 0) {
return retrieveItem(rootNode.getLeftChild(), searchKey);
} else {
return retrieveItem(rootNode.getRightChild(), searchKey);
}
}
private TreeNode<T> deleteItem(TreeNode<T> rootNode, T searchKey) {
if (rootNode == null) {
return null;
}
int comparison = searchKey.compareTo(rootNode.getItem());
if (comparison < 0) {
rootNode.setLeftChild(deleteItem(rootNode.getLeftChild(), searchKey));
} else if (comparison > 0) {
rootNode.setRightChild(deleteItem(rootNode.getRightChild(), searchKey));
} else {
// Node with one or no child
if (rootNode.getLeftChild() == null) {
return rootNode.getRightChild();
} else if (rootNode.getRightChild() == null) {
return rootNode.getLeftChild();
}
// Node with two children
T minValue = minValue(rootNode.getRightChild());
rootNode.setItem(minValue);
rootNode.setRightChild(deleteItem(rootNode.getRightChild(), minValue));
}
return rootNode;
}
// Helper method to find the minimum value in a subtree
private T minValue(TreeNode<T> rootNode) {
T minValue = rootNode.getItem();
while (rootNode.getLeftChild() != null) {
rootNode = rootNode.getLeftChild();
minValue = rootNode.getItem();
}
return minValue;
}
}