-
Notifications
You must be signed in to change notification settings - Fork 85
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adding further binary tree and hashtable implementations with activities
- Loading branch information
Showing
11 changed files
with
514 additions
and
0 deletions.
There are no files selected for viewing
11 changes: 11 additions & 0 deletions
11
...kt/datastructuresandalg/lesson3/activity/inordersuccessor/InOrderSuccessorBinaryTree.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
package com.packt.datastructuresandalg.lesson3.activity.inordersuccessor; | ||
|
||
import com.packt.datastructuresandalg.lesson3.binarytree.SimpleBinaryTree; | ||
|
||
import java.util.Optional; | ||
|
||
public class InOrderSuccessorBinaryTree<K,V> extends SimpleBinaryTree<K,V> { | ||
public Optional<K> inOrderSuccessorKey(K key) { | ||
return null; | ||
} | ||
} |
23 changes: 23 additions & 0 deletions
23
...ructuresandalg/lesson3/activity/inordersuccessor/solution/InOrderSuccessorBinaryTree.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
package com.packt.datastructuresandalg.lesson3.activity.inordersuccessor.solution; | ||
|
||
import com.packt.datastructuresandalg.lesson3.binarytree.BinaryTreeNode; | ||
import com.packt.datastructuresandalg.lesson3.binarytree.SimpleBinaryTree; | ||
|
||
import java.util.Optional; | ||
|
||
public class InOrderSuccessorBinaryTree<K, V> extends SimpleBinaryTree<K, V> { | ||
public Optional<K> inOrderSuccessorKey(K key) { | ||
Optional<BinaryTreeNode<K, V>> node = Optional.ofNullable(root); | ||
Optional<K> successor = Optional.empty(); | ||
while (node.isPresent() && !node.get().getKey().equals(key)) { | ||
if (((Comparable) node.get().getKey()).compareTo(key) > 0) { | ||
successor = node.map(BinaryTreeNode::getKey); | ||
node = node.flatMap(BinaryTreeNode::getLeft); | ||
} else | ||
node = node.flatMap(BinaryTreeNode::getRight); | ||
} | ||
|
||
return node.flatMap(BinaryTreeNode::getRight).map(this::minKey) | ||
.map(Optional::of).orElse(successor); | ||
} | ||
} |
24 changes: 24 additions & 0 deletions
24
...ava/com/packt/datastructuresandalg/lesson3/activity/openaddressing/OpenAddrHashTable.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
package com.packt.datastructuresandalg.lesson3.activity.openaddressing; | ||
|
||
import com.packt.datastructuresandalg.lesson3.hashtable.HashProvider; | ||
import com.packt.datastructuresandalg.lesson3.hashtable.HashTable; | ||
import com.packt.datastructuresandalg.lesson3.hashtable.Pair; | ||
|
||
import java.util.Optional; | ||
|
||
public class OpenAddrHashTable<K, V> implements HashTable<K, V> { | ||
private final HashProvider<K> hashProvider; | ||
private Pair<K, V>[] array; | ||
|
||
public OpenAddrHashTable(int capacity, HashProvider<K> hashProvider) { | ||
array = new Pair[capacity]; | ||
this.hashProvider = hashProvider; | ||
} | ||
|
||
public void put(K key, V value) { | ||
} | ||
|
||
public Optional<V> get(K key) { | ||
return Optional.empty(); | ||
} | ||
} |
40 changes: 40 additions & 0 deletions
40
...ackt/datastructuresandalg/lesson3/activity/openaddressing/solution/OpenAddrHashTable.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
package com.packt.datastructuresandalg.lesson3.activity.openaddressing.solution; | ||
|
||
import com.packt.datastructuresandalg.lesson3.hashtable.HashProvider; | ||
import com.packt.datastructuresandalg.lesson3.hashtable.HashTable; | ||
import com.packt.datastructuresandalg.lesson3.hashtable.Pair; | ||
|
||
import java.util.Optional; | ||
|
||
public class OpenAddrHashTable<K, V> implements HashTable<K, V> { | ||
private final HashProvider<K> hashProvider; | ||
private Pair<K, V>[] array; | ||
|
||
public OpenAddrHashTable(int capacity, HashProvider<K> hashProvider) { | ||
array = new Pair[capacity]; | ||
this.hashProvider = hashProvider; | ||
} | ||
|
||
public void put(K key, V value) { | ||
int s = array.length; | ||
int hashValue = hashProvider.hashKey(key, s); | ||
int i = 0; | ||
while (i < s && array[(hashValue + i) % s] != null) | ||
i++; | ||
if (i < s) array[(hashValue + i) % s] = new Pair<>(key, value); | ||
} | ||
|
||
public Optional<V> get(K key) { | ||
int s = array.length; | ||
int hashValue = hashProvider.hashKey(key, s); | ||
int i = 0; | ||
while (i < s && | ||
array[(hashValue + i) % s] != null && | ||
!array[(hashValue + i) % s].getKey().equals(key)) | ||
i++; | ||
|
||
return Optional.ofNullable(array[(hashValue + i) % s]) | ||
.filter(kv -> kv.getKey().equals(key)).map(Pair::getValue); | ||
} | ||
|
||
} |
9 changes: 9 additions & 0 deletions
9
src/main/java/com/packt/datastructuresandalg/lesson3/binarytree/BinaryTree.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
package com.packt.datastructuresandalg.lesson3.binarytree; | ||
|
||
import java.util.Optional; | ||
|
||
public interface BinaryTree<K,V> { | ||
void put(K key,V value); | ||
|
||
Optional<V> get(K key); | ||
} |
47 changes: 47 additions & 0 deletions
47
src/main/java/com/packt/datastructuresandalg/lesson3/binarytree/BinaryTreeNode.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
package com.packt.datastructuresandalg.lesson3.binarytree; | ||
|
||
import java.util.Optional; | ||
|
||
public class BinaryTreeNode<K,V> { | ||
private BinaryTreeNode<K,V> left; | ||
private BinaryTreeNode<K,V> right; | ||
private K key; | ||
private V value; | ||
|
||
public BinaryTreeNode(K key, V value) { | ||
this.key = key; | ||
this.value = value; | ||
} | ||
|
||
public Optional<BinaryTreeNode<K,V>> getLeft() { | ||
return Optional.ofNullable(left); | ||
} | ||
|
||
public Optional<BinaryTreeNode<K,V>> getRight() { | ||
return Optional.ofNullable(right); | ||
} | ||
|
||
public void setLeft(BinaryTreeNode<K,V> left) { | ||
this.left = left; | ||
} | ||
|
||
public void setRight(BinaryTreeNode<K,V> right) { | ||
this.right = right; | ||
} | ||
|
||
public K getKey() { | ||
return key; | ||
} | ||
|
||
public void setKey(K key) { | ||
this.key = key; | ||
} | ||
|
||
public V getValue() { | ||
return value; | ||
} | ||
|
||
public void setValue(V value) { | ||
this.value = value; | ||
} | ||
} |
135 changes: 135 additions & 0 deletions
135
src/main/java/com/packt/datastructuresandalg/lesson3/binarytree/SimpleBinaryTree.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,135 @@ | ||
package com.packt.datastructuresandalg.lesson3.binarytree; | ||
|
||
import java.util.LinkedList; | ||
import java.util.Optional; | ||
import java.util.Queue; | ||
|
||
public class SimpleBinaryTree<K, V> implements BinaryTree<K, V> { | ||
protected BinaryTreeNode<K, V> root; | ||
|
||
public void put(K key, V value) { | ||
if (root == null) | ||
root = new BinaryTreeNode<>(key, value); | ||
else | ||
put(key, value, root); | ||
} | ||
|
||
private void put(K key, V value, BinaryTreeNode<K, V> node) { | ||
if (((Comparable) key).compareTo(node.getKey()) == 0) { | ||
node.setKey(key); | ||
node.setValue(value); | ||
} else if (((Comparable) key).compareTo(node.getKey()) < 0) { | ||
if (node.getLeft().isPresent()) | ||
put(key, value, node.getLeft().get()); | ||
else | ||
node.setLeft(new BinaryTreeNode<>(key, value)); | ||
} else { | ||
if (node.getRight().isPresent()) | ||
put(key, value, node.getRight().get()); | ||
else | ||
node.setRight(new BinaryTreeNode<>(key, value)); | ||
} | ||
} | ||
|
||
public Optional<V> get(K key) { | ||
return Optional.ofNullable(root).flatMap(n -> get(key, n)); | ||
} | ||
|
||
private Optional<V> get(K key, BinaryTreeNode<K, V> node) { | ||
if (((Comparable) key).compareTo(node.getKey()) == 0) | ||
return Optional.of(node.getValue()); | ||
else if (((Comparable) key).compareTo(node.getKey()) < 0) | ||
return node.getLeft().flatMap(n -> get(key, n)); | ||
else | ||
return node.getRight().flatMap(n -> get(key, n)); | ||
} | ||
|
||
public void leftRotate(BinaryTreeNode<K, V> nodeX, | ||
BinaryTreeNode<K, V> parent) { | ||
BinaryTreeNode<K, V> nodeY = nodeX.getRight().get(); | ||
nodeX.setRight(nodeY.getLeft().orElse(null)); | ||
if (parent == null) | ||
this.root = nodeY; | ||
else if (parent.getLeft().filter(n -> n == nodeX).isPresent()) | ||
parent.setLeft(nodeY); | ||
else | ||
parent.setRight(nodeY); | ||
nodeY.setLeft(nodeX); | ||
} | ||
|
||
public void rightRotate(BinaryTreeNode<K, V> nodeX, | ||
BinaryTreeNode<K, V> parent) { | ||
BinaryTreeNode<K, V> nodeY = nodeX.getLeft().get(); | ||
nodeX.setLeft(nodeY.getRight().orElse(null)); | ||
if (parent == null) | ||
this.root = nodeY; | ||
else if (parent.getRight().filter(n -> n == nodeX).isPresent()) | ||
parent.setRight(nodeY); | ||
else | ||
parent.setLeft(nodeY); | ||
nodeY.setRight(nodeX); | ||
} | ||
|
||
|
||
public Optional<K> minKey() { | ||
return Optional.ofNullable(root).map(this::minKey); | ||
} | ||
|
||
protected K minKey(BinaryTreeNode<K, V> node) { | ||
return node.getLeft().map(this::minKey).orElse(node.getKey()); | ||
} | ||
|
||
public void printBfs() { | ||
Optional.ofNullable(root).ifPresent(r -> { | ||
Queue<BinaryTreeNode<K, V>> queue = new LinkedList<>(); | ||
queue.add(r); | ||
while (!queue.isEmpty()) { | ||
BinaryTreeNode<K, V> node = queue.remove(); | ||
System.out.println(node.getKey()); | ||
node.getLeft().ifPresent(queue::add); | ||
node.getRight().ifPresent(queue::add); | ||
} | ||
}); | ||
} | ||
|
||
|
||
public void printDfs() { | ||
Optional.ofNullable(root).ifPresent(this::printDfs); | ||
} | ||
|
||
private void printDfs(BinaryTreeNode<K, V> node) { | ||
//System.out.println("PREORDER " + node.getKey()); | ||
node.getLeft().ifPresent(this::printDfs); | ||
System.out.println("INORDER " + node.getKey()); | ||
node.getRight().ifPresent(this::printDfs); | ||
//System.out.println("POSTORDER " + node.getKey()); | ||
} | ||
|
||
|
||
public static void main(String[] args) { | ||
SimpleBinaryTree<Integer, String> binaryTree = new SimpleBinaryTree<Integer, String>(); | ||
System.out.println(binaryTree.minKey()); | ||
binaryTree.put(457998224, "Isabel"); | ||
binaryTree.put(366112467, "John"); | ||
binaryTree.put(671031776, "Ruth"); | ||
binaryTree.put(225198452, "Sarah"); | ||
binaryTree.put(419274013, "Peter"); | ||
binaryTree.put(751965387, "Tom"); | ||
|
||
System.out.println(binaryTree.get(457998224)); | ||
System.out.println(binaryTree.get(366112467)); | ||
System.out.println(binaryTree.get(671031776)); | ||
System.out.println(binaryTree.get(225198452)); | ||
System.out.println(binaryTree.get(419274013)); | ||
System.out.println(binaryTree.get(751965387)); | ||
|
||
binaryTree.put(751965387, "Sam"); | ||
|
||
System.out.println(binaryTree.get(751965387)); | ||
System.out.println(binaryTree.get(999999999)); | ||
System.out.println(binaryTree.minKey()); | ||
|
||
binaryTree.printDfs(); | ||
binaryTree.printBfs(); | ||
} | ||
} |
46 changes: 46 additions & 0 deletions
46
src/main/java/com/packt/datastructuresandalg/lesson3/hashtable/ChainedHashTable.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
package com.packt.datastructuresandalg.lesson3.hashtable; | ||
|
||
import java.util.LinkedList; | ||
import java.util.Optional; | ||
|
||
public class ChainedHashTable<K, V> implements HashTable<K, V> { | ||
private final HashProvider<K> hashProvider; | ||
private LinkedList<Pair<K, V>>[] array; | ||
|
||
public ChainedHashTable(int capacity, HashProvider<K> hashProvider) { | ||
array = new LinkedList[capacity]; | ||
for (int i = 0; i < capacity; i++) array[i] = new LinkedList<>(); | ||
this.hashProvider = hashProvider; | ||
} | ||
|
||
public void put(K key, V value) { | ||
int hashValue = hashProvider.hashKey(key, array.length); | ||
array[hashValue].addFirst(new Pair<>(key, value)); | ||
} | ||
|
||
public Optional<V> get(K key) { | ||
int hashValue = hashProvider.hashKey(key, array.length); | ||
return array[hashValue].stream() | ||
.filter(keyValue -> keyValue.getKey().equals(key)) | ||
.findFirst() | ||
.map(Pair::getValue); | ||
} | ||
|
||
public static void main(String args[]) { | ||
ChainedHashTable<Integer, String> chainedHashTable = new ChainedHashTable<>(10, new RemainderHashing()); | ||
chainedHashTable.put(12,"Isabel"); | ||
chainedHashTable.put(22,"Ruth"); | ||
chainedHashTable.put(32,"Michelle"); | ||
chainedHashTable.put(11,"James"); | ||
chainedHashTable.put(21,"John"); | ||
chainedHashTable.put(31,"Peter"); | ||
System.out.println(chainedHashTable.get(12)); | ||
System.out.println(chainedHashTable.get(22)); | ||
System.out.println(chainedHashTable.get(32)); | ||
System.out.println(chainedHashTable.get(11)); | ||
System.out.println(chainedHashTable.get(21)); | ||
System.out.println(chainedHashTable.get(31)); | ||
System.out.println(chainedHashTable.get(42)); | ||
System.out.println(chainedHashTable.get(45)); | ||
} | ||
} |
19 changes: 19 additions & 0 deletions
19
src/main/java/com/packt/datastructuresandalg/lesson3/hashtable/Pair.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
package com.packt.datastructuresandalg.lesson3.hashtable; | ||
|
||
public class Pair<K, V> { | ||
private final K key; | ||
private final V value; | ||
|
||
public Pair(K key, V value) { | ||
this.key = key; | ||
this.value = value; | ||
} | ||
|
||
public K getKey() { | ||
return key; | ||
} | ||
|
||
public V getValue() { | ||
return value; | ||
} | ||
} |
Oops, something went wrong.