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());
}
}
}
嘿伙计们! :)
我在为我的密钥集添加密钥时遇到了很多麻烦。
如何将密钥附加到集合中? :/
最糟糕的是,它并没有向我展示它所说的所有异常
at TreeMap.inorder(TreeMap.java:188)
at TreeMap.keySet(MyTreeMap.java:60)
at TreeMap.main(MyTreeMap.java:244)
Java Result: 1
这只是我的代码片段,一切正常但我的keySet / inorder方法。我的代码中的第188行是
keys.add(tree.getRoot().key);
我再次搜索,重试和搜索,但无法到达任何地方。我很感激你们能给我的任何帮助。 提前致谢! :)
这是BinaryTree类
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);
}
}
答案 0 :(得分:0)
根据你的代码,TreeSet从未被初始化
TreeSet ts = new TreeSet();
我认为应该解决它......