0

I have seen that when a node is deleted from an AVL tree it may require restructuring multiple no times in contrast to inserting which requires only once. Can anyone give me an example of such a case of deletion? Also, I have implemented an AVL Tree and I want to check whether the delete() function works properly. So can you give a sequence of insertions and then deletions that can help me figure out whether my delete() is correct and takes care of all this?

Assume you have an AVL Tree with each node storing a name (string) and you have functions insertAVL(element) and deleteAVL(element).

Karma
  • 1
  • 1
  • 3
  • 9
zed111
  • 200
  • 5
  • 15

2 Answers2

0

Well, both insert and delete can have multiple rotations since you have to work your way up the tree.

For example, add the data set of {5,2,4,3,7,8,10,9} then remove {5}, add {9}, and finally remove {2}. You get the following.

addValue. id=5
└── (1) 5

addValue. id=2
└── (2) 5
    └── (1) 2

addValue. id=4
└── (3) 5 *unbalanced left 2 - right 0*
    └── (2) 2
        └── (1) 4
After left rotation:
└── (3) 5 *unbalanced left 2 - right 0*
    └── (2) 4
        └── (1) 2
After right rotation:
└── (2) 4
    ├── (1) 2
    └── (1) 5

addValue. id=3
└── (3) 4
    ├── (2) 2
    │   └── (1) 3
    └── (1) 5

addValue. id=7
└── (3) 4
    ├── (2) 2
    │   └── (1) 3
    └── (2) 5
        └── (1) 7

addValue. id=8
└── (3) 4
    ├── (2) 2
    │   └── (1) 3
    └── (3) 5 *unbalanced right 2 - left 0*
        └── (2) 7
            └── (1) 8
After left rotation:
└── (3) 4
    ├── (2) 2
    │   └── (1) 3
    └── (2) 7
        ├── (1) 5
        └── (1) 8

addValue. id=10
└── (4) 4
    ├── (2) 2
    │   └── (1) 3
    └── (3) 7
        ├── (1) 5
        └── (2) 8
            └── (1) 10

addValue. id=9
└── (4) 4
    ├── (2) 2
    │   └── (1) 3
    └── (3) 7
        ├── (1) 5
        └── (3) 8 *unbalanced left 0 - right 2*
            └── (2) 10
                └── (1) 9
After right rotation:
└── (5) 4
    ├── (2) 2
    │   └── (1) 3
    └── (4) 7
        ├── (1) 5
        └── (3) 8 *unbalanced right 2 - left 0*
            └── (2) 9
                └── (1) 10
After left rotation:
└── (4) 4
    ├── (2) 2
    │   └── (1) 3
    └── (3) 7
        ├── (1) 5
        └── (2) 9
            ├── (1) 8
            └── (1) 10

removeValue. value=5
└── (4) 4
    ├── (2) 2
    │   └── (1) 3
    └── (3) 7 *unbalanced right 2 - left 0*
        └── (2) 9
            ├── (1) 8
            └── (1) 10
After left rotation:
└── (4) 4
    ├── (2) 2
    │   └── (1) 3
    └── (3) 9
        ├── (2) 7
        │   └── (1) 8
        └── (1) 10

addValue. id=9
└── (5) 4
    ├── (2) 2
    │   └── (1) 3
    └── (4) 9
        ├── (3) 7 *unbalanced right 2 - left 0*
        │   └── (2) 8
        │       └── (1) 9
        └── (1) 10
After left:
└── (4) 4
    ├── (2) 2
    │   └── (1) 3
    └── (3) 9
        ├── (2) 8
        │   ├── (1) 7
        │   └── (1) 9
        └── (1) 10

removeValue. value=2
└── (4) 4 *unbalanced right 3 - left 1*
    ├── (1) 3
    └── (3) 9
        ├── (2) 8
        │   ├── (1) 7
        │   └── (1) 9
        └── (1) 10
After right rotation:
└── (4) 4 *unbalanced right 3 - left 1*
    ├── (1) 3
    └── (3) 8
        ├── (1) 7
        └── (2) 9
            ├── (1) 9
            └── (1) 10
After left rotation:
└── (3) 8
    ├── (2) 4
    │   ├── (1) 3
    │   └── (1) 7
    └── (2) 9
        ├── (1) 9
        └── (1) 10

I have an AVL tree here, if you want to take a look closer.

Justin
  • 4,196
  • 4
  • 24
  • 48
  • I said multiple restructurings not rotations; By restructurings, I mean a complete set of left and right rotation at one location – zed111 Oct 01 '13 at 04:41
  • @zed111 There is no restructuring in an AVL tree beside rotations. In the example above the double (left/right) rotations are done at the same location. – Justin Oct 01 '13 at 11:25
  • I never said that there is any restructuring apart from rotations, I only said that I mean by 1 restructuring= 1 left rotation+1 right rotation at one location. The difference between insert and delete is that in insert the tree balance is ensured after at most one restructuring whereas in deletion, you may have to do restructuring at multiple locations, hence more than one restructuring – zed111 Oct 02 '13 at 05:01
0

The reason why you only need at most one restructuring for insert and potentially many when deleting is because an effect of restructuring is a decrease the height of that part by 1.

If you need restructuring in insert, that means that you have inserted under a subtree that has a balance factor of 1 or -1, making it 2 or -2. By this insertion, the height of that subtree is increased by 1, and restructuring basically put the tree back to its original height. Thus, the parents of that subtree still have the same height.

On the other hand if you deleted and you need restructuring, you basically deleted the left node of the shorter child of a subtree, causing the balance factor to be 2 or -2. In this case, you did not affect the original height of that subtree after the deletion. However, restructuring the tree will make newheight = oldheight - 1, which might affect the height and balance of its parents. Thus, you need to check every parent of the subtree that's restructured to make sure every subtree is balanced.

Shuyang Ji
  • 11
  • 1