



Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
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
1 / 6
This page cannot be seen from the preview
Don't miss anything!




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
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