How do you calculate the equilibrium factor for the AVL tree?

Asked 2 years ago, Updated 2 years ago, 44 views

           [+2]                            [-2]
            7                                3
          /   \                            /   \
   [+2]  6     9  [0]                [0]  2     5  [-2]
        /                                        \                                    
 [+1]  3                                          9  [-1]
      /                                            \
[0]  1                                             10  [0]

       (Tree 1)                           (Tree 2)

It represents the equilibrium argument of each node in a binary tree. I don't understand why you can get the equilibrium argument by subtracting the height of the right subtree from the height of the left subtree.

Doesn't the level of the tree start with 1 from the root node? Does the leaf node start with 1 only when calculating the equilibrium argument?

algorithm

2022-09-22 18:13

1 Answers

The height in the tree is also expressed as Depth and Level, which means that it increases by 1 each time it goes down from the reference node to the child node. In this case, child can be left/right. So you can think of it as the depth of the tree.

Since the leaf node has no child, the depth (height) of both subtrees is of course zero.

If you look at the (6) node in (1), the depth from the left subtree to (1) is 2. Because the right side is zero, the equilibrium factor is +2. Even if the subtree is divided into several branches, you can look at it based on the deepest node.

If you look at the root node in (Tree 1), the depth from the left subtree to (1) is 3, and the depth from the right subtree to (9) is 1, so 3 - 1 = 2 is the equilibrium factor.


2022-09-22 18:13

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.