简体   繁体   English

AVL树插入

[英]AVL Tree insertion

How do I calculate the balance factor for a particular node, when I am recursively calling an insert function for adding a node to an AVL tree. 当我递归调用用于将节点添加到AVL树中的插入函数时,如何计算特定节点的平衡因子。 I haven't started on the rotation logic. 我尚未开始旋转逻辑。 I simply want to calculate the balance factors. 我只想计算平衡因子。

In my current attempt, I am forced to store heights of left & right subtrees as I can't find the balance factor without them. 在当前尝试中,由于没有左右平衡树,我不得不存储左右子树的高度。

typedef struct _avlTree
{
 int num;
 int balFactor;
 int height[2];  // left & right subtree heights
 struct _avlTree *left,*right;
} *avlTree;


int avlAdd(avlTree a,avlTree aNew)
{
                ...
  if(a->left == NULL)   // left subtree insertion case
  {
   a->left = aNew;
   return(1); 
  }
  else
  {
   a->height[0] = avlAdd(a->left,aNew);
   a->balFactor = a->height[0] - a->height[1];
   return( (a->height[0]>a->height[1]) ? (a->height[0]+1) : (a->height[1]+1) );
  }
                ...
}

The balance factor is the difference in heights between the right and left subtrees of a node. 平衡因子是节点左右子树之间的高度差。

When creating a new node, initialize the balance factor to zero since it is balanced (it has no subtrees). 创建新节点时,将平衡因子初始化为零,因为它是平衡的(没有子树)。

If you are inserting a new node to the right, increase the balance factor by 1. 如果要在右侧插入新节点,请将平衡因子增加1。

If you are inserting a new node to the left, decrease the balance factor by 1. 如果要在左侧插入新节点,请将平衡因子减小1。

After rebalancing (rotating), if you increase the height of the subtree at this node, recursively propagate the height increase to the parent node. 重新平衡(旋转)后,如果您在此节点上增加子树的高度,则将高度增加量递归传播到父节点。

Here is a very simple approach. 这是一个非常简单的方法。 If there was a recursive height() function, then balance factor can be computed simply as 如果有一个递归的height()函数,那么平衡因子可以简单地计算为

node->balFactor = height( node->right ) - height( node->left );

This is not the best approach though, since the complexity of this approach is O( h ) where h is the height of the node in the AVL tree. 但是,这并不是最佳方法,因为此方法的复杂度为O( h ) ,其中h是AVL树中node的高度。 For better approach, a bigger discussion is required :) 为了更好的方法,需要进行更大的讨论:)

There are numerous resources on AVL tree in the web, a chosen few are: 网上的AVL树上有很多资源,其中一些是:

  1. http://en.wikipedia.org/wiki/AVL_tree http://en.wikipedia.org/wiki/AVL_tree
  2. C implementation: http://www.stanford.edu/~blp/avl/libavl.html C实现: http//www.stanford.edu/~blp/avl/libavl.html
  3. Animation: http://www.cs.jhu.edu/~goodrich/dsa/trees/avltree.html 动画: http//www.cs.jhu.edu/~goodrich/dsa/trees/avltree.html
  4. Animation: http://www.strille.net/works/media_technology_projects/avl-tree_2001/ 动画: http//www.strille.net/works/media_technology_projects/avl-tree_2001/

BTW, The avlAdd() function looks wrong. 顺便说一句, avlAdd()函数看起来不对。 I don't see where aNew->num is compared to a->num . 我看不到aNew->numa->num相比。 Whether to go to left subtree or right subtree must depend on that. 转到左子树还是右子树必须取决于此。 The given code seems to be adding to the left subtree unconditionally . 给定的代码似乎无条件地添加到左子树中。

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

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