2
$\begingroup$

I'm studying AVL Trees in my programming class and we got this exercise dealing with right, left, left-right and right-left rotations as a way to check if we understand the theoretical concept of AVL Trees. We're given the numbers $100,50,25,10,37,32,200$. Creating an AVL Tree until $37$ wasn't that difficult but then I got stuck at balancing out the tree when I insert $32$. The following is my method:

enter image description here

Now I know that there is a conflict at $52$ but since it has 3 nodes (LRL), I don't understand how I should rotate. I think, I should get $37$ as the root node $25$ as it's left child and $52$ as it's right child but I dunno.

$\endgroup$

3 Answers 3

1
$\begingroup$

I think, I should get 37 as the root node 25 as it's left child and 52 as it's right child but I dunno.

You plan is correct.

What you need to do is a left-right rotation as shown in the third column of the table below. That is, a left rotation at node 25 followed by a right rotation at 52. It is symmetric to the right-left rotation as shown in the fourth column and as explained in Wikipedia.

tree rebalancing picture at wikimedia

$\endgroup$
0
$\begingroup$

Before you insert a node, you already know the insertion is forbidden. You can rotate 52 before you insert a new node, because the insertion direction is otherwise wrong (52 has the red light 1). You can check and rotate all the nodes before you insert a new node such that all roads are as clear as possible (all green lights).

This generally means that after two insertions, the global height may increase (amorphous flip-flopping); in other words, every 2 insertion may trigger the extra height compression subroutine; such subroutines are very costly and can be the real main cost (hidden cost).

The flip-flop sequence obeys a very simple horizonal nonlinear wave equation with probablistic short pebble-rain+ripples. The height growth is like a simple dirac delta.

$\endgroup$
1
  • $\begingroup$ Welcome to COMPUTER SCIENCE @SE. Can you provide a reference for this use of (amorphous) flip-flopping? I remember the cost to restore the balance criterion to be "constant" with AVL tree insert. $\endgroup$
    – greybeard
    Oct 24, 2021 at 9:29
-1
$\begingroup$

Balanced AVL tree

1st step : Left-Left rotation. 2nd & 3rd step :Left-Right Rotation. 4th step: Right-Right Rotation. 5th is Final AVL tree.

https://www.tutorialspoint.com/data_structures_algorithms/avl_tree_algorithm.htm

$\endgroup$
6
  • $\begingroup$ Can you explain how did you know in which directions to perform the rotations? $\endgroup$ Mar 8, 2019 at 21:15
  • $\begingroup$ @Yuval Filmus , a link is added for better explanation. $\endgroup$
    – NIKHIL
    Mar 9, 2019 at 9:15
  • $\begingroup$ OK but what happens if that link breaks? We're trying to be a collection of answers: Google already does the "links to answers" thing far better than we ever could. $\endgroup$ Mar 9, 2019 at 11:07
  • $\begingroup$ In that case that child will be added to same level where it was before rotation. $\endgroup$
    – NIKHIL
    Mar 9, 2019 at 11:21
  • $\begingroup$ (In that case that child will be added to same level where it was before rotation. What case, what child? The link D. Richerby was referring to was the hyperlink you planted to stand-in for an explanation.) $\endgroup$
    – greybeard
    Oct 24, 2021 at 9:22

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.