gpt4 book ai didi

java - 打印树的最小和路径

转载 作者:行者123 更新时间:2023-11-29 04:18:21 25 4
gpt4 key购买 nike

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

我的树类是这样的:

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求和,我想把这个过程打印出来。任何想法我该怎么做?我将不胜感激任何帮助。

提前致谢。

最佳答案

不是在递归方法中返回一个 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;
}

关于java - 打印树的最小和路径,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50907440/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com