Recalculation of Balance Factor in AVL Tree

戏子无情 提交于 2019-12-08 07:52:48

问题


After performing a rotation to balance an AVL tree, immediately after an insertion, how can I change the balance factor of all the parent nodes (appropriately, by -1 or 1)?

Each node of the AVL tree has the following structure:

typedef struct _avlTree
{
 nutbolt part;
 int balanceFactor;
 struct _avlTree *left,*right;
} *avlTree;

I have set the balance factor as per the definition given on Wikipedia.

Do I need to have a pointer to the parent node in each node?


回答1:


You either need a parent pointer for each node, which will need modification too whenever you change the tree structure. Or you need to keep track of all visited nodes beginning from the root, either automatically by the recursion or manually in an array if you have an iterative approach.

You shouldn't miss this for an in-depth study of the topic:

http://www.stanford.edu/~blp/avl/




回答2:


Maybe have a look at AVL C Library for inspiration?



来源:https://stackoverflow.com/questions/3915350/recalculation-of-balance-factor-in-avl-tree

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!