没有setter的Java中的平衡二进制树

时间:2016-03-29 22:22:22

标签: java tree binary-tree setter

我正在完成一项关于创建平衡二叉树的学校作业。 Node和Tree的接口提供了声明的方法。但是,Node接口只有getLeftgetRightgetValue方法,没有setter。由于我们只提交了对实现文件进行评分,因此我通过使用实现类本身来键入而不是接口来解决它。

当我向老师发信息时,他告诉我可以仅使用Node构造函数来实现它,添加一个“提示”,“对于每个节点,它的子节点也是树”。这很明显,但我不确定如何帮助我。

在我看来,如果不使用setter,我首先需要提前将树映射出来,然后从底部而不是从顶部开始构建它,这似乎是不必要的复杂和反直觉。是否有一些我不知道的技巧?

感谢您提供任何帮助或建议。

我目前的实施如下:

TreeImpl.java

public class TreeImpl implements Tree {
    private NodeImpl root;

    public TreeImpl() {}

    @Override
    public void setTree(int[] values) {
        this.root = null;
        Arrays.sort(values);
        recurseSet(values);
    }

    private void recurseSet(int[] values) {
        if (values.length > 0) {
            int middleIndex = values.length / 2;
            NodeImpl tempNode = new NodeImpl(values[middleIndex]);
            insert(tempNode, root, 1);
            recurseSet(cutArray(values, 0, middleIndex - 1));
            recurseSet(cutArray(values, middleIndex+1, values.length-1));
        }
    }

    private int[] cutArray(int[] array, int begin, int end) {
        int length = end-begin+1;
        int[] newArray = new int[length];
        System.arraycopy(array, begin, newArray, 0, length);
        return newArray;
    }


    private void insert(NodeImpl node, NodeImpl location, int depth) {
        if (root == null) {
            root = node;
            return;
        }
        if (node.getValue() < location.getValue()) {
            /* left branch */
            if(location.getLeft() == null) {
                node.setDepth(depth);
                location.setLeft(node);
            } else {
                insert(node, location.getLeft(), depth+1);
            }

        } else {
            /* right branch */
            if(location.getRight() == null) {
                node.setDepth(depth);
                location.setRight(node);
            } else {
                insert(node, location.getRight(), depth+1);
            }
        }

    }

    @Override
    public Node getRoot() {
        return root;
    }

    private String toString(NodeImpl root) {
        String finalString = "";
        if (root != null) {
            finalString += root;
            finalString += toString(root.getLeft());
            finalString += toString(root.getRight());
        }
        return finalString;
    }

    @Override
    public String toString() {
        return toString(root);
    }
}

NodeImpl.java

public class NodeImpl implements Node {
    private int value;
    private NodeImpl left;
    private NodeImpl right;
    private int depth = 0;

    public NodeImpl(int value) {
        this.value = value;
    }

    public void setLeft(NodeImpl left) {
        this.left = left;
    }

    public void setRight(NodeImpl right) {
        this.right = right;
    }

    public void setDepth(int depth) {
        this.depth = depth;
    }

    @Override
    public NodeImpl getLeft() {
            return left;
    }

    @Override
    public NodeImpl getRight() {
        return right;
    }

    @Override
    public int getValue() {
        try {
            return value;
        } catch (NullPointerException e) {
            System.out.println("Null pointer.");
        }
        return 0;
    }

    @Override
    public String toString() {
        String finalString = "";
        for(int i = 0; i < depth; i++) {
            finalString += " ";
        }
        finalString += "- ";
        finalString += value;
        finalString += "\n";
        return finalString;
    }

}

1 个答案:

答案 0 :(得分:1)

我使用了你的代码,我想我已经知道如何做到这一点:

class NodeImpl implements Node {
    private int value;
    private Node left;
    private Node right;

    public NodeImpl(int value, Node left, Node right) {
        this.value = value;
        this.left = left;
        this.right = right;
    }

    public Node getLeft() {
        return left;
    }

    public Node getRight() {
        return right;
    }

    public int getValue() {
        return value;
    }

    @Override
    public String toString() {
        // here you have to put some nice drawing logic.
        return  (left != null ? left.toString() : "") + "<-" + value + "->" + (right != null ? right.toString() : "");
    }
}

class TreeImpl implements Tree {
    private Node root;

    public void setTree(int[] values) {
        Arrays.sort(values);
        this.root = recurseSet(values);
    }

    private Node recurseSet(int[] values) {
        if (values.length > 0) {
            int middleIndex = values.length / 2;
            return new NodeImpl(
                values[middleIndex], recurseSet(cutArray(values, 0, middleIndex - 1)),
                recurseSet(cutArray(values, middleIndex + 1, values.length - 1))
            );
        } else {
            return null;
        }
    }

    private int[] cutArray(int[] array, int begin, int end) {
        int length = end - begin + 1;
        int[] newArray = new int[length];
        System.arraycopy(array, begin, newArray, 0, length);
        return newArray;
    }

    public Node getRoot() {
        return root;
    }
}

您将使用以下类:

public static void main(String[] args) {
    final Tree tree = new TreeImpl();
    tree.setTree(new int[]{1, 10, 9, 8, 2, 5});
    System.out.println(tree.getRoot().toString());
}

你只需要考虑如何实现NodeImpl.toString()方法以一种很好的方式绘制每个节点:)我希望它能帮助你。