2

B Tree is self balancing tree like AVL tree. HERE we can see how left and right rotations are used to keep AVL tree balanced.

And HERE is a link which explains insertion in B tree. This insertion technique does not involve any rotations, if I am not wrong, to keep tree balanced. And therefore it looks simpler.

Question: Is there any similar (or any other technique without using rotations) to keep avl tree balanced ?

templatetypedef
  • 362,284
  • 104
  • 897
  • 1,065
Andy897
  • 6,915
  • 11
  • 51
  • 86
  • 2
    B tree is not a binary tree. – amit Feb 23 '15 at 19:00
  • (In the example at geeksforgeeks (2nd [HERE](http://www.geeksforgeeks.org/b-tree-set-1-insert-2/), the diagram "after insertion of 90" seems out of whack.) There is [2-3 tree](http://en.wikipedia.org/wiki/2%E2%80%933_tree) - have a look at deletions there and in B trees, too. – greybeard Feb 23 '15 at 19:45
  • @greybeard They are not binary trees. – amit Feb 23 '15 at 20:11
  • Define "looks simpler". B-trees are good at one kind of problems (spinning disk storage being slow) that AVL trees are not good for, and AVL and other binary tree balancing techniques have their own advantages (say, for implementing in-memory sorted sets or maps without wasting memory). What is the intended application? – tucuxi Oct 26 '21 at 09:09

2 Answers2

5

The answer is... yes and no.

B-trees don't need to perform rotations because they have some slack with how many different keys they can pack into a node. As you add more and more keys into a B-tree, you can avoid the tree becoming lopsided by absorbing those keys into the nodes themselves.

Binary trees don't have this luxury. If you insert a key into a binary tree, it will increase the height of some branch in that tree by 1 in all cases because that key needs to go into its own node. Rotations combat the overall growth of the tree by ensuring that if certain branches grow too much, that height is shuffled into the rest of the tree.

Most balanced BSTs have some sort of rebalancing strategy that involves rotations, but not all do. One notable example of a strategy that doesn't directly involve rotations is the scapegoat tree, which rebalances by tearing huge subtrees out of the master tree, optimally rebuilding them, then gluing the subtree back into the main tree. This approach doesn't technically involve any rotations and is a pretty clean way to implement a balanced tree.

That said - the most space-efficient implementations of scapegoat trees do indeed use rotations to convert an imbalanced tree into a perfectly balanced one. You don't have to use rotations to do this, though if space is short it's probably the best way to do so.

Hope this helps!

templatetypedef
  • 362,284
  • 104
  • 897
  • 1,065
  • Hi Templatetypedef, you have been of great help. Just one last thing, in the example here - http://www.geeksforgeeks.org/b-tree-set-1-insert-2/ ... I am not able to understand the way "90" is inserted. Can you kindly have a look at it. – Andy897 Feb 24 '15 at 06:05
0

Rotations can be made simple (if you need only simplicity).

If the insertion traffic is left, the balance -1 is the red-light.

If the insertion traffic is right, the balance 1 is the red-light.

This is a (simplified) coarse-graining (2-adic rounding) of the normalized fundamental AVL balance:

{left,even,right} ~ {low,even,high} ~ {green,green,red}

Walk the insertion route and rotate every red-light (before the insertion). If the next light is green, you can just rotate the red-light 1 or 2 times. You may have to re-balance the next subtrees before each rotation, because inner subtrees are invariant. This is simple, but it takes a very long time. You have to move down the green-light before each rotation. You can always move down the green-light to the root, and you can rotate the tree-top to generate a new green-light.

The red-light rotations naturally move down the green-light.

At this point, you have only the green-lights for the insertion.

The cost structure of this naive method is topologically simplified as

df(h)/dh=∫f(h)dh

such as sin(h),sinh(h),etc.

Jean-François Fabre
  • 137,073
  • 23
  • 153
  • 219