Write a non recursive function to insert into an avl tree

This is where you really need to start drawing pictures, but I will assert without proof that if the next step is in the same direction as the step into a larger subtree, then a single rotation will be needed.

pseudo code for avl tree

To my eye, abstracting out the direction, and having an array of pointers instead of two explicit pointers makes for more elegant code. I suspect that the deletion code would be very symetrical to this code, but I haven't worked through it yet, so I cannot say for sure.

Avl tree insertion algorithm

Left-Right Case : — x is the right child of y and y is the left child of z. The growth is perfectly legal, it just needs to be recorded. As well as a direction, we need a way to store the current 'balance' at any node in the tree. Because this code will eventually hopefully live inside the Linux kernel, I wanted it to be non-recursive as recursive code doesn't sit will in the context of the kernel. This path may not actually start with an unbalanced node. The other problem I have with the traditional approach is the use of "left" and "right" pointers and the code duplication this results in. Towards AVL Insertion We know the result that at most one rotation possibly double is needed after an insertion, we want to find out where that rotation must take place. It is important to be able to find the 'other' direction given a direction in a variable. To check whether it is Right Right case or not, compare the newly inserted key with the key in right subtree root. Well, it starts to get tricky.

Left-Left Case : — x is the left child of y and y is the left child of z. That takes O nwhile if we keep the node balanced in every insertion or deletion, we could have O log n.

This fact, that the need to rotate only once is not made apparent in the algorithm, is the particular problem that I see with the recursive approach.

Write a non recursive function to insert into an avl tree

In fact, I liked what I came up with so much that I thought I would share it. This check happens at every level in the recursion, however we know that it will only be needed at one level. In all of the cases, we only need to re-balance the subtree rooted with z and the complete tree becomes balanced as the height of subtree After appropriate rotations rooted with z becomes same as it was before insertion. I will call them 2-point rotation and 3-point rotation. Node y is the child of z and x be the grandchild of z. To find out if a node is on the tree or not, you will have to visit every node when the tree is unbalanced. In AVL Tree, the heights of child subtrees at any node differ by at most 1. The other is a rotation of three nodes and is sometimes called a double rotation, though you could argue that it is only one-and-a-half times the first. Most of the BST operations e. Well, it starts to get tricky. The increase in height of node x can only be caused by a height increase in node y and in node z Let's figure out what are the value of all the? To check whether it is left left case or not, compare the newly inserted key with the key in left subtree root. There are often pairs of very similar functions, one that handles a "left" case and one that handles a "right" case.

They won't be affected at all. So I will discuss the details of the first tri-node reconstration operation only Rebalancing an AVL Tree - in general for all 4 configurations How to use a tri-node reconfiguration operation to re-balance an AVL tree: If after an insertion, the AVL tree is still balanced: We are done : If it ain't broken, don't fix it!!!

avl tree creator

We will assume a signed bitfield and use '-1' to say 'neither subtree is longer'. I'll just take a moment at this point to say that I don't like the term "double rotation" either maybe I was born grumpy It is not perfectly balanced, but there is a good limit on how unbalanced it can be.

avl tree insertion tutorial
Rated 7/10 based on 108 review
Download
C Programing: Non