AVL Tree节点平衡因子
为了计算 AVL 树中节点的平衡因子,我们需要找到其左子树的高度和右子树的高度。然后我们用左子树的高度减去右子树的高度:
balancefactor = leftsubtreeheigh - rightsubtreeheight
我的问题是:如何计算左子树或右子树的高度?
例如,在给定的图中1,根节点 40 的左子树的高度为 4,而 40 的右子树的高度为 2,因此高度是 2。
我如何在 C++ 中做到这一点?我不想使用递归,因为它很混乱。使用显式堆栈而不是递归更容易理解。
1 该人物已从 imgur 服务器中删除。
To calculate the balance factor of a node in an AVL tree we need to find the height of its left subtree and the height of its right subtree. Then we subtract the height of right subtree from the height of its left subtree:
balancefactor = leftsubtreeheigh - rightsubtreeheight
My question is: How do I calculate the height of the left subtree or the right subtree?
For example in the given figure1 the height of the left subtree of the root node 40 is 4 and the height of the right subtree of 40 is 2 so the difference of the heights is 2.
How do I do this in C++? I don't want to use recursion because it's very confusing. Using an explicit stack instead of recursion is much more understandable.
1 The figure has been deleted from the imgur servers.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

节点的深度比其最深子节点的深度多 1。
您可以通过递归非常简单地完成此操作。
The depth of a node is 1 more then the depth of it's deepest child.
You can do this quite simply with recursion.