Java 打印树的最小和路径

Java 打印树的最小和路径,java,tree,Java,Tree,我有一个代码,可以计算我创建的树的最小和路径 我的树类是这样的: import java.util.LinkedList; import java.util.Queue; public class SolutionTree { SolutionNode root; public SolutionTree() { root = null; } public void addRoot(int rootValue) { root =

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

我的树类是这样的:

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;
    }


}
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;
}
最小和路径为7,通过1+2+4求和计算得出。我想打印这个过程。你知道我该怎么做吗?我将感谢任何帮助


提前谢谢。

你为什么不创建一个平衡搜索树呢,最小的总和总是朝左,如果失败了,朝右,那么你所要做的就是在树上迭代,直到到达终点。这样,您就不必访问树上的所有节点

大概是这样的:

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;
    }


}
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;
}
private int-minPath(节点n,int-min,ArrayList-pathTake){
如果(n.left!=null){//left小于父项并且存在,则转到那里
添加(n.value);
返回最小路径(n.left,min+n.value);
}
如果(n.right!=null){//else向右走
添加(n.value);
返回minPath(n.右,min+n.值);
}
return min;//不再有子项
}
公共minSumPath(){
if(root==null)
返回-1;
ArrayList pathTake=新建ArrayList();
add(root.getValue());
int min=minSumPath(根,路径);
System.out.println(“Patk-take:+pathtake.toString());
返回最小值;
}
要保留所采用路径的记录,只需将ArrayList添加到递归方法参数中。请注意,我没有检查添加的路径是否为null,您可能应该这样做

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;
}
private int minSumPath(SolutionNode当前,ArrayList路径){
如果(当前==null)
返回0;
int sum=current.getValue();
int left_sum=minSumPath(current.childLeft);
int right_sum=minSumPath(current.childRight);

如果(left_sum而不是在递归方法中返回int,则应该返回一个类,该类保存所传递的节点的sum和字符串

此代码应适用于您:

 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;
}
公共类编号{
私人整数和;
私有字符串str;
//建造师
公共编号(整数和,字符串str){
这个。sum=sum;
str=str;
}
公共无效添加(int sum2){
sum+=sum2;
如果(!str.equals(“”)
str=str+“+”+sum2;
else if(str.equals(“”)
str=”“+sum2;
}
//访问者
公共字符串getStr(){
返回此.str;
}
公共整数getSum(){
返回此.sum;
}
//修饰语
公共void setStr(字符串newStr){
this.str=newStr;
}
公共无效集合(int newSum){
this.sum=newSum;
}
}
//函数来计算最小和路径
//它只返回最小和路径上节点的值之和
minSumPath数(解决方案节点当前){
编号tr1=新编号(0,“”);
如果(当前==null){
返回tr1;
}
int sum=current.getValue();
int left_sum=minSumPath(current.childLeft).sum;
int right_sum=minSumPath(current.childRight).sum;

如果(左)和谢谢它工作非常艰难,我在我的主程序中做了一些改变,但无论如何谢谢你。