Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

create me #1

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
133 changes: 133 additions & 0 deletions lab7/BSTMap.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,133 @@
import java.util.Iterator;
import java.util.Set;
import java.util.Iterator;

public class BSTMap<K extends Comparable<K>, V> implements Map61B<K, V> {
/**
* 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<K> 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<K> iterator() {
throw new UnsupportedOperationException();
}


}