遍历通用二叉树

香辣虎

将使用泛型类创建抽象的二叉树。每个节点都有一个字符串值以及一个initialCalculatedValue值。不应对主类进行任何更改,并且在通用类中应包括一个静态内部类。我想要一些有关代码的建议,因为主类在访问“ timesVisited”和“ values”时给了我错误。我的代码似乎无法访问这些变量。

主类代码:

public class Main{
    public static void main(String[] args) {        
        WalkableTree<String, Integer> ast = new WalkableTree<>(0);

        WalkableTree.Node<String, Integer> plus = ast.setRoot("+");
        plus.setRightChild("20");

        WalkableTree.Node<String, Integer> multiply = plus.setLeftChild("*");
        multiply.setLeftChild("10");

        WalkableTree.Node<String, Integer> bracketedPlus = multiply.setRightChild("+");
        bracketedPlus.setLeftChild("3");
        bracketedPlus.setRightChild("4");

        // write visitor to display pre-order
        System.out.println("Pre-order traversal:");
        ast.walk(current -> {
            if(current.timesVisited == 2)
                System.out.print(current.value + " ");
        });
        System.out.println();

        // write visitor to display in-order
        System.out.println("In-order traversal:");
        ast.walk(current -> {
            if(current.timesVisited == 3)
                System.out.print(current.value + " ");
        });
        System.out.println();

        // write visitor to display post-order
        System.out.println("Post-order traversal:");
        ast.walk(current -> {
            if(current.timesVisited == 4)
                System.out.print(current.value + " ");
        });
        System.out.println();
    }
}

功能界面:

@FunctionalInterface
public interface Visitor<N> {
    public void visit(N node);
}

通用类:

public class WalkableTree <T, R> {

     private T root = null;
     private R initialCalculatedValue;
     public static Node current;

    public WalkableTree(R initialCalculatedValue) {
        this.initialCalculatedValue = initialCalculatedValue;
    }

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

    public Node setRoot(T value) {
        current = new Node(null,null,null,value,null,0);
        return current;
    }

    public R getInitialCalculatedValue() {
        return initialCalculatedValue;
    }

    public void setInitialCalculatedValue(R initialCalculatedValue) {
        this.initialCalculatedValue = initialCalculatedValue;
    }

    protected void reset(Node node) {
        node.timesVisited = 0;
        node.calculatedValue = initialCalculatedValue;
        reset((Node) node.leftChild);
        reset((Node) node.rightChild);
    }

    public Node nextNode(Node node) {
        node.timesVisited++;
        if(node.timesVisited == 1)
            return node;

        if(node.timesVisited == 2)
            return (Node) node.leftChild;

        if(node.timesVisited == 3)
            return (Node) node.rightChild;

        if(node.timesVisited == 4)
            return (Node) node.getParent();

        return node;
    }

    public void walk(Visitor visitor) {
        //Reset all the nodes in the tree
        reset((Node) root);

        //Set the current node to visit at the root of the tree
        visitor.visit(root);

        //Walking through the tree as long as the current node still exists
            //If current node exists, let the visitor object visit the current node
            //Current node is set to the next node using nextNode() method
        while (this.current == current)
        {
            nextNode(current);
        }
    }

    public static class Node<T, R> {
        //Variables
        Object leftChild;
        Object rightChild;
        Object parent;
        T value;
        R calculatedValue;
        int timesVisited = 0;

        public Node(Object leftChild, Object rightChild, Object parent, T value, R calculatedValue, int timesVisited) {
            this.leftChild = leftChild;
            this.rightChild = rightChild;
            this.parent = parent;
            this.value = value;
            this.calculatedValue = calculatedValue;
            this.timesVisited = timesVisited;
        }

        public Object getLeftChild() {
            return leftChild;
        }

        public Node setLeftChild(T value) {
            Node newLeft = new Node(null,null, current,value,0,0);
            current = newLeft;
            return current;
        }

        public Object getRightChild() {
            return rightChild;
        }

        public Node setRightChild(T value) {
            Node newRight = new Node(null,null, current,value,0,0);
            current = newRight;
            return current;
        }

        public Object getParent() {
            return parent;
        }

        public void setParent(Node parent) {
            this.parent = parent;
        }

        public T getValue() {
            return value;
        }

        public void setValue(T value) {
            this.value = value;
        }

        public R getCalculatedValue() {
            return calculatedValue;
        }

        public void setCalculatedValue(R calculatedValue) {
            this.calculatedValue = calculatedValue;
        }

        public int getTimesVisited() {
            return timesVisited;
        }

        public void setTimesVisited(int timesVisited) {
            this.timesVisited = timesVisited;
        }
    }
}
安舒尔·坎德尔瓦尔(Anshul Khandelwal):

更新WalkableTree中的方法,如下所示:

 public void walk(Visitor<Node> visitor) {
        //Reset all the nodes in the tree
        reset((Node) root);

        //Set the current node to visit at the root of the tree
        visitor.visit((Node) root);

        //Walking through the tree as long as the current node still exists
        //If current node exists, let the visitor object visit the current node
        //Current node is set to the next node using nextNode() method
        while (this.current == current)
        {
            nextNode(current);
        }
    }

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章