Repository navigation
Expand file tree
/
Copy pathBinarySearchTree.java
More file actions
144 lines (125 loc) · 4.05 KB
/
Copy pathBinarySearchTree.java
File metadata and controls
144 lines (125 loc) · 4.05 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
/*
* Uses the TreeNode class.
*/
public class BinarySearchTree {
public static void main(String[] args) {
TreeNode tree = new TreeNode(45);
insertIntoTree(tree, 56);
insertIntoTree(tree, 67);
insertIntoTree(tree, 24);
insertIntoTree(tree, 61);
insertIntoTree(tree, 52);
System.out.print("Preorder: ");
preOrder(tree);
System.out.print("\nInorder: ");
inOrder(tree);
System.out.print("\nPostorder: ");
postOrder(tree);
deleteFromTree(tree, 45);
deleteFromTree(tree, 24);
deleteFromTree(tree, 56);
System.out.println("\n");
System.out.print("Preorder: ");
preOrder(tree);
System.out.print("\nInorder: ");
inOrder(tree);
System.out.print("\nPostorder: ");
postOrder(tree);
System.out.println("\n");
int key = 56;
if (binarySearch(tree, key) != null) System.out.println("\nFound key: " + key);
else System.out.println("\nKey not found: " + key);
System.out.println("Tree height: " + treeHeight(tree));
}
public static TreeNode binarySearch(TreeNode node, int key) {
while (node != null) {
if (node.getKey() == key) return node;
if (key > node.getKey()) node = node.getRight();
else node = node.getLeft();
}
return null;
}
public static TreeNode findParentNode(TreeNode current, int key) {
TreeNode parentNode = null;
while (current != null) {
if (current.getKey() == key) return parentNode;
parentNode = current;
if (current.getKey() < key) current = current.getRight();
else current = current.getLeft();
}
return parentNode;
}
public static TreeNode predecessor(TreeNode current) {
current = current.getLeft();
while (current.getRight() != null) current = current.getRight();
return current;
}
public static boolean deleteFromTree(TreeNode node, int key) {
TreeNode current, parentNode, replacement;
replacement = null;
current = binarySearch(node, key);
if (current == null) return false; // The key was not found
parentNode = findParentNode(node, key);
if (current.getLeft() == null || current.getRight() == null) { // The node has zero or one child
if (current.getLeft() == null) {
replacement = current.getRight();
} else {
replacement = current.getLeft();
}
if (parentNode == null) { // The root is the only node without a parent
node.setKey(replacement.getKey());
node.setLeft(replacement.getLeft());
node.setRight(replacement.getRight());
} else {
if (key < parentNode.getKey()) parentNode.setLeft(replacement);
else parentNode.setRight(replacement);
}
} else {
replacement = predecessor(current);
current.setKey(replacement.getKey());
if (replacement.getLeft() != null) current.setLeft(replacement.getLeft());
else current.setLeft(null);
}
return true;
}
public static void insertIntoTree(TreeNode tree, int key) {
TreeNode previous = tree;
while (tree != null) {
previous = tree;
if (tree.getKey() < key) tree = tree.getRight();
else tree = tree.getLeft();
}
TreeNode newNode = new TreeNode(key);
if (previous.getKey() < key) previous.setRight(newNode);
else previous.setLeft(newNode);
}
public static void preOrder(TreeNode node) {
if (node != null) {
System.out.print(node.getKey() + ", ");
preOrder(node.getLeft());
preOrder(node.getRight());
}
}
public static void postOrder(TreeNode node) {
if (node != null) {
postOrder(node.getLeft());
postOrder(node.getRight());
System.out.print(node.getKey() + ", ");
}
}
public static void inOrder(TreeNode node) {
if (node != null) {
inOrder(node.getLeft());
System.out.print(node.getKey() + ", ");
inOrder(node.getRight());
}
}
public static int max(int a, int b) {
if (a > b) return a;
return b;
}
public static int treeHeight(TreeNode tree) {
if (tree == null) return 0;
return 1 + max(treeHeight(tree.getLeft()), treeHeight(tree.getRight()));
}
}