代码之家  ›  专栏  ›  技术社区  ›  BUY

打印树的最小和路径

  •  1
  • BUY  · 技术社区  · 7 年前

    我有一个计算我创建的树的最小和路径的代码。

    我的树类是这样的:

    import java.util.LinkedList;
    import java.util.Queue;
    public class SolutionTree {
    
        SolutionNode root;
    
        public SolutionTree() {
            root = null;
        }
    
        public void addRoot(int rootValue) {
            root = new SolutionNode(rootValue);
        }
    
        // New Node Adder
        public void addSolutionNode(int newNodeValue) {
            SolutionNode newNode = new SolutionNode(newNodeValue);
            SolutionNode newNodeRoot = breadth(root);
            if(newNodeRoot.getChildLeft() == null) {
                newNodeRoot.setChildLeft(newNode);
                newNode.setParentLeft(newNodeRoot);
            }
            else if(newNodeRoot.getChildRight() == null) {
                newNodeRoot.setChildRight(newNode);
                newNode.setParentLeft(newNodeRoot);
                if(newNodeRoot != root) {
                        if(newNodeRoot.getParentLeft().getChildRight().getChildLeft() == null) {
                            newNodeRoot.getParentLeft().getChildRight().setChildLeft(newNode);
                            newNode.setParentRight(newNodeRoot.getParentLeft().getChildRight());
                        }
                }
            }
        }
    
        // Node Class of Solution Tree
        protected class SolutionNode {
    
            private int value;
            private SolutionNode parentLeft;
            private SolutionNode parentRight;
            private SolutionNode childLeft;
            private SolutionNode childRight;
    
            // Constructor
            public SolutionNode() {
                value = 0;
                parentLeft = null;
                parentRight = null;
                childLeft = null;
                childRight = null;
            }
    
            // Constructor
            public SolutionNode(int v) {
                value = v;
                parentLeft = null;
                parentRight = null;
                childLeft = null;
                childRight = null;
            }
    
            // MODIFIERS
    
            public void setValue(int val) {
                value = val;
            }
    
            public void setParentLeft(SolutionNode leftParent) {
                parentLeft = leftParent;
            }
    
        public void setParentRight(SolutionNode rightParent) {
            parentLeft = rightParent;
        }
    
            public void setChildLeft(SolutionNode leftChild) {
                childLeft = leftChild;
            }
    
            public void setChildRight(SolutionNode rightChild) {
                childRight = rightChild;
            }
    
            //ACCESSORS
    
            public int getValue() {
                return value;
            }
    
            public SolutionNode getParentLeft() {
                return parentLeft;
            }
    
            public SolutionNode getParentRight() {
                return parentRight;
            }
    
            public SolutionNode getChildLeft() {
                return childLeft;
            }
    
            public SolutionNode getChildRight() {
                return childRight;
            }
    
    
    
    
        }
    
        // function to compute the minimum sum path
        // It only returns the sum of the values of nodes on the min sum path 
        int minSumPath(SolutionNode current) {
            if(current == null)
                return 0;
    
            int sum = current.getValue();
    
            int left_sum = minSumPath(current.childLeft);
            int right_sum = minSumPath(current.childRight);
    
            if(left_sum <= right_sum) {
                sum += minSumPath(current.childLeft);
            }
            else {
                sum += minSumPath(current.childRight);
            }
    
            return sum;
        }
    
        // Breadth First Traversal
        public static SolutionNode breadth(SolutionNode root) {
            Queue<SolutionNode> queue = new LinkedList<SolutionNode>() ;
            if (root == null)
                return null;
            queue.clear();
            queue.add(root);
           while(!queue.isEmpty()){
                SolutionNode node = queue.remove();
                if(node.childLeft != null) 
                    queue.add(node.childLeft);
                if(node.childRight != null) 
                    queue.add(node.childRight);
                if(node.childLeft == null || node.childRight == null)
                    return node;
            }
            return null;
        }
    
    
    }
    

    我有一个程序,它从一个.txt文件中读取整数并添加到解决方案树中,然后计算树的最小和路径(从根到叶节点(节点值的总和))。通过调用SolutionTree的minSumpath方法。

    我想打印计算出的路径。例如,如果树是:

            1
        2        3
    4        5        6   
    

    最小求和路径为7,通过求和1+2+4计算。我想打印这个过程。有什么办法吗?我很感激你的帮助。

    提前谢谢。

    2 回复  |  直到 7 年前
        1
  •  2
  •   BUY    7 年前

    不要在递归方法中返回int,而应该返回一个包含传递的节点的和和和字符串的类。

    此代码应适用于您:

     public class number{
        private int sum;
        private String str;
    
        // CONSTRUCTOR
        public number(int sum, String str){
            this.sum=sum;
            this.str=str;
        }
    
        public void add(int sum2){
            sum+=sum2;
            if(!str.equals(""))
                str = str +" + "+ sum2;
            else if(str.equals(""))
                str = "" + sum2;
        }
    
        // ACCESSORS
        public String getStr() {
            return this.str;
        }
    
        public int getSum() {
            return this.sum;
        }
    
        // MODIFIERS
        public void setStr(String newStr) {
            this.str = newStr;
        }
    
        public void setSum(int newSum) {
            this.sum = newSum;
        }
    
    }
    // function to compute the minimum sum path
    // It only returns the sum of the values of nodes on the min sum path 
    number minSumPath(SolutionNode current) {
      number  tr1= new number(0,"");
        if(current == null){
            return tr1;
        }
        int sum = current.getValue();
    
        int left_sum = minSumPath(current.childLeft).sum;
        int right_sum = minSumPath(current.childRight).sum;
    
        if(left_sum <= right_sum) {
           tr1= minSumPath(current.childLeft);
            tr1.add(sum);
        }
        else {
            tr1= minSumPath(current.childLeft);
            tr1.add(sum);
           }
        return tr1;
    }
    
        2
  •  1
  •   Tiago Redaelli    7 年前

    为什么不创建一个balancedsearchtree呢,最小的和总是朝左,如果失败了,朝右,那么您所要做的就是遍历树,直到到达末尾。这样就不必访问树上的所有节点。

    像这样的:

    private int minPath(Node<E> n, int min, ArrayList<Integer> pathTaken) {
        if (n.left != null) {// Left is smaller than parent and exists, go there
            pathTaken.add(n.value);
            return minPath(n.left, min + n.value);
        }
        else if (n.right != null) {// Else go right
            pathTaken.add(n.value);
            return minPath(n.right, min + n.value);
        }
        return min; // There are no more children 
    }
    
    public minSumPath() {
        if (root == null)
            return -1;
        ArrayList<Integer> pathTaken = new ArrayList<>();
        pathTaken.add(root.getValue());
        int min = minSumPath(root, pathTaken);
        System.out.println("Patk taken: " + pathTaken.toString());
        return min;
    }
    

    为了记录执行的路径,只需在递归方法参数中添加一个arraylist。注意,我没有检查添加的路径是否为空,您可能应该这样做。

    private int minSumPath(SolutionNode current, ArrayList<Integer> pathTaken) {
            if(current == null)
                return 0;
    
            int sum = current.getValue();
    
            int left_sum = minSumPath(current.childLeft);
            int right_sum = minSumPath(current.childRight);
    
            if(left_sum <= right_sum) {
                pathTaken.add(current.childLeft.getValue());
                sum += minSumPath(current.childLeft);
            }
            else {
                pathTaken.add(current.childRight.getValue());
                sum += minSumPath(current.childRight);
            }
    
            return sum;
        }
    
    public minSumPath() {
        if (root == null)
            return -1;
        ArrayList<Integer> pathTaken = new ArrayList<>();
        pathTaken.add(root.getValue());
        int min = minSumPath(root, pathTaken);
        System.out.println("Patk taken: " + pathTaken.toString());
        return min;
    }
    

    您可以做的一个优化是存储找到的最新最小路径的变量,当它所在的路径大于上一条记录时,您将返回integer.max_value并中止该分支的递归,因为它在那里找不到较短的路径。