CS301-Lec21 handout - CS301 Data Structures Lecture No. 21...

Info iconThis preview shows pages 1–4. Sign up to view the full content.

View Full Document Right Arrow Icon
CS301 – Data Structures Lecture No. 21 ___________________________________________________________________ Data Structures Lecture No. 21 Reading Material Data Structures and Algorithm Analysis in C++ Chapter. 4 4.4, 4.4.1 Summary AVL Tree Building Example Cases for Rotation AVL Tree Building Example This lecture is a sequel of the previous one in which we had briefly discussed about building an AVL tree. We had inserted three elements in the tree before coming to the end of the lecture. The discussion on the same example will continue in this lecture. Let’s see the tree’s figures below: 2 1 3 Fig 21.1: insert(3) single left rotation -2 Page 1 of 10
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
CS301 – Data Structures Lecture No. 21 ___________________________________________________________________ 2 1 3 Fig 21.2: insert(3) Node containing number 2 became the root node after the rotation of the node having number 1 . Note the direction of rotation here. Let’s insert few more nodes in the tree. We will build an AVL tree and rotate the node when required to fulfill the conditions of an AVL tree. To insert a node containing number 4, we will, at first, compare the number inside the root node. The current root node is containing number 2 . As 4 is greater than 2 , it will take the right side of the root. In the right subtree of the root, there is the node containing number 3 . As 4 is also greater than 3 , it will become the right child of the node containing number 3 . 2 1 3 Fig 21.3: insert(4) 4 Once we insert a node in the tree, it is necessary to check its balance to see whether it is within AVL defined balance. If it is not so, then we have to rotate a node. The balance factor of the node containing number 4 is zero due to the absence of any left or right subtrees. Now, we see the balance factor of the node containing number 3 . As it has no left child, but only right subtree, the balance factor is –1 . The balance factor of the node containing number 1 is 0 . For the node containing number 2 , the height of the left subtree is 1 while that of the right subtree is 2. Therefore, the balance factor of the node containing number 2 is 1 – 2 = -1 . So every node in the tree in fig. 21.3 has balance factor either 1 or less than that. You must be remembering that the condition for a tree to be an AVL tree, every node’s balance needs not to be zero necessarily. Rather, the tree will be called AVL tree, if the balance factor of each node in a tree is 0 , 1 or –1. By the way, if the balance factor of each node inside the tree is 0, it will be a perfectly balanced tree. Page 2 of 10
Background image of page 2
CS301 – Data Structures Lecture No. 21 ___________________________________________________________________ 2 1 3 Fig 21.4: insert(5) 4 5 -2 Next, we insert a node containing number 5 and see the balance factor of each node. The balance factor for the node containing 5 is 0 . The balance factor for node containing 4 is –1 and for the node containing 3 is -2 . The condition for AVL is not satisfied here for the
Background image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Image of page 4
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 05/21/2010 for the course CS CS301 taught by Professor Dr.naveed malik during the Spring '10 term at Virtual University of Pakistan.

Page1 / 10

CS301-Lec21 handout - CS301 Data Structures Lecture No. 21...

This preview shows document pages 1 - 4. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online