data-structuresbinary-search-treeavl-treetreenodesplay-tree

Why is splay tree in textbook different than mine?


In Data Structures and Algorithm Analysis in C++ (4th Edition) by Mark Allen Weiss, on page 162 at figure 4.50, the book describes how splaying the left most child of tree with only left children would ultimately look like.

Where I am confused is how the book gets from step 2 to step 3. Here are the steps highlighted on page 162 at figure 4.50:

enter image description here

Whereas my thirds step looks like this:

               7                                                                                          
              /                                                  
             6                                                   
            /                                                    
           1                    
            \                                                  
             3                                                  
            / \
           2   4
                \
                 5

And my fourth and final step like this:

    1
     \
      4
     / \
    3   6
   /   / \
  2    5  7

I am confused as to how the book is balancing the tree. For me, when 1 surpassed 4 the bottom of the tree looked like this:

  ...
 /
1
 \
  2
   \
    3
     \
      4

My logic was that the root where the balance would occur would be 2. Then you would do a left rotation and the bottom of the tree would look like this:

  ...
 /
1
 \
  3
 / \
2   4

Am I doing something wrong or am I just doing it in a different but equally valid to the book way? I am also confused because the book's final tree is imbalanced from the root of 6 whereas my root of 4 doesn't have an imbalance.


Solution

  • This is mostly the "Zig-zig" case till upwards, so each time you do a right rotation on the node's grandparent followed by a right rotation on the parent.

    Take example:

               5                                                                                          
              /                                                  
             4                                                   
            /                                                    
           3                    
          /                                                  
         2
        /
       1
    

    If you want to splay 1, you rotate right around 3 then 2, resulting in:

            5                                                                                          
           /                                                  
          4                                                   
         /                                                    
        1
         \
          2
           \
            3                   
    

    Since it is Zig-Zig case again, we rotate around 5 then 4, resulting:

       1
        \
         4
        / \
       2   5
        \
         3     
    

    So you keep doing this until you have 1 as root. There are 3 cases, Zig-Zig, Zig and Zig-Zag. Here is a tool that visualizes the whole concept it will be helpful I believe.