Java 创建给定二进制树的递归方法
我正在准备几个小时后的一个测试,我正在复习一些练习题,但我确实遇到了与递归相关的问题。我想知道是否有人能教我怎么做 使用下面的BinaryTree类,创建以下递归方法: sum–返回二进制树中所有值的总和。 countGreaterThanint–返回值大于指定整数的节点数。 isValidSearchTree–如果二叉树是有效的搜索树,则返回true,否则返回false。 我得到了如何创建这些函数的基本前提,例如,对于求和函数,它通常是这样的:Java 创建给定二进制树的递归方法,java,Java,我正在准备几个小时后的一个测试,我正在复习一些练习题,但我确实遇到了与递归相关的问题。我想知道是否有人能教我怎么做 使用下面的BinaryTree类,创建以下递归方法: sum–返回二进制树中所有值的总和。 countGreaterThanint–返回值大于指定整数的节点数。 isValidSearchTree–如果二叉树是有效的搜索树,则返回true,否则返回false。 我得到了如何创建这些函数的基本前提,例如,对于求和函数,它通常是这样的: int sum = 0 for(int i; i
int sum = 0
for(int i; i<binaryTree.size(); i++){
sum += binaryTree[i]
}
如果你能提供一个递归的尝试,你做的总和,我将能够帮助你更容易。一旦你提供了一个例子,我将用更全面的内容更新这个答案 作为一般提示/提示,在处理递归和二叉树时: 把它画出来,找出可能的解决办法。这可以帮助您可视化需要编写的代码。 对于任何给定节点,您都希望访问其所有子节点。这意味着如果它只有一个左边或右边的孩子,你可以访问那个孩子,如果它有两个左边和右边的孩子,你可以访问两个,如果你没有孩子,你可以终止,这是一个相当大的基本情况提示。
首先,我认为你有太多的问题,但是,我将通过递归求和函数,解释如何递归地解决这样的问题 这些算法似乎都需要,所以我建议你也看看链接的视频 递归最巧妙的地方是允许我们只考虑每个特定节点必须做什么。对于sum示例,这意味着返回它存储的值,加上它的子项的递归和
public int exampleSum(){
// Start with the data we have.
int sum = this.getData();
// Then get the data we need (from each child, which can be done recursively).
if (this.getLeftChild() != null) {
sum += this.getLeftChild().exampleSum();
}
// Do the same for the right node.
return sum;
}
这相当于通过每个节点,得到每个子树的总和
想象一下,一群人站在一棵二叉树的形状中,每个人都有一个“父母”和0到2个“孩子”,他们都持有一个数字
我们从最上面开始,第一个人问他们的孩子求和,他们的孩子问他们的孩子求和,等等
每个人把孩子给他们的号码加在自己的号码上,然后交给父母
这两种特殊情况是,当一个人没有孩子时,他们会停下来,只把自己的号码交给父母,而“根”节点则会将自己的号码交给最初要求他们的任何函数
我建议你画一棵二叉搜索树,然后自己完成这个算法
我希望这是有帮助的,如果它是你能喜欢这个答案
以后我建议每次问一个问题。祝你考试顺利 你应该研究递归的威力,但我已经编写了你要求的方法,所有这些方法都非常有效:
/*
* INPUT: Root node
* OUTPUT: Sum of the data of all nodes of tree
*/
public void sum(TreeNode node)
{
if(node == null)
return;
sum(node.leftNode); // recursive call to left subtree of each node
sum += node.data;
sum(node.rightNode); // recursive call to right subtree of each node
}
/*
* INPUT: Root node , threshold value
* OUTPUT: sum of all node's data, that rae greater than "value"
*/
public void countGreaterThan(TreeNode node, int value)
{
if(node == null)
return;
countGreaterThan(node.leftNode,value);
if( node.data > value) // only adds is node.data is greater than given value
sum += node.data;
countGreaterThan(node.rightNode,value);
}
/*
* INPUT: nothing
* OUTPUT: call to its helper function, taking MIN, MAX, and root as input
*/
public boolean isBST()
{
return isBSThelper(root, Integer.MIN_VALUE, Integer.MAX_VALUE);
}
public boolean isBSThelper(TreeNode node, int min, int max)
{
if (node == null) //empty tree is always a BST
return true;
if (node.data < min || node.data > max) //if node breaks the min/max condition
return false;
// recursive call to left subtree and right subtree
return (isBSThelper(node.leftNode, min, node.data-1) && isBSThelper(node.rightNode, node.data+1, max));
}
对于方法isValidSearchTree,什么定义了一个有效的搜索树?所以你得到了你想要的或者你需要更多的澄清?
/*
* INPUT: Root node
* OUTPUT: Sum of the data of all nodes of tree
*/
public void sum(TreeNode node)
{
if(node == null)
return;
sum(node.leftNode); // recursive call to left subtree of each node
sum += node.data;
sum(node.rightNode); // recursive call to right subtree of each node
}
/*
* INPUT: Root node , threshold value
* OUTPUT: sum of all node's data, that rae greater than "value"
*/
public void countGreaterThan(TreeNode node, int value)
{
if(node == null)
return;
countGreaterThan(node.leftNode,value);
if( node.data > value) // only adds is node.data is greater than given value
sum += node.data;
countGreaterThan(node.rightNode,value);
}
/*
* INPUT: nothing
* OUTPUT: call to its helper function, taking MIN, MAX, and root as input
*/
public boolean isBST()
{
return isBSThelper(root, Integer.MIN_VALUE, Integer.MAX_VALUE);
}
public boolean isBSThelper(TreeNode node, int min, int max)
{
if (node == null) //empty tree is always a BST
return true;
if (node.data < min || node.data > max) //if node breaks the min/max condition
return false;
// recursive call to left subtree and right subtree
return (isBSThelper(node.leftNode, min, node.data-1) && isBSThelper(node.rightNode, node.data+1, max));
}