Skip to content

Commit 0a6ce62

Browse files
committed
Adding further binary tree and hashtable implementations with activities
1 parent 46963f0 commit 0a6ce62

File tree

11 files changed

+514
-0
lines changed

11 files changed

+514
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package com.packt.datastructuresandalg.lesson3.activity.inordersuccessor;
2+
3+
import com.packt.datastructuresandalg.lesson3.binarytree.SimpleBinaryTree;
4+
5+
import java.util.Optional;
6+
7+
public class InOrderSuccessorBinaryTree<K,V> extends SimpleBinaryTree<K,V> {
8+
public Optional<K> inOrderSuccessorKey(K key) {
9+
return null;
10+
}
11+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package com.packt.datastructuresandalg.lesson3.activity.inordersuccessor.solution;
2+
3+
import com.packt.datastructuresandalg.lesson3.binarytree.BinaryTreeNode;
4+
import com.packt.datastructuresandalg.lesson3.binarytree.SimpleBinaryTree;
5+
6+
import java.util.Optional;
7+
8+
public class InOrderSuccessorBinaryTree<K, V> extends SimpleBinaryTree<K, V> {
9+
public Optional<K> inOrderSuccessorKey(K key) {
10+
Optional<BinaryTreeNode<K, V>> node = Optional.ofNullable(root);
11+
Optional<K> successor = Optional.empty();
12+
while (node.isPresent() && !node.get().getKey().equals(key)) {
13+
if (((Comparable) node.get().getKey()).compareTo(key) > 0) {
14+
successor = node.map(BinaryTreeNode::getKey);
15+
node = node.flatMap(BinaryTreeNode::getLeft);
16+
} else
17+
node = node.flatMap(BinaryTreeNode::getRight);
18+
}
19+
20+
return node.flatMap(BinaryTreeNode::getRight).map(this::minKey)
21+
.map(Optional::of).orElse(successor);
22+
}
23+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package com.packt.datastructuresandalg.lesson3.activity.openaddressing;
2+
3+
import com.packt.datastructuresandalg.lesson3.hashtable.HashProvider;
4+
import com.packt.datastructuresandalg.lesson3.hashtable.HashTable;
5+
import com.packt.datastructuresandalg.lesson3.hashtable.Pair;
6+
7+
import java.util.Optional;
8+
9+
public class OpenAddrHashTable<K, V> implements HashTable<K, V> {
10+
private final HashProvider<K> hashProvider;
11+
private Pair<K, V>[] array;
12+
13+
public OpenAddrHashTable(int capacity, HashProvider<K> hashProvider) {
14+
array = new Pair[capacity];
15+
this.hashProvider = hashProvider;
16+
}
17+
18+
public void put(K key, V value) {
19+
}
20+
21+
public Optional<V> get(K key) {
22+
return Optional.empty();
23+
}
24+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
package com.packt.datastructuresandalg.lesson3.activity.openaddressing.solution;
2+
3+
import com.packt.datastructuresandalg.lesson3.hashtable.HashProvider;
4+
import com.packt.datastructuresandalg.lesson3.hashtable.HashTable;
5+
import com.packt.datastructuresandalg.lesson3.hashtable.Pair;
6+
7+
import java.util.Optional;
8+
9+
public class OpenAddrHashTable<K, V> implements HashTable<K, V> {
10+
private final HashProvider<K> hashProvider;
11+
private Pair<K, V>[] array;
12+
13+
public OpenAddrHashTable(int capacity, HashProvider<K> hashProvider) {
14+
array = new Pair[capacity];
15+
this.hashProvider = hashProvider;
16+
}
17+
18+
public void put(K key, V value) {
19+
int s = array.length;
20+
int hashValue = hashProvider.hashKey(key, s);
21+
int i = 0;
22+
while (i < s && array[(hashValue + i) % s] != null)
23+
i++;
24+
if (i < s) array[(hashValue + i) % s] = new Pair<>(key, value);
25+
}
26+
27+
public Optional<V> get(K key) {
28+
int s = array.length;
29+
int hashValue = hashProvider.hashKey(key, s);
30+
int i = 0;
31+
while (i < s &&
32+
array[(hashValue + i) % s] != null &&
33+
!array[(hashValue + i) % s].getKey().equals(key))
34+
i++;
35+
36+
return Optional.ofNullable(array[(hashValue + i) % s])
37+
.filter(kv -> kv.getKey().equals(key)).map(Pair::getValue);
38+
}
39+
40+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
package com.packt.datastructuresandalg.lesson3.binarytree;
2+
3+
import java.util.Optional;
4+
5+
public interface BinaryTree<K,V> {
6+
void put(K key,V value);
7+
8+
Optional<V> get(K key);
9+
}
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
package com.packt.datastructuresandalg.lesson3.binarytree;
2+
3+
import java.util.Optional;
4+
5+
public class BinaryTreeNode<K,V> {
6+
private BinaryTreeNode<K,V> left;
7+
private BinaryTreeNode<K,V> right;
8+
private K key;
9+
private V value;
10+
11+
public BinaryTreeNode(K key, V value) {
12+
this.key = key;
13+
this.value = value;
14+
}
15+
16+
public Optional<BinaryTreeNode<K,V>> getLeft() {
17+
return Optional.ofNullable(left);
18+
}
19+
20+
public Optional<BinaryTreeNode<K,V>> getRight() {
21+
return Optional.ofNullable(right);
22+
}
23+
24+
public void setLeft(BinaryTreeNode<K,V> left) {
25+
this.left = left;
26+
}
27+
28+
public void setRight(BinaryTreeNode<K,V> right) {
29+
this.right = right;
30+
}
31+
32+
public K getKey() {
33+
return key;
34+
}
35+
36+
public void setKey(K key) {
37+
this.key = key;
38+
}
39+
40+
public V getValue() {
41+
return value;
42+
}
43+
44+
public void setValue(V value) {
45+
this.value = value;
46+
}
47+
}
Lines changed: 135 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,135 @@
1+
package com.packt.datastructuresandalg.lesson3.binarytree;
2+
3+
import java.util.LinkedList;
4+
import java.util.Optional;
5+
import java.util.Queue;
6+
7+
public class SimpleBinaryTree<K, V> implements BinaryTree<K, V> {
8+
protected BinaryTreeNode<K, V> root;
9+
10+
public void put(K key, V value) {
11+
if (root == null)
12+
root = new BinaryTreeNode<>(key, value);
13+
else
14+
put(key, value, root);
15+
}
16+
17+
private void put(K key, V value, BinaryTreeNode<K, V> node) {
18+
if (((Comparable) key).compareTo(node.getKey()) == 0) {
19+
node.setKey(key);
20+
node.setValue(value);
21+
} else if (((Comparable) key).compareTo(node.getKey()) < 0) {
22+
if (node.getLeft().isPresent())
23+
put(key, value, node.getLeft().get());
24+
else
25+
node.setLeft(new BinaryTreeNode<>(key, value));
26+
} else {
27+
if (node.getRight().isPresent())
28+
put(key, value, node.getRight().get());
29+
else
30+
node.setRight(new BinaryTreeNode<>(key, value));
31+
}
32+
}
33+
34+
public Optional<V> get(K key) {
35+
return Optional.ofNullable(root).flatMap(n -> get(key, n));
36+
}
37+
38+
private Optional<V> get(K key, BinaryTreeNode<K, V> node) {
39+
if (((Comparable) key).compareTo(node.getKey()) == 0)
40+
return Optional.of(node.getValue());
41+
else if (((Comparable) key).compareTo(node.getKey()) < 0)
42+
return node.getLeft().flatMap(n -> get(key, n));
43+
else
44+
return node.getRight().flatMap(n -> get(key, n));
45+
}
46+
47+
public void leftRotate(BinaryTreeNode<K, V> nodeX,
48+
BinaryTreeNode<K, V> parent) {
49+
BinaryTreeNode<K, V> nodeY = nodeX.getRight().get();
50+
nodeX.setRight(nodeY.getLeft().orElse(null));
51+
if (parent == null)
52+
this.root = nodeY;
53+
else if (parent.getLeft().filter(n -> n == nodeX).isPresent())
54+
parent.setLeft(nodeY);
55+
else
56+
parent.setRight(nodeY);
57+
nodeY.setLeft(nodeX);
58+
}
59+
60+
public void rightRotate(BinaryTreeNode<K, V> nodeX,
61+
BinaryTreeNode<K, V> parent) {
62+
BinaryTreeNode<K, V> nodeY = nodeX.getLeft().get();
63+
nodeX.setLeft(nodeY.getRight().orElse(null));
64+
if (parent == null)
65+
this.root = nodeY;
66+
else if (parent.getRight().filter(n -> n == nodeX).isPresent())
67+
parent.setRight(nodeY);
68+
else
69+
parent.setLeft(nodeY);
70+
nodeY.setRight(nodeX);
71+
}
72+
73+
74+
public Optional<K> minKey() {
75+
return Optional.ofNullable(root).map(this::minKey);
76+
}
77+
78+
protected K minKey(BinaryTreeNode<K, V> node) {
79+
return node.getLeft().map(this::minKey).orElse(node.getKey());
80+
}
81+
82+
public void printBfs() {
83+
Optional.ofNullable(root).ifPresent(r -> {
84+
Queue<BinaryTreeNode<K, V>> queue = new LinkedList<>();
85+
queue.add(r);
86+
while (!queue.isEmpty()) {
87+
BinaryTreeNode<K, V> node = queue.remove();
88+
System.out.println(node.getKey());
89+
node.getLeft().ifPresent(queue::add);
90+
node.getRight().ifPresent(queue::add);
91+
}
92+
});
93+
}
94+
95+
96+
public void printDfs() {
97+
Optional.ofNullable(root).ifPresent(this::printDfs);
98+
}
99+
100+
private void printDfs(BinaryTreeNode<K, V> node) {
101+
//System.out.println("PREORDER " + node.getKey());
102+
node.getLeft().ifPresent(this::printDfs);
103+
System.out.println("INORDER " + node.getKey());
104+
node.getRight().ifPresent(this::printDfs);
105+
//System.out.println("POSTORDER " + node.getKey());
106+
}
107+
108+
109+
public static void main(String[] args) {
110+
SimpleBinaryTree<Integer, String> binaryTree = new SimpleBinaryTree<Integer, String>();
111+
System.out.println(binaryTree.minKey());
112+
binaryTree.put(457998224, "Isabel");
113+
binaryTree.put(366112467, "John");
114+
binaryTree.put(671031776, "Ruth");
115+
binaryTree.put(225198452, "Sarah");
116+
binaryTree.put(419274013, "Peter");
117+
binaryTree.put(751965387, "Tom");
118+
119+
System.out.println(binaryTree.get(457998224));
120+
System.out.println(binaryTree.get(366112467));
121+
System.out.println(binaryTree.get(671031776));
122+
System.out.println(binaryTree.get(225198452));
123+
System.out.println(binaryTree.get(419274013));
124+
System.out.println(binaryTree.get(751965387));
125+
126+
binaryTree.put(751965387, "Sam");
127+
128+
System.out.println(binaryTree.get(751965387));
129+
System.out.println(binaryTree.get(999999999));
130+
System.out.println(binaryTree.minKey());
131+
132+
binaryTree.printDfs();
133+
binaryTree.printBfs();
134+
}
135+
}
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
package com.packt.datastructuresandalg.lesson3.hashtable;
2+
3+
import java.util.LinkedList;
4+
import java.util.Optional;
5+
6+
public class ChainedHashTable<K, V> implements HashTable<K, V> {
7+
private final HashProvider<K> hashProvider;
8+
private LinkedList<Pair<K, V>>[] array;
9+
10+
public ChainedHashTable(int capacity, HashProvider<K> hashProvider) {
11+
array = new LinkedList[capacity];
12+
for (int i = 0; i < capacity; i++) array[i] = new LinkedList<>();
13+
this.hashProvider = hashProvider;
14+
}
15+
16+
public void put(K key, V value) {
17+
int hashValue = hashProvider.hashKey(key, array.length);
18+
array[hashValue].addFirst(new Pair<>(key, value));
19+
}
20+
21+
public Optional<V> get(K key) {
22+
int hashValue = hashProvider.hashKey(key, array.length);
23+
return array[hashValue].stream()
24+
.filter(keyValue -> keyValue.getKey().equals(key))
25+
.findFirst()
26+
.map(Pair::getValue);
27+
}
28+
29+
public static void main(String args[]) {
30+
ChainedHashTable<Integer, String> chainedHashTable = new ChainedHashTable<>(10, new RemainderHashing());
31+
chainedHashTable.put(12,"Isabel");
32+
chainedHashTable.put(22,"Ruth");
33+
chainedHashTable.put(32,"Michelle");
34+
chainedHashTable.put(11,"James");
35+
chainedHashTable.put(21,"John");
36+
chainedHashTable.put(31,"Peter");
37+
System.out.println(chainedHashTable.get(12));
38+
System.out.println(chainedHashTable.get(22));
39+
System.out.println(chainedHashTable.get(32));
40+
System.out.println(chainedHashTable.get(11));
41+
System.out.println(chainedHashTable.get(21));
42+
System.out.println(chainedHashTable.get(31));
43+
System.out.println(chainedHashTable.get(42));
44+
System.out.println(chainedHashTable.get(45));
45+
}
46+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package com.packt.datastructuresandalg.lesson3.hashtable;
2+
3+
public class Pair<K, V> {
4+
private final K key;
5+
private final V value;
6+
7+
public Pair(K key, V value) {
8+
this.key = key;
9+
this.value = value;
10+
}
11+
12+
public K getKey() {
13+
return key;
14+
}
15+
16+
public V getValue() {
17+
return value;
18+
}
19+
}

0 commit comments

Comments
 (0)