简体   繁体   English

如何在树形图中找到总和? Java的

[英]How to find the sum in a Treemap? Java

import java.util.Scanner;

public class BinaryTree {

private int info;
private BinaryTree left;
private BinaryTree right;
private int sum;

public BinaryTree()
{
    left = null;
    right = null;
}
// This is a second constructor. 
// It can tell the difference by parameter.
public BinaryTree(int theInfo)
{
    Scanner sc = new Scanner(System.in);
    int intNum;
    String s;

    info = theInfo;

    System.out.print("Does the node " + info + " have a left child (y or n)? ");
    s = sc.next();
    if (s.equals("y"))
    {
        System.out.print ("What value should go in the left child node? ");
        intNum = sc.nextInt();
        left = new BinaryTree(intNum);
    }
    System.out.print("Does the node " + info + " have a right child (y or n)? ");
    s = sc.next();
    if (s.equals("y"))
    {
        System.out.print ("What value should go in the right child node? ");
        intNum = sc.nextInt();
        right = new BinaryTree(intNum);
    }
}

public void TraverseNLR()
{

    System.out.print(info + " ");
    if (left != null)
    {
        left.TraverseNLR();
    }
    if (right != null)
    {
        right.TraverseNLR();
    }
}

public void TraverseLNR()
{
    if (left != null)
    {
        left.TraverseLNR();
    }
    System.out.print(info + " ");
    if (right != null)
    {
        right.TraverseLNR();
    }
}

public void TraverseLRN()
{

    if (left != null)
    {
        left.TraverseLRN();
    }
    if (right != null)
    {
        right.TraverseLRN();
    }
    System.out.print(info + " ");
}
/* QUESTION FUNCTION HERE */
public int sumValues()
{ 
    System.out.print(info + " ");
    sum += info;
    if (left != null)
    {
        sum += info;
        left.TraverseNLR();
    }

    if (right != null)
    {
        sum += info;
        right.TraverseNLR();
    }
    return sum;
 }
} 

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

I'm having trouble finding the sum of a treemap. 我在查找树图总和时遇到了麻烦。 For example I will input: 例如,我将输入:

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

and get the sum is 1200 instead of the true answer 1300. There must be something that I'm not understanding recursively. 得出的总和是1200,而不是真正的答案1300。一定有我不递归理解的内容。 How could it possibly skip the 100 from the sum? 它怎么可能从总数中跳过100? I made the sum variable globally so it would remember everything I'd assign it. 我全局设置了sum变量,因此它会记住我要为其分配的所有内容。 Does anyone have any hints or point me in a direction of summing up an entire Treemap? 有没有人有任何提示或指向我总结整个树图的方向?

I don't understand what the purpose of the TraverseXXX() functions are. 我不明白TraverseXXX()函数的用途是什么。 You should be able to just get rid of them, and rewrite your sumValues() function using recursion: 您应该能够摆脱它们,并使用递归重写您的sumValues()函数:

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

Using this code for sumValues() I got the correct output of 1300: 将此代码用于sumValues()我得到了1300的正确输出:

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 1300

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM