简体   繁体   English

如何找到不平衡AVL树的类型?

[英]how to find the type of unbalanced AVL tree?

我已经编写了AVL树代码,但是如何编写代码来确定我的树是否不平衡,并且找到不平衡类型,即“左偏左”,“左偏右”,“右偏右”和“右偏右”?

You can do a normal DFS traversal and at each node find the max height of the left subtree and max height of the right subtree . 您可以进行正常的DFS traversal并在每个节点处找到max height of the left subtree max height of the right subtree

Then the tree is balanced if abs(height_left - height_right) <= 1 for all nodes. 如果所有节点的abs(height_left - height_right) <= 1 ,则树是平衡的。

To find the type of unbalanced tree (by which i think you mean the type of rotation that is requires to fix the tree), you can use left and right child pointers to deduce the type of rotation is required. 要找到不平衡树的类型(我认为这是指修复树所需的旋转类型),可以使用左右子指针来推导需要的旋转类型。

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

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