Recalculation of Balance Factor in AVL Tree
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 d开发者_开发知识库efinition given on Wikipedia.
Do I need to have a pointer to the parent node in each node?
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/
Maybe have a look at AVL C Library for inspiration?
精彩评论