Skip to content

Commit 7552e44

Browse files
committed
Updated 1 solution
1 parent ef68371 commit 7552e44

File tree

5 files changed

+99
-25
lines changed

5 files changed

+99
-25
lines changed

Chp. 04 - Trees and Graphs/_4_06_Successor/Successor.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ private static TreeNode leftMostChild(TreeNode node) {
2525
return result;
2626
}
2727

28-
/* Finds the parent (well, ancestor) that has treeNode in its left subtree. Returns null if such parent doesn't exist */
28+
// Finds the parent (well, ancestor) that has treeNode in its left subtree. Returns null if such parent doesn't exist
2929
private static TreeNode properParent(TreeNode node) {
3030
if (node == null) {
3131
return null;
@@ -39,3 +39,6 @@ private static TreeNode properParent(TreeNode node) {
3939
return parent;
4040
}
4141
}
42+
43+
// Finding Successor of a Node is useful when deleting a node in a binary *search* tree,
44+
// since that's a node we can put in place of the deleted node

Chp. 04 - Trees and Graphs/_4_11_Random_Node/BST.java

Lines changed: 19 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -2,26 +2,31 @@
22

33
class BST {
44
Node root = null;
5+
RandomizedCollection<Node> collection = new RandomizedCollection<>();
56

67
public void insert(int value) {
8+
Node item = new Node(value);
9+
10+
// Update collection
11+
collection.add(item);
12+
13+
// Update tree
714
if (root == null) {
8-
root = new Node(value);
15+
root = item;
916
return;
1017
}
11-
1218
Node curr = root;
1319
while (true) {
14-
curr.size++;
1520
if (value <= curr.value) {
1621
if (curr.left == null) {
17-
curr.left = new Node(value);
22+
curr.left = item;
1823
return;
1924
} else {
2025
curr = curr.left;
2126
}
2227
} else {
2328
if (curr.right == null) {
24-
curr.right = new Node(value);
29+
curr.right = item;
2530
return;
2631
} else {
2732
curr = curr.right;
@@ -45,21 +50,14 @@ public Node find(int value) {
4550
}
4651

4752
public Node getRandomNode() {
48-
if (root == null) {
49-
return null;
50-
}
51-
int randomNumber = (int) (Math.random() * root.size);
52-
return getRandomHelper(root, randomNumber);
53-
}
54-
55-
private static Node getRandomHelper(Node node, int num) {
56-
int leftSize = (node.left == null) ? 0 : node.left.size;
57-
if (num < leftSize) {
58-
return getRandomHelper(node.left, num);
59-
} else if (num == leftSize) {
60-
return node;
61-
} else {
62-
return getRandomHelper(node.right, num - leftSize - 1);
63-
}
53+
return collection.getRandom();
6454
}
55+
56+
// delete() not coded
57+
//
58+
// - To delete, can find "inorder successor" from node to delete, and put that in deleted node's spot
59+
// - For "Inorder Successor" code, see Cracking the Coding Interview question 4.6
60+
// - For Deletion, see 1-3 at: https://www.geeksforgeeks.org/binary-search-tree-set-2-delete/
6561
}
62+
63+
// Time Complexity: O(1) for getRandomNode()

Chp. 04 - Trees and Graphs/_4_11_Random_Node/Node.java

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,30 @@ class Node {
44
int value;
55
Node left;
66
Node right;
7-
int size;
87

98
Node(int value) {
109
this.value = value;
1110
left = null;
1211
right = null;
13-
size = 1; // This will count size of the entire tree rooted at this Node.
12+
}
13+
14+
@Override
15+
public boolean equals(Object other) { // must take an "Object" as a parameter, not a
16+
// "Node", so that it overrides the .equals method
17+
if (other == this) {
18+
return true;
19+
} else if (other == null || !(other instanceof Node)) {
20+
return false;
21+
}
22+
Node otherNode = (Node) other;
23+
return this.value == otherNode.value
24+
&& this.left.equals(otherNode.left)
25+
&& this.right.equals(otherNode.right);
26+
}
27+
28+
@Override
29+
public int hashCode() {
30+
return value; // since only using 1 int, won't be multiplying by primes.
1431
}
1532

1633
@Override
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
package _4_11_Random_Node;
2+
3+
import java.util.*;
4+
5+
public class RandomizedCollection<T> {
6+
Random rand = new Random();
7+
List<T> list = new ArrayList<>();
8+
Map<T, Set<Integer>> valToIndices = new HashMap<>();
9+
10+
public void add(T item) {
11+
// update Map
12+
if (!valToIndices.containsKey(item)) {
13+
valToIndices.put(item, new HashSet<>());
14+
}
15+
valToIndices.get(item).add(list.size());
16+
17+
// update List
18+
list.add(item);
19+
20+
return;
21+
}
22+
23+
public boolean remove(T item) {
24+
if (!valToIndices.containsKey(item) || valToIndices.get(item).isEmpty()) {
25+
return false;
26+
}
27+
28+
int indexToRemove = valToIndices.get(item).iterator().next();
29+
T itemLast = list.get(list.size() - 1);
30+
31+
// update List
32+
list.set(indexToRemove, itemLast);
33+
list.remove(list.size() - 1);
34+
35+
// update Map: remove overwritten index from set
36+
valToIndices.get(item).remove(indexToRemove);
37+
38+
// update Map: update the moved number's index
39+
valToIndices.get(itemLast).add(indexToRemove);
40+
valToIndices.get(itemLast).remove(list.size());
41+
42+
return true;
43+
}
44+
45+
public boolean contains(T item) {
46+
return valToIndices.containsKey(item) && !valToIndices.get(item).isEmpty();
47+
}
48+
49+
public T getRandom() {
50+
if (list.isEmpty()) {
51+
throw new Error("Collection is empty");
52+
}
53+
int index = rand.nextInt(list.size());
54+
return list.get(index);
55+
}
56+
}

Chp. 04 - Trees and Graphs/_4_11_Random_Node/Tester.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ public class Tester {
44
public static void main(String[] args) {
55
System.out.println("*** Test 4.11: RandomNode\n");
66
BST bst = new BST();
7-
int[]array = { 1, 0, 6, 2, 3, 9, 4, 5, 8, 7 };
7+
int[] array = { 1, 0, 6, 2, 3, 9, 4, 5, 8, 7 };
88
for (int num : array) {
99
bst.insert(num);
1010
}

0 commit comments

Comments
 (0)