Topics in this Lecture - PowerPoint PPT Presentation

1 / 29
About This Presentation
Title:

Topics in this Lecture

Description:

Recall the remove operation in the BST. Remove replaces the node being removed ... An AVL (Adelson-Velskii and Landis named after the researches who proposed ... – PowerPoint PPT presentation

Number of Views:27
Avg rating:3.0/5.0
Slides: 30
Provided by: itd677
Category:

less

Transcript and Presenter's Notes

Title: Topics in this Lecture


1
Topics in this Lecture
  • Problem with BST-s
  • Balanced Trees
  • AVL Tree
  • Rotations Single, Double

2
Problem of BST
  • Recall the remove operation in the BST
  • Remove replaces the node being removed by the
    minimum child in the right subtree of the node
    being removed
  • This means that some node is getting removed and
    another node on the right subtree of the branch
    is replacing it

3
Left Heavy BST
  • Repeated remove-s along the same branch of a BST
    results in the the right subtree losing nodes
    that replace a node somewhere to the left of that
    right subtree
  • This results in BST-s becoming left heavy

4
Why are Left Heavy BST-s Bad?
  • Because the average time for the tree operations
    deteriorates from O(log N) and for some specific
    cases it can become O(N2)
  • The problem of left heavy BST-s can be solved
    using balanced trees

5
Balanced Trees
  • Balanced trees require every node to be balanced
  • An AVL (Adelson-Velskii and Landis named after
    the researches who proposed this data structure)
    tree is an example of a balanced tree
  • AVL trees are identical to BST for operations
    such as insert, remove, find, etc.
  • However, every time the number of nodes in the
    AVL tree changes (through an insert or delete)
    the tree needs to be balanced

6
AVL Tree
  • To measure the balance at every node, a balance
    factor is stored at every node
  • In an AVL tree every node stores the difference
    in height d between its left and right
    subtrees. This is the balance factor for an AVL
    tree
  • If d 0 or d 1 or d -1 then the node is
    balanced

7
Examples of AVL Tree d
d-1
d1
d0
d0
  • The d values above are for node A only
  • Nodes B and C have d0 in all cases
  • Nodes B and C can be single nodes(as shown) or
    subtrees

8
Allowable Values for d
  • The value of d at a node can only get changed
    by an insert or a remove operation
  • If d takes some other value because of an
    insert or a delete we have to adjust nodes so
    that all nodes in the AVL tree have d lt 1
  • d stands for modulus of d. Modulus means we
    consider the value of d and not its sign
  • So 0 0, 1 1, and -1 1

9
Where in the AVL Treemight d change?
  • If a node is inserted or deleted under a node N,
    the d value can change at any node along the
    path starting from the root till node N

d-1
d-2 violation
insert node F
d-1
10
Example of insert in an AVL Tree
d-2 violation
d-1
insert node F
d-1
d-1
  • All nodes where d values are not shown have d
    0
  • After the insert the d values of nodes C and
    E become 1
  • After the insert, the right subtree of A has
    height 3 while
  • the left subtree has height 1 (only node B)
  • So, the d value of node A is 2 after the
    insert.
  • Since -2 2gt 1 therefore there is a
    violation of the balance factor at node A

11
Rotation in an AVL Tree
  • To restore the d value of nodes where the d
    value has been violated, an operation called a
    rotation needs to be done
  • Violation of the d value at a node is also
    called violation of balance or imbalance at that
    node
  • There can be two types of rotation in an AVL tree
  • single rotation
  • double rotation

12
Conditions for Violation of Balance Condition
  • For simplicity we will classify the operations at
    a node that might lead to an imbalance into four
    types
  • So, an imbalance can be caused by one of the
    following four operations
  • An insertion into the left subtree of the left
    child
  • An insertion into the right subtree of the left
    child
  • An insertion into the left subtree of the right
    child
  • An insertion into the right subtree of the right
    child

13
Similarity between Insert and Remove on AVL Tree
  • A remove is just the reverse of an insert.
  • So the technique for removing an imbalance caused
    by an insert will apply in the same way for
    removing an imbalance caused by a remove.
  • Hence, the cause of the imbalance from a remove
    can be classified into one of the four types on
    the last slide, except that, insertion into
    would be replaced by remove from.

14
Rotations in AVL Tree
  • Single rotation is used to restore balance when
    conditions 1 or 4 (on slide 12) happen
  • Double rotation is used to restore balance when
    conditions 2 and 3 (on slide 12) happen

15
Condition 1 Single Rotation Figure
d 1
16
Condition 1 Single Rotation
  • In the figure on the left hand side of the
    previous slide, k2 is the left child of k1
  • A node had been inserted in the left subtree X of
    the left child(k2) of k1
  • This is similar to condition 1 mentioned on slide
    12
  • As a result of the insertion, there is an
    imbalance at k1 as shown by its d-value2

17
Condition 1 Single Rotation (contd.)
  • To restore balance, a single rotation operation
    is done as shown in the figure on slide 17
  • After the single rotation notice that all nodes
    are balanced (i.e., they have d lt1)
  • Also notice that k2 is the new parent node
  • Verify that the BST property is preserved by the
    rotation

18
Condition 4 Single Rotation Figure
d 3-12
d2-11
k1
S. R.
1
k2
2
X
1
1
2
Z
Single Rotation
19
Condition 4 Single Rotation
  • Condition 4 is just the mirror image of condition
    1
  • In the figure on the left hand side of the
    previous slide, a node got inserted in the right
    subtree Z of the right child (k2) of node k1
  • This corresponds to condition 4 on slide 14
  • The node k1 got imbalanced due to this insertion
    and got a d-value 2

20
Condition 4 Single Rotation (contd.)
  • To restore balance, once again a single rotation
    operation is done as shown in the figure on slide
    18
  • After the single rotation notice that all nodes
    are balanced (i.e., they have d lt1)
  • Also notice that k2 is the new parent node
  • Verify once again that the BST property is
    preserved by the rotation

21
Condition 2 Double Rotation Figure
d 3-12
Double Rotation
1
d1-21
1
0.5
22
Condition 2 Double Rotation
  • In the figure on the left hand side on the last
    slide, k1 is the left child of k3
  • An insertion occurred into the right subtree of
    the left child (k1) of node k3
  • This is condition 2 on slide 12

23
Condition 2 Double Rotation (contd.)
  • The right subtree of k1 comprises node k2 and
    subtrees B and C.
  • Before the insertion the right subtree of k1
    contained only node k2 (because the tree must
    have been balanced before the insertion).
  • Due to the insertion only one of nodes B or C got
    inserted NOT BOTH

24
Condition 2 Double Rotation (contd.)
  • B and C would be a single node subtree of height
    1
  • To denote the average height, that is, either B
    or C is present (but not both) we show both B and
    C but give them a height 0.5
  • Showing B and C both on the same figure with a
    height of 0.5 is just a short-cut used on paper
    to avoid drawing two separate diagrams one
    diagram for Bs insertion and another diagram for
    Cs insertion
  • In real life (when the program runs), either B or
    C only gets inserted with a height of 1

25
Condition 2 Double Rotation (contd.)
  • Due to the insertion an imbalance occurred at k3
  • You should calculate the height of the left and
    right subtrees at k3 and verify that there is
    indeed an imbalance at k3
  • While calculating the heights assume that only
    one of B or C is present with a height of 1
    instead of 0.5
  • The imbalance at k3 is restored using the double
    rotation as shown on slide 23
  • Verify once again that after the double rotation
    BST property is still valid and balance is
    restored at every node

26
Condition 3 Double Rotation
  • Condition 3 is just the mirror image of condition
    2
  • The corresponding figure is given as Figure 4.39
    on page 149 of the text book

27
AVL Tree Implementation Data Members
  • The data structure for an AVL tree is identical
    to the BST
  • Additionally, the balance factor needs to be
    stored at every node as an integer
  • class AVLNode
  • AVLNode lchild, rchild
  • int balance
  • int key

28
AVL Tree Operations
  • The operations of BST such as insert, delete,
    find etc. can be used as is in AVL trees
  • AVL trees require two additional methods one
    for single rotation and another for double
    rotation

29
Epilogue
  • In this lecture we studied about an extension of
    BST-s called AVL trees
  • AVL trees are height balanced
  • Balance is maintained at each node by checking
    for an imbalance following an insert or remove
  • If there is an imbalance, it is restored by doing
    a single or a double rotation, as need be, to
    restore the balance
Write a Comment
User Comments (0)
About PowerShow.com