Java中的简单二叉树仅在一个类中



我必须实现一个非常简单的二叉树。我只需要一个put和一个get的方法。如果一个键已经在使用中,我只需要替换该值。这是我的代码,但我非常不确定这是否正常工作......你能确认这段代码会起作用吗?不好意思。。。我不知道如何调试这样的类...:/不 - 这不是家庭作业。只是学习Java的课程中的模板... ;)

//Don't import any other classes.
public class BinaryTree<K extends Comparable<K>, V> {
    //Please don't add any further attributes.
    private K key;
    private V value;
    private BinaryTree<K, V> left;
    private BinaryTree<K, V> right;
    /**
     * This class is a binary tree-based collection for key-value-pairs.
     */
    public static void main(String[] args) {
      BinaryTree<Integer, String> treeInteger = new BinaryTree<>(2, "two ");
      treeInteger.put(1, "one ");
      treeInteger.put(0, "zero ");
      treeInteger.put(3, "three ");
      treeInteger.put(6, "six ");
      treeInteger.put(3, "threeNew ");

        System.out.println(tree.toString());
        if (tree.get(4) == null) {
            System.out.println("null");
        }
    }
    public BinaryTree(K key, V value) {
        //Fill in your solution here.
        this.key = key;
        this.value = value;
    }
    public void put(K key, V value) {
        //Fill in your solution here.
        if (this.key.compareTo(key) > 0 ) {
            // links
            if (this.left == null) {
                this.left = new BinaryTree<>(key, value);
            } else {
                left.put(key, value);
            }
        } else if (this.key.compareTo(key) < 0) {
            if (this.right == null) {
                this.right = new BinaryTree<>(key, value);
            } else {
                this.right.put(key, value);
            }
        } else if (this.key.compareTo(key) == 0) {
            this.key = key;
            this.value = value;
        }
    }
    public V get(K key) {
        //Fill in your solution here.
        if (this.key.compareTo(key) > 0 && this.left != null) {
            return this.left.get(key);
        } else if (this.key.compareTo(key) < 0 && this.right != null) {
            return this.right.get(key);
        } else if (this.key.compareTo(key) == 0 && this.value != null) {
            return this.value;
        } else if (this.key == key) {
            return value;
        }
        return null;
    }
}

我自己的touring

    String s = "";
    if (left != null) {
        s += left.toString();
    }
    if (value != null) {
        s += value.toString();
    }
    if (right != null) {
        s += right.toString();
    }
    return s;

添加这个并测试:

@Override
public String toString() {
    if(this.left==null&&this.right != null){
        return this.value.toString()+" " + this.right.toString();
    } 
    if(this.left!=null&&this.right == null){
        return this.value.toString()+" " + this.left.toString();
    } 
    if(this.left !=null && this.right != null)
    return this.value.toString()+" " + this.left.toString()
                +" " + this.right.toString();
    return this.value.toString();;
}

这是打印树中所有节点的预序树遍历。如果正确创建树,它应该能够打印所有值。

要按升序打印,请使用按顺序树遍历:

@Override
public String toString() {
    if(this.left==null&&this.right != null){
        return this.value.toString()+" " + this.right.toString();
    } 
    if(this.left!=null&&this.right == null){
        return  this.left.toString() +" " + this.value.toString();
    } 
    if(this.left !=null && this.right != null)
    return this.left.toString()
                +" " +this.value.toString()+" "  + this.right.toString();
    return this.value.toString();
}

最新更新