Balance Factor - Data Structures - Lecture Notes, Study notes of Data Structures and Algorithms

Some concept of Data Structures are Abstract, Balance Factor, Complete Binary Tree, Dynamically, Storage, Implementation, Sequential Search, Advanced Data Structures, Graph Coloring Two, Insertion Sort. Main points of this lecture are: Balance Factor, Special Type, Binary Search Tree, Height Balanced, Guarantee, Insertions, Deletions, Height, Integer Keys, Subtrees

Typology: Study notes

2012/2013

Uploaded on 04/30/2013

jut
jut 🇮🇳

4.5

(63)

77 documents

1 / 6

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
1. An AVL Tree is a special type of Binary Search Tree (BST) that it is height balanced. By height balanced I mean
that the height of every node’s left and right subtrees differ by at most one. This is enough to guarantee that a AVL
tree with n nodes has a height no worst than O(1.44 log
2
n). Therefore, insertions, deletions, and search are worst case
O( log
2
n ). An example of an AVL tree with integer keys is shown below. The height of each node is shown.
50
30 60
9 34
32 47
80
0
00
0 1
2 1
3
Each AVL-tree node usually stores a balance factor in addition to its key and payload. The balance factor keeps track
of the relative height difference between its left and right subtrees, i.e., height(left subtree) - height(right subtree).
a) Label each node in the above AVL tree with one of the following balance factors:
0 if its left and right subtrees are the same height
1 if its left subtree is one taller than its right subtree
-1 if its right subtree is one taller than its left subtree
b) We start a put operation by adding the new item into the AVL as a leaf just like we did for Binary Search Trees
(BSTs). Add the key 90 to the above tree?
c) Identify the node “closest up the tree" from the inserted node (90) that no longer satisfies the height-balanced
property of an AVL tree. This node is called the pivot node. Label the pivot node above.
d) Consider the subtree whose root is the pivot node. How could we rearrange this subtree to restore the AVL height
balanced property? (Draw the rearranged tree below)
30
9 34
32 47
0 0
0 1
2
Data Structures (CS 1520) Lecture 23 Name:________________
Lecture 23 Page 1
Docsity.com
pf3
pf4
pf5

Partial preview of the text

Download Balance Factor - Data Structures - Lecture Notes and more Study notes Data Structures and Algorithms in PDF only on Docsity!

  1. An AVL Tree is a special type of Binary Search Tree (BST) that it is height balanced. By height balanced I mean that the height of every node’s left and right subtrees differ by at most one. This is enough to guarantee that a AVL tree with n nodes has a height no worst than O (1.44 log 2 n). Therefore, insertions, deletions, and search are worst case O ( log 2 n ). An example of an AVL tree with integer keys is shown below. The height of each node is shown.

0

0 0

0 1

2 1

3

Each AVL-tree node usually stores a balance factor in addition to its key and payload. The balance factor keeps track of the relative height difference between its left and right subtrees, i.e., height(left subtree) - height(right subtree). a) Label each node in the above AVL tree with one of the following balance factors :  0 if its left and right subtrees are the same height  1 if its left subtree is one taller than its right subtree  -1 if its right subtree is one taller than its left subtree

b) We start a put operation by adding the new item into the AVL as a leaf just like we did for Binary Search Trees (BSTs). Add the key 90 to the above tree?

c) Identify the node “closest up the tree" from the inserted node (90) that no longer satisfies the height-balanced property of an AVL tree. This node is called the pivot node. Label the pivot node above.

d) Consider the subtree whose root is the pivot node. How could we rearrange this subtree to restore the AVL height balanced property? (Draw the rearranged tree below)

0 0

0 1

2

  1. Typically, the addition of a new key into an AVL requires the following steps:  compare the new key with the current tree node’s key (as we did in the _put function called by the put method in the BST) to determine whether to recursively add the new key into the left or right subtree  add the new key as a leaf as the base case(s) to the recursion  recursively (updateBalance method) adjust the balance factors of the nodes on the search path from the new node back up toward the root of the tree. If we encounter a pivot node (as in question (c) above) we perform one or two “rotations” to restore the AVL tree’s height-balanced property.

For example, consider the previous example of adding 90 to the AVL tree. Before the addition, the pivot node (60) was already -1 (“tall right” - right subtree had a height one greater than its left subtree). After inserting 90, the pivot’s right subtree had a height 2 more than its left subtree (balance factor -2) which violates the AVL tree’s height-balance property. This problem is handled with a left rotation about the pivot as shown in the following generalized diagram:

Before the addition: After the addition, but before rotation:

After left rotation at pivot:

B B

B

D D

D

-1 -

0

0 -

0

from parent from parent

from parent

T

T

T

T T

T

T T

T

E

E

C

A A

A

E C

C

height

height

height

height height

height

height height

height

n

n

n - 1

n - 1 n - 1

n - 1

n - 1 n - 1

n - 1

new

new

node

node

Recursive updateBalance method finds the pivot

(D's balance factor was already adjusted before the pivot is found by the recursive updateBalance

and calls the rebalance method to perform proper rotation(s)

Rotate Left at Pivot

method which moves toward the root)

a) Assuming the same initial AVL tree (upper, left-hand of above diagram) if the new node would have increased the height of TC (instead of TE), would a left rotation about the node B have rebalanced the AVL tree?

Consider the AVLTreeNode class that inherits and extends the TreeNode class to include balance factors.

from tree_node import TreeNode class AVLTreeNode(TreeNode): def init(self,key,val,left=None,right=None,parent=None, balanceFactor=0): TreeNode.init(self,key,val,left,right,parent) self.balanceFactor = balanceFactor

Now let’s consider the partial AVLTree class code that inherits from the BinarySearchTree class:

from avl_tree_node import AVLTreeNode from binary_search_tree import BinarySearchTree class AVLTree(BinarySearchTree): def put(self,key,val): if self.root: self._put(key,val,self.root) else: self.root = AVLTreeNode(key,val) self.size = self.size + 1 def _put(self,key,val,currentNode): if key < currentNode.key: if currentNode.hasLeftChild(): self._put(key,val,currentNode.leftChild) else: currentNode.leftChild = AVLTreeNode(key,val,parent=currentNode) self.updateBalance(currentNode.leftChild) else: if currentNode.hasRightChild(): self._put(key,val,currentNode.rightChild) else: currentNode.rightChild = AVLTreeNode(key,val,parent=currentNode) self.updateBalance(currentNode.rightChild) def updateBalance(self,node): if node.balanceFactor > 1 or node.balanceFactor < -1: self.rebalance(node) return if node.parent != None: if node.isLeftChild(): node.parent.balanceFactor += 1 elif node.isRightChild(): node.parent.balanceFactor -= 1 if node.parent.balanceFactor != 0: self.updateBalance(node.parent) def rotateLeft(self,rotRoot): ## NOTE: You will complete rotateRight in Lab newRoot = rotRoot.rightChild rotRoot.rightChild = newRoot.leftChild if newRoot.leftChild != None: newRoot.leftChild.parent = rotRoot newRoot.parent = rotRoot.parent if rotRoot.isRoot(): self.root = newRoot else: if rotRoot.isLeftChild(): rotRoot.parent.leftChild = newRoot else: rotRoot.parent.rightChild = newRoot newRoot.leftChild = rotRoot rotRoot.parent = newRoot rotRoot.balanceFactor = rotRoot.balanceFactor + 1 - min(newRoot.balanceFactor, 0) newRoot.balanceFactor = newRoot.balanceFactor + 1 + max(rotRoot.balanceFactor, 0) def rebalance(self,node): if node.balanceFactor < 0: if node.rightChild.balanceFactor > 0: self.rotateRight(node.rightChild) self.rotateLeft(node) else: self.rotateLeft(node) elif node.balanceFactor > 0: if node.leftChild.balanceFactor < 0: self.rotateLeft(node.leftChild) self.rotateRight(node) else: self.rotateRight(node)

c) Trace the code for myBST.put(90)by updating the below diagram:

myBST BinarySearchTree object

size root

60

80

50

0

1

Consider balance factor formulas for rotateLeft. We know: newBal(B) = hA - hC and oldBal(B) = hA - (1+ max ( hC, hE )) newBal(D) = 1+ max ( hA, hC ) - hE and oldBal(D) = hC - hE

Consider: newBal(B) - oldBal(B) newBal(B) - oldBal(B) = hA - hC - hA + (1+ max ( hC, hE )) newBal(B) - oldBal(B) = 1 + max ( hC, hE ) - hC newBal(B) - oldBal(B) = 1 + max ( hC, hE ) - hC newBal(B) = oldBal(B) + 1 + max ( hC - hC, hE - hC ) newBal(B) = oldBal(B) + 1 + max (0, -oldBal(D) ) newBal(B) = oldBal(B) + 1 - min (0, oldBal(D) ), so rotRoot.balanceFactor = rotRoot.balanceFactor + 1 - min(newRoot.balanceFactor, 0)

d) Consider: newBal(D) - oldBal(D)

Lecture 23 Page 5

Before left rotation: After left rotation at pivot:

B B D

D

rotRoot (^) rotRoot newRoot

rotRoot

newRoot

T

T T h T h h h h

h T (^) E T

E A A A C E A C

E C (^) C height

height height height (^) height height

Rotate Left at Pivot

Docsity.com