Skip to content

Instantly share code, notes, and snippets.

@cdduarte
Created June 5, 2014 04:17
Show Gist options
  • Save cdduarte/abf6d780cb17d1917f32 to your computer and use it in GitHub Desktop.
Save cdduarte/abf6d780cb17d1917f32 to your computer and use it in GitHub Desktop.
/**
* MyTreeMap<K,V> - Simple binary tree implementation.
*
* @author christopherduarte
* @course cis551
* @assignment HW7
*
* @param <K,V>
*/
public class MyTreeMap<K extends Comparable<K>,V> {
private int size = 0;
private int count = 0;
class Node {
K key;
V value;
Node leftLeaf;
Node rightLeaf;
Node(K key, V value) {
this.key = key;
this.value = value;
}
}
Node root;
public void putNode(K key, V value) {
Node newNode = new Node(key, value);
if (root == null) {
root = newNode;
} else {
Node thisNode = root;
Node parent;
while (true) {
parent = thisNode;
if (key.compareTo(thisNode.key) < 0) {
thisNode = thisNode.leftLeaf;
if (thisNode == null) {
parent.leftLeaf = newNode;
return;
}
} else {
thisNode = thisNode.rightLeaf;
if (thisNode == null) {
parent.rightLeaf = newNode;
return;
}
}
}
}
++this.count;
++this.size;
}
public Node nodeSearch(K key) {
Node thisNode = root;
while (thisNode.key != key) {
if ( key.compareTo(thisNode.key) < 0) {
thisNode = thisNode.leftLeaf;
} else {
thisNode = thisNode.rightLeaf;
}
if (thisNode == null)
return null;
}
return thisNode;
}
public void clear() {
++this.count;
this.size = 0;
root = null;
}
public Object clone() {
Node tmp = root;
return tmp;
}
public boolean containsKey(K key) {
@SuppressWarnings("unchecked")
K tmp = (K) nodeSearch(key);
if(tmp != null)
return true;
else
return false;
}
public boolean containsValue(Object value) {
Node tmp = root;
if (tmp != null)
while (tmp.leftLeaf != null)
tmp = tmp.leftLeaf;
return tmp.value == value;
}
public V get(K key) {
return nodeSearch(key).value;
}
public K firstKey() {
Node tmp = root;
if (tmp != null)
while (tmp.leftLeaf != null)
tmp = tmp.leftLeaf;
return tmp.key;
}
public boolean isEmpty() {
return this.size == 0;
}
public K lastKey() {
Node tmp = root;
if (tmp != null)
while (tmp.rightLeaf != null)
tmp = tmp.rightLeaf;
return tmp.key;
}
public V put(K key, V value) {
V tmp = nodeSearch(key).value;
nodeSearch(key).value = value;
++this.count;
return tmp;
}
public V remove(K key) {
V tmp = nodeSearch(key).value;
nodeSearch(key).value = null;
++this.count;
return tmp;
}
public int size() {
return this.size;
}
public void inOrder(Node thisNode) {
if (thisNode != null) {
preOrder(thisNode.leftLeaf);
System.out.println(thisNode);
preOrder(thisNode.rightLeaf);
}
}
public void preOrder(Node thisNode) {
if (thisNode != null) {
System.out.println(thisNode);
preOrder(thisNode.leftLeaf);
preOrder(thisNode.rightLeaf);
}
}
public void postOrder(Node thisNode) {
if (thisNode != null) {
preOrder(thisNode.leftLeaf);
preOrder(thisNode.rightLeaf);
System.out.println(thisNode);
}
}
}
@cdduarte
Copy link
Author

cdduarte commented Jun 5, 2014

The class is incomplete and a work in progress

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment