diff --git a/lab7/BSTMap.java b/lab7/BSTMap.java new file mode 100644 index 000000000..530be22aa --- /dev/null +++ b/lab7/BSTMap.java @@ -0,0 +1,133 @@ +import java.util.Iterator; +import java.util.Set; +import java.util.Iterator; + +public class BSTMap, V> implements Map61B { + /** + * Removes all of the mappings from this map. + */ + + private Node root; + + private class Node { + private K key; // sorted by key + private V val; // associated data + private Node left, right; // left and right subtrees + private int size; // number of nodes in subtree + + Node(K key, V val, int size) { + this.key = key; + this.val = val; + this.size = size; + } + } + + public void clear() { + this.root = null; + } + + /* Returns true if this map contains a mapping for the specified key. */ + public boolean containsKey(K key) { + if (key == null) { + throw new IllegalArgumentException("argument to contains() is null"); + } + return get(key) != null; + } + + /* Returns the value to which the specified key is mapped, or null if this + * map contains no mapping for the key. + */ + public V get(K key) { + return get(root, key); + } + + private V get(Node x, K key) { + if (key == null) { + throw new IllegalArgumentException("calls get() with a null key"); + } + if (x == null) { + return null; + } + int cmp = key.compareTo(x.key); + if (cmp < 0) { + return get(x.left, key); + } else if (cmp > 0) { + return get(x.right, key); + } else { + return x.val; + } + } + + /* Returns the number of key-value mappings in this map. */ + public int size() { + return root.size; + } + + /* Associates the specified value with the specified key in this map. */ + public void put(K key, V value) { + if (key == null) { + throw new IllegalArgumentException("calls put() with a null key"); + } + if (value == null) { + this.remove(key); + return; + } + root = put(root, key, value); + } + + private Node put(Node x, K key, V val) { + if (x == null) { + return new Node(key, val, 1); + } + int cmp = key.compareTo(x.key); + if (cmp < 0) { + x.left = put(x.left, key, val); + } else if (cmp > 0) { + x.right = put(x.right, key, val); + } else { + x.val = val; + } + return x; + } + + /* Returns a Set view of the keys contained in this map. */ + public Set keySet() { + throw new UnsupportedOperationException(); + } + + public void printInOrder() { + printInOrderHelper(root); + } + + private void printInOrderHelper(Node x) { + if (x.left == null && x.right == null) { + System.out.println((x.key).toString()); + } else if (x.left != null && x.right != null) { + printInOrderHelper(x.left); + } else if (x.left == null && x.right != null) { + printInOrderHelper(x.right); + } else if (x.left != null && x.right == null) { + printInOrderHelper(x.left); + } + } + + /* Removes the mapping for the specified key from this map if present. + * Not required for Lab 8. If you don't implement this, throw an + * UnsupportedOperationException. */ + public V remove(K key) { + throw new UnsupportedOperationException(); + } + + /* Removes the entry for the specified key only if it is currently mapped to + * the specified value. Not required for Lab 8. If you don't implement this, + * throw an UnsupportedOperationException.*/ + public V remove(K key, V value) { + throw new UnsupportedOperationException(); + } + + public Iterator iterator() { + throw new UnsupportedOperationException(); + } + + +}