30
1 AVL Trees II Implementation

1 AVL Trees II Implementation. 2 AVL Tree ADT A binary search tree in which the balance factor of each…

Embed Size (px)

DESCRIPTION

3 AVL Trees Class to represent AVL tree nodes Includes new data member for the balance factor

Citation preview

Page 1: 1 AVL Trees II Implementation. 2 AVL Tree ADT A binary search tree in which the balance factor of each…

1

AVL Trees II

Implementation

Page 2: 1 AVL Trees II Implementation. 2 AVL Tree ADT A binary search tree in which the balance factor of each…

2

AVL Tree ADT

A binary search tree in which the balance factor of each node is 0, 1, of -1.

Basic Operations Construction, empty, search, and traverse

are the same as for a BST. Insert a new item in such a way that the

height-balanced property is maintained. Delete a item in such a way that the height-

balanced property is maintained. Left for advanced course! Discussed in Brozdek.

Page 3: 1 AVL Trees II Implementation. 2 AVL Tree ADT A binary search tree in which the balance factor of each…

3

AVL Trees

Class to represent AVL tree nodes Includes new data member for the balance factor

Page 4: 1 AVL Trees II Implementation. 2 AVL Tree ADT A binary search tree in which the balance factor of each…

4

What do we need to know? When we look at a tree on paper, it is

fairly easy to determine if the tree has become unbalanced. Also what to do to fix the problem.

When implementing the ADT, we have to work with local information. What do we have available as we descend

the tree to add a node and return? What do we need to know at each node?

Page 5: 1 AVL Trees II Implementation. 2 AVL Tree ADT A binary search tree in which the balance factor of each…

5

Observations

When we add a node to an AVL tree, the parent node will either be a leaf or have a single child.

If the parent node is a leaf, its balance factor prior to the addition is 0.

After the addition, the balance factor absolute value will be 1. Ancestor nodes’ balance factor may increase, decrease, or

remain the same. Rebalancing may or may not be necessary.

If the parent node has a single child, its absolute balance factor prior to the addition will be 1.

After the addition, it will be 0. Ancestor nodes’ balance factors will not change. Rebalancing will not be necessary.

Page 6: 1 AVL Trees II Implementation. 2 AVL Tree ADT A binary search tree in which the balance factor of each…

6

Observations

Given that the tree was an admissible AVL tree before we added a node:

Adding a node always changes the balance factor of the parent of the new node.

Absolute value 0 to 1 or 1 to 0.

The parent node will never become unbalanced due to an addition.

The grandparent is the first node (going up the tree) that can become unbalanced due to an addition.

An ancestor further up the tree could be the first node to become unbalanced.

See addition of MA in previous presentation.

Page 7: 1 AVL Trees II Implementation. 2 AVL Tree ADT A binary search tree in which the balance factor of each…

7

When does a node become unbalanced?

A node will become unbalanced due to an addition if the following conditions are true: It already had an balance factor of +1 or -1. In searching for the point to do the addition we

descended into its subtree that already had the greater height.

Left subtree if balance factor was +1. Right subtree is balance factor was -1.

Adding the node increased the height of that subtree.

We need to know if adding a node to a subtree increased the height of the subtree.

Page 8: 1 AVL Trees II Implementation. 2 AVL Tree ADT A binary search tree in which the balance factor of each…

8

Did the subtree height increase?

We need to know if adding a node to a subtree increased the height of the subtree.

The (recursive) insert function must report back to its caller whether adding a new node increased the height of the subtree to which it was added.

At each step we can then determine if the node has become unbalanced. Whether or not we need to do a rotation.

Page 9: 1 AVL Trees II Implementation. 2 AVL Tree ADT A binary search tree in which the balance factor of each…

9

When does a node become unbalanced?

Adding a node increased the height of the subtree at a node along the path from the new node back to the root if: The addition increased the height of the subtree

rooted at the parent of the new node. We added the node to a leaf.

The addition increased the height of the subtree rooted at each intermediate node.

Balance factor at each intermediate node was 0

If a node previously had a nonzero balance factor and adding the new node increased the height of its higher subtree, the addition made that node unbalanced.

Page 10: 1 AVL Trees II Implementation. 2 AVL Tree ADT A binary search tree in which the balance factor of each…

10

Rebalancing the Tree

If adding a node increased the height of a subtree and any node became unbalanced because of the increase, we will do a rotation at the nearest ancestor that became unbalanced.

The rotation reduces the height of the subtree rooted at that position. Will ensure that the subtree at that position

does not increase in height. No further rotations will be needed.

Page 11: 1 AVL Trees II Implementation. 2 AVL Tree ADT A binary search tree in which the balance factor of each…

11

Rebalancing the Tree

We will use a recursive search to find the node at which to attach the new node.

Previously we used a loop.

When we find the place to attach the new node, after attaching it, return values saying Whether or not the subtree height increased. On which side the new node was added.

Required in order to determine if a double rotation is needed.

Page 12: 1 AVL Trees II Implementation. 2 AVL Tree ADT A binary search tree in which the balance factor of each…

12

Rebalancing the Tree

At each step in the sequence of recursive calls, report back the same information: Did the subtree rooted at this position

increase in height? Which direction did we descend to do

the addition?

Page 13: 1 AVL Trees II Implementation. 2 AVL Tree ADT A binary search tree in which the balance factor of each…

13

Rebalancing the Tree At each node as we descend from the root If the balance factor is nonzero

If we descended in the direction of our higher subtree

If the next node down reports back that the height of its subtree increased

This node has become unbalanced. We must do a rotation at this node.

Page 14: 1 AVL Trees II Implementation. 2 AVL Tree ADT A binary search tree in which the balance factor of each…

14

Rebalancing the Tree

If we have to do a rotation If the next node down reports that it

descended in the same direction Do a single rotation.

Else Do a double rotation.

Page 15: 1 AVL Trees II Implementation. 2 AVL Tree ADT A binary search tree in which the balance factor of each…

15

Implementation

Download: http://www.cse.usf.edu/~turnerr/Data_Structures/Dow

nloads/2011_03_28_AVL_Tree/ File AVL_Tree_Demo.zip

Project contains: AVL_BST.h Our most recent BST template updated

to implement an AVL tree. main.cpp Uses the AVL BST to replicate the states

example from last class.

Extract, build, and run

Page 16: 1 AVL Trees II Implementation. 2 AVL Tree ADT A binary search tree in which the balance factor of each…

16

Adding RI, PA, DE

Page 17: 1 AVL Trees II Implementation. 2 AVL Tree ADT A binary search tree in which the balance factor of each…

17

Adding GA, OH

Page 18: 1 AVL Trees II Implementation. 2 AVL Tree ADT A binary search tree in which the balance factor of each…

18

Adding GA, OH

Page 19: 1 AVL Trees II Implementation. 2 AVL Tree ADT A binary search tree in which the balance factor of each…

19

Adding MA

Page 20: 1 AVL Trees II Implementation. 2 AVL Tree ADT A binary search tree in which the balance factor of each…

20

Adding MA

Page 21: 1 AVL Trees II Implementation. 2 AVL Tree ADT A binary search tree in which the balance factor of each…

21

Adding MA

Page 22: 1 AVL Trees II Implementation. 2 AVL Tree ADT A binary search tree in which the balance factor of each…

22

Adding IL, MI, IN

Page 23: 1 AVL Trees II Implementation. 2 AVL Tree ADT A binary search tree in which the balance factor of each…

23

Adding IL, MI, IN

Page 24: 1 AVL Trees II Implementation. 2 AVL Tree ADT A binary search tree in which the balance factor of each…

24

Adding IL, MI, IN

Page 25: 1 AVL Trees II Implementation. 2 AVL Tree ADT A binary search tree in which the balance factor of each…

25

Adding NY

Page 26: 1 AVL Trees II Implementation. 2 AVL Tree ADT A binary search tree in which the balance factor of each…

26

Adding NY

Page 27: 1 AVL Trees II Implementation. 2 AVL Tree ADT A binary search tree in which the balance factor of each…

27

Adding NY

Page 28: 1 AVL Trees II Implementation. 2 AVL Tree ADT A binary search tree in which the balance factor of each…

28

Adding VT

Page 29: 1 AVL Trees II Implementation. 2 AVL Tree ADT A binary search tree in which the balance factor of each…

29

Adding VT

Page 30: 1 AVL Trees II Implementation. 2 AVL Tree ADT A binary search tree in which the balance factor of each…

30

Adding TX, WY