Commit f6231142 authored by Adam Blank's avatar Adam Blank
Browse files

Update BSTDictionary.java

parent 400f284e
No related merge requests found
Pipeline #6815 failed with stage
in 0 seconds
Showing with 26 additions and 0 deletions
+26 -0
......@@ -56,6 +56,32 @@ public class BSTDictionary<K extends Comparable<K>, V> implements IDictionary<K,
public Iterator<K> iterator() {
return null;
}
@Override
public String toString() {
if (this.root == null) {
return "{}";
}
StringBuilder contents = new StringBuilder();
IQueue<BSTNode<K, V>> nodes = new ArrayDeque<>();
BSTNode<K, V> current = this.root;
while (current != null) {
contents.append(current.key + ": " + current.value + ", ");
if (current.children[0] != null) {
nodes.enqueue(current.children[0]);
}
if (current.children[1] != null) {
nodes.enqueue(current.children[1]);
}
current = nodes.dequeue();
}
return "{" + contents.toString().substring(0, contents.length() - 2) + "}";
}
protected static class BSTNode<K, V> {
public final K key;
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment