AVL Trees II

30
1 AVL Trees II Implementation

description

AVL Trees II. Implementation. 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. - PowerPoint PPT Presentation

Transcript of AVL Trees II

Page 1: AVL Trees II

1

AVL Trees II

Implementation

Page 2: AVL Trees II

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: AVL Trees II

3

AVL Trees

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

Page 4: AVL Trees II

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: AVL Trees II

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: AVL Trees II

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: AVL Trees II

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: AVL Trees II

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: AVL Trees II

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: AVL Trees II

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: AVL Trees II

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: AVL Trees II

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: AVL Trees II

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: AVL Trees II

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: AVL Trees II

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: AVL Trees II

16

Adding RI, PA, DE

Page 17: AVL Trees II

17

Adding GA, OH

Page 18: AVL Trees II

18

Adding GA, OH

Page 19: AVL Trees II

19

Adding MA

Page 20: AVL Trees II

20

Adding MA

Page 21: AVL Trees II

21

Adding MA

Page 22: AVL Trees II

22

Adding IL, MI, IN

Page 23: AVL Trees II

23

Adding IL, MI, IN

Page 24: AVL Trees II

24

Adding IL, MI, IN

Page 25: AVL Trees II

25

Adding NY

Page 26: AVL Trees II

26

Adding NY

Page 27: AVL Trees II

27

Adding NY

Page 28: AVL Trees II

28

Adding VT

Page 29: AVL Trees II

29

Adding VT

Page 30: AVL Trees II

30

Adding TX, WY