BST设置每个指向节点的高度
BST setting height at each pointing node
我正在尝试实现递归方法,以递归方式设置每个节点的高度。部分解决方案已经实现,但我不完全确定在哪里可以减少高度并检查Order遍历中的特定节点是否完成。我的程序基于此实现:http://visualgo.net/bst.html
感谢
如果这里的高度表示节点所在的树的级别,那么使用全局变量会产生非常奇怪的结果。我也承认,我不完全确定你在用变量u
做什么。
也就是说,我认为你应该接受这样的东西:
public void setHeight(struct node *r, int h = -1) {
// pointer pointing to null, return
if(r == NULL) {
return;
}
h++; // increment height
r.height = h; // set update height to a current node
setHeight(r ->u.lc, h); // traverse the list pointing to the left child
visit(r) // visit pointing node
setHeight(r ->u.rc, h); // visit right child of the node
}
编辑:我还没有发表评论的资格,所以我只能用编辑来回应@ProgLearner,您不需要单独的变量u
,因为您的节点指针是一个函数参数,因此每次调用函数时都会有一个新的变量。类似地,正如Jonathan Mee所说,h
变量不需要外部初始化,因为它也是函数的本地变量。在没有提供任何初始值的情况下(比如在根上调用它时),它将默认为-1。
相关文章:
- 反向给定链表中的K节点
- 如果我只是不访问queue_front节点的子节点,而是将它们推到队列中呢?还是BFS吗
- Boost Graph Library,修复节点大小
- C++A*算法并不总是在路径中具有目标节点
- 如何找到2个单链表的公共节点
- 计算每个节点的树高,帮助我解释这个代码解决方案
- 为什么我的删除节点函数实际上没有删除节点?
- 我们可以删除链表中静态内存中的节点吗
- 如何在pugixml中获取节点的内部XML
- 为什么我们要为avl树实现返回一个指向节点的指针,而不是void函数
- C++RapidXml-使用first_node()遍历以修改XML文件中节点的值
- 为什么C++对链表中的下一个节点使用指针,而像 C# 或 Java 这样的语言只使用类 Node 的名称?
- C++17 - 使用自定义分配器的节点提取/重新插入 - 适用于 clang++/libc++,但不适用于 libstd
- 节点 *temp; 和节点 *tmp = 新节点之间的差异
- 接受来自键盘的树节点以确定其高度
- C ,二进制树的高度,而不是检查我的子树是否为空,而是在检查我的子树是否是叶子节点.抛出分割故障
- 找到任何节点的BST高度
- BST设置每个指向节点的高度
- 在一棵有191个节点且高度为9的二叉树中找到一个键所需的最大比较次数是多少?
- 二叉树中特定节点的高度