This action might not be possible to undo. Are you sure you want to continue?

Welcome to Scribd! Start your free trial and access books, documents and more.Find out more

CSE 373 Data Structures Lecture 8

Readings

Reading

¾ Section 4.4,

12/26/03

AVL Trees - Lecture 8

2

**Binary Search Tree - Best Time
**

All BST operations are O(d), where d is tree depth minimum d is d ! log2N½ for a binary tree with N nodes

¾ What is the best case tree? ¾ What is the worst case tree?

** So, best case running time of BST operations is O(log N)
**

12/26/03 AVL Trees - Lecture 8 3

**Binary Search Tree - Worst Time
**

Worst case running time is O(N)

¾ What happens when you Insert elements in ascending order?

Insert: 2, 4, 6, 8, 10, 12 into an empty BST

**¾ Problem: Lack of ³balance´:
**

compare depths of left and right subtree

¾ Unbalanced degenerate tree

12/26/03

AVL Trees - Lecture 8

4

Lecture 8 5 3 Is this ³balanced´? 3 5 7 5 .Balanced and unbalanced BST 1 2 3 4 4 2 1 12/26/03 4 2 1 5 6 6 7 AVL Trees .

Approaches to balancing trees Don't balance ¾ May end up with some nodes very deep Strict balance ¾ The tree must always be balanced perfectly Pretty good balance ¾ Only allow a little out of balance Adjust on access ¾ Self-adjusting 12/26/03 AVL Trees .Lecture 8 6 .

Lecture 8 7 .Balancing Binary Search Trees Many algorithms exist for keeping binary search trees balanced ¾ Adelson-Velskii and Landis (AVL) trees (height-balanced trees) ¾ Splay trees and other self-adjusting trees ¾ B-trees and other multiway search trees 12/26/03 AVL Trees .

Lecture 8 . insert 2 in the tree on the left and then rebuild as a complete tree 6 4 1 12/26/03 5 9 Insert 2 & complete tree 2 4 6 8 9 8 5 8 1 AVL Trees .Perfect Balance Want a complete tree after every operation ¾ tree is full except possibly in the lower right This is expensive ¾ For example.

heights of left and right subtree can differ by no more than 1 ¾ Store current heights in each node 12/26/03 AVL Trees .Good but not Perfect Balance AVL trees are height-balanced binary search trees Balance factor of a node ¾ height(left subtree) .Lecture 8 9 .AVL .height(right subtree) An AVL tree has balance factor calculated at every node ¾ For every node.

N(1) = 2 Induction ¾ N(h) = N(h-1) + N(h-2) + 1 h Solution (recall Fibonacci analysis) ¾ N(h) > Jh (J } 1. Basis ¾ N(0) = 1.62) 12/26/03 AVL Trees .Height of an AVL Tree N(h) = minimum number of nodes in an AVL tree of height h.Lecture 8 h-1 h-2 10 .

Lecture 8 11 .. Find takes O(logn)) 12/26/03 AVL Trees .62) Suppose we have n nodes in an AVL tree of height h.Height of an AVL Tree N(h) > Jh (J } 1.44 log2n (i.e. ¾ n > N(h) (because N(h) was the minimum) ¾ n > Jh hence logJ n > h (relatively well balanced tree!!) ¾ h < 1.

Lecture 8 12 .Node Heights Tree A (AVL) height=2 BF=1-0=1 Tree B (AVL) 2 1 6 1 0 6 0 0 1 4 0 0 9 0 4 1 5 8 9 1 5 height of node = h balance factor = hleft-hright empty height = -1 12/26/03 AVL Trees .

Node Heights after Insert 7 Tree A (AVL) 2 Tree B (not AVL) 3 1 6 1 1 6 0 1 balance factor 1-(-1) = 2 2 4 0 0 0 9 0 4 1 5 0 9 -1 1 5 7 8 7 height of node = h balance factor = hleft-hright empty height = -1 12/26/03 AVL Trees .Lecture 8 13 .

go back up to the root node by node. updating heights ¾ If a new balance factor (the difference hlefthright) is 2 or ±2.Insert and Rotation in AVL Trees Insert operation may cause balance factor to become 2 or ±2 for some node ¾ only nodes on the path from insertion point to root node have possibly changed in height ¾ So after the Insert. adjust tree by rotation around the node 12/26/03 AVL Trees .Lecture 8 14 .

Single Rotation in an AVL Tree 2 2 6 1 2 1 6 1 4 0 0 1 9 0 4 0 0 8 0 1 5 0 8 7 1 5 7 9 12/26/03 AVL Trees .Lecture 8 15 .

2. There are 4 cases: Outside Cases (require single rotation) : 1. 4. 12/26/03 AVL Trees . Insertion into left subtree of left child of E. Insertion into right subtree of right child of E.Lecture 8 16 . Insertion into left subtree of right child of E. Insertion into right subtree of left child of E. Inside Cases (require double rotation) : 3. The rebalancing is performed through four separate rotation algorithms.Insertions in AVL Trees Let the node that needs rebalancing be E.

Lecture 8 .AVL Insertion: Outside Case Consider a valid AVL subtree j h h k h Z 17 X 12/26/03 Y AVL Trees .

AVL Insertion: Outside Case j k h+1 h h Inserting into X destroys the AVL property at node j Z Y X 12/26/03 AVL Trees .Lecture 8 18 .

AVL Insertion: Outside Case j k h+1 h Do a ³right rotation´ h Z Y X 12/26/03 AVL Trees .Lecture 8 19 .

Lecture 8 20 .Single right rotation j k h+1 h Do a ³right rotation´ h Z Y X 12/26/03 AVL Trees .

Lecture 8 Z 21 AVL property has been restored! .Outside Case Completed k h+1 h ³Right rotation´ done! (³Left rotation´ is mirror symmetric) j h X 12/26/03 Y AVL Trees .

AVL Insertion: Inside Case Consider a valid AVL subtree j h h k h Z 22 X 12/26/03 Y AVL Trees .Lecture 8 .

Lecture 8 23 .AVL Insertion: Inside Case Inserting into Y destroys the AVL property at node j j k h+1 Does ³right rotation´ restore balance? h h Z X 12/26/03 Y AVL Trees .

Lecture 8 24 .AVL Insertion: Inside Case k h j h+1 ³Right rotation´ does not restore balance« now k is out of balance h X Y 12/26/03 Z AVL Trees .

AVL Insertion: Inside Case Consider the structure of subtree Y« j h h+1 k h Z X 12/26/03 Y AVL Trees .Lecture 8 25 .

AVL Insertion: Inside Case Y = node i and subtrees V and W j h k h i V h+1 Z X 12/26/03 h or h-1 W AVL Trees .Lecture 8 26 .

. .AVL Insertion: Inside Case j k i X V 12/26/03 We will do a left-right ³double rotation´ .Lecture 8 27 . Z W AVL Trees .

Double rotation : first rotation j i k W X 12/26/03 left rotation complete Z V AVL Trees .Lecture 8 28 .

Lecture 8 29 .Double rotation : second rotation j i k W X 12/26/03 Now do a right rotation Z V AVL Trees .

Double rotation : second rotation right rotation complete i k h h or h-1 Balance has been restored j h X 12/26/03 V W AVL Trees .Lecture 8 Z 30 .

e. this has to be modified on the path of insertion even if you don¶t perform rotations Once you have performed a rotation (single or double) you won¶t need to go back up the tree 12/26/03 AVL Trees . the balance factor.Implementation balance (1.Lecture 8 31 .0. i.-1) key left right No need to keep the height. just the difference in height.

Single Rotation RotateFromRight(n : reference node pointer) { p : node pointer.right := p. p.left. n := p } You also need to modify the heights or balance factors of n and p X Insert Y Z 12/26/03 AVL Trees .Lecture 8 32 . p := n.left := n. n n.right.

DoubleRotateFromRight(n : reference node pointer) { ???? n } X Z 12/26/03 AVL Trees .Lecture 8 V W 33 .Double Rotation Implement Double Rotation in two lines.

Lecture 8 34 . updating heights ¾ If a new balance factor (the difference hlefthright) is 2 or ±2. go back up to the root node by node.Insertion in AVL Trees Insert at the leaf (as for all BST) ¾ only nodes on the path from insertion point to root node have possibly changed in height ¾ So after the Insert. adjust tree by rotation around the node 12/26/03 AVL Trees .

Insert in BST Insert(T : reference tree pointer. x).data < x : return Insert(T.//the links to //children are null case T.data := x.Lecture 8 35 .data = x : return 0.left. endcase } 12/26/03 AVL Trees .data > x : return Insert(T. T. T. return 1.right. x). x : element) : integer { if T = null then T := new tree. //Duplicate do nothing T.

height := max(height(T.} case T. x).left).Insert in AVL trees Insert(T : reference tree pointer.left.data = x : return .} T. return.height(T. else //inside case T = DoubleRotatefromLeft (T). T. //Duplicate do nothing T.left). } 12/26/03 AVL Trees .right.data > x ) then //outside case T = RotatefromLeft (T).left. x). code similar to the left case Endcase T. x : element) : { if T = null then {T := new tree.height(T. return.right)) = 2){ if (T.data := x. if ((height(T.Lecture 8 36 .data > x : Insert(T.right)) +1. height := 0.data < x : Insert(T.

Lecture 8 37 .Example of Insertions in an AVL Tree 2 20 0 1 Insert 5. 40 0 10 0 30 25 35 12/26/03 AVL Trees .

Lecture 8 38 .Example of Insertions in an AVL Tree 2 20 1 1 1 3 20 30 0 2 1 10 0 0 30 0 0 10 35 5 Now Insert 45 5 25 25 35 0 40 12/26/03 AVL Trees .

Lecture 8 39 .Single rotation (outside case) 3 20 1 2 1 3 20 30 0 2 10 0 0 30 2 0 10 35 1 40 0 45 Now Insert 34 5 25 Imbalance 5 25 0 40 1 0 35 45 12/26/03 AVL Trees .

Double rotation (inside case) 3 20 1 3 1 3 20 35 1 0 25 2 10 0 0 Imbalance 25 30 2 0 10 40 5 30 5 40 1 0 1 35 Insertion of 34 0 45 0 34 45 34 AVL Trees .Lecture 8 40 12/26/03 .

12/26/03 AVL Trees .Lecture 8 41 .AVL Tree Deletion Similar but more complex than insertion ¾ Rotations and double rotations needed to rebalance ¾ Imbalance may propagate upward so that many rotations may be needed.

B-trees). more space for balance factor.g. Asymptotically faster but rebalancing costs time.Lecture 8 42 . Search is O(log N) since AVL trees are always balanced.g. 2. 2. May be OK to have O(N) for a single operation if total run time for many consecutive operations is fast (e. 4. Insertion and deletions are also O(logn) 3.Pros and Cons of AVL Trees Arguments for AVL trees: 1. 12/26/03 AVL Trees . Difficult to program & debug. 3. The height balancing adds no more than a constant factor to the speed of insertion. Arguments against using AVL trees: 1. Splay trees). Most large searches are done in database systems on disk and use other structures (e.

n RotateFromRight(n). } X Z V 12/26/03 AVL Trees .right).Lecture 8 W 43 .Double Rotation Solution DoubleRotateFromRight(n : reference node pointer) { RotateFromLeft(n.

- Lecture 08
- A TechXpress Guide Ethernet Bonding for NICs
- DATASHEET Hotfind L Series IR
- What is the Snapshot Control File
- LVM on three SCSI disks.docx
- Channel Status
- Windows 10 Compare Editions
- timer3_set32
- OS New_BBA 24th
- Black Berry
- 14 Things Hackers Dont Want You to Know
- Cache1
- 4GT
- Presentation 1
- MeeGo
- 16-178_180
- Database Version Details
- CSA 2014 Blackberry-slides
- Presentation to deepak sir
- ENC Tools
- IMEI
- j2me
- Network Programming II - OOPN
- da248-01
- Timers Lecture
- BSNL Nodal Officers Ppt New
- ZigBeeRFID-9
- Ssh Configure
- Dataload General Information
- Automation Presentation

Are you sure?

This action might not be possible to undo. Are you sure you want to continue?

We've moved you to where you read on your other device.

Get the full title to continue

Get the full title to continue listening from where you left off, or restart the preview.

scribd