如何在树形图中求和?JAVA

如何在树形图中求和?JAVA,java,Java,我找不到树形图的和。例如,我将输入: import java.util.Scanner; public class BinaryTester { public static void main(String[] args) { BinaryTree myTree; Scanner sc = new Scanner(System.in); int intNum; System.out.print("What value should go in the ro

我找不到树形图的和。例如,我将输入:

import java.util.Scanner;

public class BinaryTester {

public static void main(String[] args) 
{
    BinaryTree myTree;
    Scanner sc = new Scanner(System.in);
    int intNum;

    System.out.print("What value should go in the root? ");
    intNum = sc.nextInt();
    myTree = new BinaryTree(intNum);
    //myTree.TraverseNLR();
    //System.out.println();
    //myTree.TraverseLNR();
    //System.out.println();
    //myTree.TraverseLRN();
    //System.out.println();
    System.out.println("Sum is " + myTree.sumValues());
    //myTree.sumValues();
  }
}

得到的和是1200,而不是真正的答案1300。一定有什么我不明白的。它怎么可能从总数中跳过100呢?我全局地创建了sum变量,这样它就可以记住我分配给它的所有内容。有没有人能给我一些提示或指引我如何总结整个树形图

我不明白traversexx()函数的用途是什么。您应该能够摆脱它们,并使用递归重写sumValues()函数:

What value should go in the root? 400
Does the node 400 have a left child (y or n)? y  
What value should go in the left child node? 100
Does the node 100 have a left child (y or n)? n
Does the node 100 have a right child (y or n)? y
What value should go in the right child node? 300
Does the node 300 have a left child (y or n)? n 
Does the node 300 have a right child (y or n)? n 
Does the node 400 have a right child (y or n)? y
What value should go in the right child node? 500
Does the node 500 have a left child (y or n)? n
Does the node 500 have a right child (y or n)? n
400 100 300 500 Sum is 1200
将此代码用于
sumValues()
我得到了1300的正确输出:

public int sumValues() {
    System.out.println(info + " ");
    int sum = info; // account for current node

    if (left != null)
        sum += left.sumValues(); // add sum of left subtree

    if (right != null)
        sum += right.sumValues(); // add sum of right subtree

    return sum;
}

我不明白Traversexx()函数的用途是什么。您应该能够摆脱它们,并使用递归重写sumValues()函数:

What value should go in the root? 400
Does the node 400 have a left child (y or n)? y  
What value should go in the left child node? 100
Does the node 100 have a left child (y or n)? n
Does the node 100 have a right child (y or n)? y
What value should go in the right child node? 300
Does the node 300 have a left child (y or n)? n 
Does the node 300 have a right child (y or n)? n 
Does the node 400 have a right child (y or n)? y
What value should go in the right child node? 500
Does the node 500 have a left child (y or n)? n
Does the node 500 have a right child (y or n)? n
400 100 300 500 Sum is 1200
将此代码用于
sumValues()
我得到了1300的正确输出:

public int sumValues() {
    System.out.println(info + " ");
    int sum = info; // account for current node

    if (left != null)
        sum += left.sumValues(); // add sum of left subtree

    if (right != null)
        sum += right.sumValues(); // add sum of right subtree

    return sum;
}

非常感谢。是的,我不小心忘了放.sumValues(),而是把.transversal()放在那里了。谢谢!是的,我不小心忘记放置.sumValues(),而是将.transversal()放在那里。我在代码中没有看到任何
Treemap
,因此您可能需要更改标题。(您正在处理的结构是一个“二叉树”。
TreeMap
是一个Java结构,它使用一种特殊类型的树(即红黑树)来实现映射。它不是计算机科学中通常用于任何类型数据结构的名称。如果您在维基百科中查找“TreeMap”,您会发现一些完全不相关的内容。)我在您的代码中没有看到任何
Treemap
,因此您可能需要更改标题。(您正在处理的结构是一个“二叉树”。
TreeMap
是一个Java结构,它使用一种特殊类型的树(即红黑树)来实现映射。它不是计算机科学中通常用于任何类型数据结构的名称。如果您在维基百科中查找“TreeMap”,您会发现一些完全不相关的内容。)