javasetbinary-search-treeinorderkeyset

How to implement keySet method in Binary Search Tree


import tree.BinaryTree;

public class TreeMap<K extends Comparable<K>, V> implements MyMap<K, V> {

    private BinaryTree<Element> map;
    java.util.Set<K> keys;
    private int size;


    @Override
    public java.util.Set<K> keySet() {
        inorder(map);
        return keys;
    }

    public String toString() {
        return map.toString();
    }

    private class Element {

        K key;
        V value;

        public Element(K key, V value) {
            this.key = key;
            this.value = value;
        }

        public int compareTo(Element that) {
            return this.key.compareTo(that.key);
        }

        public String toString() {
            return (key.toString());
        }
    }

    private void inorder(BinaryTree<Element> tree) {
        if (tree != null) {
            inorder(tree.getLeft());
            keys.add(tree.getRoot().key);
            inorder(tree.getRight());
        }

    }
}

Hey guys! :) I'm having a lot of trouble with adding keys in inorder to my set of keys. How do I append keys to the set? :/ The worst part is that it isn't showing me the exception all it says is

at TreeMap.inorder(TreeMap.java:188)
at TreeMap.keySet(MyTreeMap.java:60)
at TreeMap.main(MyTreeMap.java:244)
Java Result: 1

This is just a snippet of my code, everything works but my keySet/inorder methods. Line 188 on my code is

keys.add(tree.getRoot().key);

I have searched, retried, and searched again but cant get anywhere. I would appreciate any help you guys can give me. Thanks in advance! :)

Here is the BinaryTree class

public class BinaryTree<E>
{
  private E root;
  private BinaryTree<E> left;
  private BinaryTree<E> right;

  public BinaryTree(E paramE, BinaryTree<E> paramBinaryTree1, BinaryTree<E> paramBinaryTree2)
  {
    this.root = paramE;
    this.left = paramBinaryTree1;
    this.right = paramBinaryTree2;
  }

  public BinaryTree(E paramE)
  {
    this(paramE, null, null);
  }

  public E getRoot()
  {
    return (E)this.root;
  }

  public BinaryTree<E> getLeft()
  {
    return this.left;
  }

  public BinaryTree<E> getRight()
  {
    return this.right;
  }

  public E setRoot(E paramE)
  {
    Object localObject = this.root;
    this.root = paramE;
    return (E)localObject;
  }

  public BinaryTree<E> setLeft(BinaryTree<E> paramBinaryTree)
  {
    BinaryTree localBinaryTree = this.left;
    this.left = paramBinaryTree;
    return localBinaryTree;
  }

  public BinaryTree<E> setRight(BinaryTree<E> paramBinaryTree)
  {
    BinaryTree localBinaryTree = this.right;
    this.right = paramBinaryTree;
    return localBinaryTree;
  }

  public String toString()
  {
    StringBuilder localStringBuilder = new StringBuilder("" + this.root);
    if (!isLeaf())
    {
      localStringBuilder.append("(");
      if (this.left != null) {
        localStringBuilder.append(this.left);
      }
      if (this.right != null) {
        localStringBuilder.append("," + this.right);
      }
      localStringBuilder.append(")");
    }
    return localStringBuilder + "";
  }

  public boolean isLeaf()
  {
    return (this.left == null) && (this.right == null);
  }
}

Solution

  • the TreeSet, according to your code, was never initialized

    TreeSet ts = new TreeSet();

    I think that should fix it...