实现递归 Void 函数(查找二叉搜索树的高度)
Posted
技术标签:
【中文标题】实现递归 Void 函数(查找二叉搜索树的高度)【英文标题】:Implementing a recursive Void function (Finding height of Binary Search Tree) 【发布时间】:2020-02-17 21:11:33 【问题描述】:我需要实现一个 void 函数来计算二叉树中每个节点的高度并将其存储在每个节点中。我在网上找到了一些本质上是递归的解决方案,但它们返回 int。示例包括 (https://www.geeksforgeeks.org/write-a-c-program-to-find-the-maximum-depth-or-height-of-a-tree/)。模型答案的区别,除了它不是一个空函数外,它也不存储每个节点的高度。
这是我对解决方案的尝试,但我似乎无法让代码工作,也无法重新调整模型答案以递归地应用于 void 函数。当我在帮助程序代码中运行我的代码进行测试时,它甚至没有显示任何输出。
void computeHeight(Node *n)
Node* ltraverser = n;
Node* rtraverser = n;
int lheight = 0;
int rheight =0;
if (n == NULL)
n->height = 0;
while (ltraverser->left != NULL)
ltraverser = ltraverser->left;
lheight += 1;
while (rtraverser->right != NULL)
rtraverser = rtraverser->right;
lheight += 1;
if (lheight > rheight)
n->height = lheight;
else
n->height = rheight;
computeHeight(n->left);
computeHeight(n->right);
供参考:
The starter code below defines a class called "Node" that has two child pointers ("left" , "right") and an integer "height" member variable. There is also a constructor Node() that initializes the children to nullptr and the height to -1.
/*
The height of a node is the number of edges in
its longest chain of descendants.
Implement computeHeight to compute the height
of the subtree rooted at the node n. Note that
this function does not return a value. You should
store the calculated height in that node's own
height member variable. Your function should also
do the same for EVERY node in the subtree rooted
at the current node. (This naturally lends itself
to a recursive solution!)
Assume that the following includes have already been
provided. You should not need any other includes
than these.
#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <string>
You have also the following class Node already defined.
You cannot change this class definition, so it is
shown here in a comment for your reference only:
class Node
public:
int height; // to be set by computeHeight()
Node *left, *right;
Node() height = -1; left = right = nullptr;
~Node()
delete left;
left = nullptr;
delete right;
right = nullptr;
;
*/
用于测试代码
// This function prints the tree in a nested linear format.
void printTree(const Node *n)
if (!n) return;
std::cout << n->height << "(";
printTree(n->left);
std::cout << ")(";
printTree(n->right);
std::cout << ")";
Node *n = new Node();
n->left = new Node();
n->right = new Node();
n->right->left = new Node();
n->right->right = new Node();
n->right->right->right = new Node();
computeHeight(n);
printTree(n);
std::cout << std::endl << std::endl;
printTreeVertical(n);
delete n;
n = nullptr;
return 0;
【问题讨论】:
你希望如何从函数中得到结果?如果它没有返回任何内容,则需要一个 out 参数。 我相信作业指定我应该将计算存储在 n->height 中,但我和你一样被迷惑了。 @foreknownas_463035818 不相关。我查看了 Geeks for Geeks 链接以获取您所基于的代码示例。请谨慎使用#include <bits/stdc++.h>
和using namespace std;
。像示例一样将两者结合起来时要更加小心。
@这个代码sn-p if (n == NULL) n->height = 0; 调用未定义的行为。
在根节点或任何其他节点都是一样的:你必须从当前节点找到深度。找到它后,将此信息传递给“父”节点。因为父节点要求其子节点完成这项工作,所以每个子节点都将信息存储在传递的参数中。存储 l-depth 和 r-depth 是一个附加信息,在以后的查询中很有用。
【参考方案1】:
不是返回节点高度,而是在左右节点上递归调用computeHeight,然后将最大高度存储在节点结构中。
#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <string>
#include <algorithm>
class Node
public:
int height;
Node *left, *right;
Node() height = -1; left = right = nullptr;
~Node()
delete left;
left = nullptr;
delete right;
right = nullptr;
;
void computeHeight(Node *node)
if (node == nullptr)
return;
computeHeight(node->left);
computeHeight(node->right);
int leftHeight = -1;
int rightHeight = -1;
if (node->left != nullptr)
leftHeight = node->left->height;
if (node->right != nullptr)
rightHeight = node->right->height;
node->height = std::max(leftHeight, rightHeight) + 1;
void printNode(Node *n, int level = 0)
if (n == nullptr)
return;
std::cout << std::string(level * 2, ' ') << "Height = " << n->height << "\n";
printNode(n->left, level + 1);
printNode(n->right, level + 1);
int main()
Node *n = new Node();
n->left = new Node();
n->right = new Node();
n->right->left = new Node();
n->right->right = new Node();
n->right->right->right = new Node();
computeHeight(n);
printNode(n);
【讨论】:
太棒了!漂亮而整洁的解决方案。谢谢【参考方案2】:您的错误在以下部分,因此您的程序退出而不显示错误
if (n == NULL)
n->height = 0;
当 n 为 NULL 时;你不应该尝试访问 n->height。如下替换它,您的代码将起作用:
if (n == NULL)
return;
另外,正如另一个答案所提到的,当您想递归计算高度时,您不需要 while 循环,只需使用以下递归公式:
Height(n) = 1 + max(Height(n->left), Height(n->right))
此外,出于一致性原因,通常将 NULL 子树的高度定义为 -1。这允许递归公式正常工作。
忠告:为了调试任何程序,一个简单的方法是在函数调用和/或某些行之前和之后打印消息。这样,通过检查哪些消息未打印,您可以快速查明哪些函数/行导致了问题,然后进行调查。
【讨论】:
以上是关于实现递归 Void 函数(查找二叉搜索树的高度)的主要内容,如果未能解决你的问题,请参考以下文章