AVL Trees in Data Structures

Choose a study mode

Play Quiz
Study Flashcards
Spaced Repetition
Chat to Lesson

Podcast

Play an AI-generated podcast conversation about this lesson

Questions and Answers

In the context of AVL trees, what does it mean for a node 'v' to be height-balanced?

  • The height of node 'v' is less than or equal to the logarithm of the total number of nodes in the tree.
  • The node 'v' is the root of a complete binary tree.
  • The node 'v' has an equal number of nodes in its left and right subtrees.
  • The absolute difference in height between the left and right subtrees of node 'v' is less than or equal to 1. (correct)

A binary search tree (BST) with a height of $n-1$ for $n$ nodes guarantees $O(log\ n)$ time complexity for search operations.

False (B)

What is the primary goal of maintaining balance in a binary search tree, such as an AVL tree, and how does it relate to the time complexity of operations?

The primary goal is to ensure that operations like search, insert, and delete have a time complexity of $O(log\ n)$, where n is the number of nodes, by keeping the tree's height logarithmic.

AVL trees maintain balance by ensuring that for every node, the absolute difference in heights of its left and right subtrees is no more than ______.

<p>1</p> Signup and view all the answers

Which of the following is NOT a standard operation defined in a dictionary interface?

<p><code>sort()</code> (C)</p> Signup and view all the answers

AVL trees are guaranteed to have a height less than $2log(n)$, ensuring they are always balanced.

<p>True (A)</p> Signup and view all the answers

Match the operation on a Binary Search Tree (BST) with its corresponding time complexity in Big O notation for a balanced BST:

<p>Search = O(log n) Insert = O(log n) Delete = O(log n) Traversal = O(n)</p> Signup and view all the answers

What condition must be checked or maintained after every insertion or deletion in an AVL tree?

<p>Whether the good height-balanced property still holds. (A)</p> Signup and view all the answers

Flashcards

Dictionary Interface

A data structure that stores (key, value) pairs, supporting operations like insert, search, delete, successor, predecessor, contains, and size.

Binary Search Tree (BST)

A tree data structure where each node has at most two children and satisfies the BST property: left subtree < node's key < right subtree.

Role of Height (h) in BST

The height of a binary search tree where operations like insert, delete, search, successor, and predecessor take O(h) time.

Balanced BST

A BST where the height h is O(log n), ensuring all operations run in O(log n) time.

Signup and view all the flashcards

Height-Balanced BST

A binary search tree that maintains a height balance property to ensure O(log n) operation times.

Signup and view all the flashcards

Height-Balanced Node

A tree is height-balanced if, for every node, the absolute difference in height between its left and right subtrees is no more than 1.

Signup and view all the flashcards

AVL Trees

A self-balancing binary search tree where for every node the height of its two child subtrees differ by at most one.

Signup and view all the flashcards

Height of Balanced Tree Theorem

A height-balanced tree with n nodes has height h < 2log(n), meaning it is balanced.

Signup and view all the flashcards

Study Notes

  • The lecture is about Data Structures and Algorithms specifically AVL Trees
  • Homework PS4 will be released Feb 12 15:15, and due Feb 18 23:59
  • Scapegoat Trees should be implemented, with caution

Todays Plan

  • Cover height-balanced binary search trees, AVL trees, Rotations, Insertion and Deletion

Recap: Dictionary Interface

  • A dictionary interface has key and value pairs
  • insert(Key k, Value v) inserts the key and value into a table
  • search(Key k) gets the value paired with the key
  • successor(Key k) finds the next key which is greater than k
  • predecessor(Key k) finds the next key which is less than k
  • delete(Key k) removes the key and value
  • contains(Key k) indicates there is a value for key k or not
  • size() states the number of key and value pairs

Recap: Binary Search Trees

  • Binary search trees have two children: v.left, and v.right
  • The key is represented as v.key
  • BST Property is such that all in left sub-tree < key < all in right sub-tree

Binary Search Tree

  • Modifying operations such as insert and delete, take O(h) time
  • Query operations such as search, predecessor, successor, findMax, and findMin take O(h) time
  • Traversals take O(n) time

The Importance of Being Balanced

  • Operations take O(h) time where log(n) −1 ≤ h ≤ n
  • A BST is balanced if h = O(log n)
  • All operations on a balanced BST run in O(log n) time

How to get a balanced tree:

  • Define a good property of a tree.
  • Show that if the good property holds, then the tree is balanced.
  • Invariant, after every insert/delete, the good property still holds, if not, fix it

AVL Trees

  • AVL Trees were developed by Adelson-Velskii & Landis in 1962
  • A node v is height-balanced if: |v.left.height – v.right.height| ≤ 1
  • A height-balanced tree with n nodes has at most height h < 2log(n)
  • A height-balanced tree is balanced

Inserting in an AVL Tree

  • Starting initially balanced, after doing an insert the tree is no longer balanced
  • Rotations are used to rebalance

Tree Rotations

  • Rotation costs O(1)
  • Rotations maintain the ordering of keys
  • Rotations maintains the BST property

Tree Rotations (Left and Right Heavy)

  • After inserting into the tree rotations are employed to restore balance
  • The height is out-of-balance by 1 node
  • Tree rotations should be used to restore balance
  • After inserting, start at the bottom and work up the tree
  • A is LEFT-heavy if the left sub-tree has larger height than the right sub-tree.
  • A is RIGHT-heavy if the right sub-tree has larger height than the left sub-tree

A is the lowest node in the tree violating the balance property.

  • Assume A is LEFT-heavy
  • Case 1: B is equi-height: h(L) = h(M) where h(R) = h(M) – 1, and a right-rotate is needed
  • Case 2: B is left-heavy : h(L) = h(M) + 1 where h(R) = h(M), and a right-rotate is needed
  • Case 3: B is right-heavy : h(L) = h(M) - 1 where h(R) = h(L), and a right-rotate is needed

Rotations Summary

  • v is out of balance and left heavy
  • v.left is balanced: right-rotate(v)
  • v.left is left-heavy: right-rotate(v)
  • v.left is right-heavy: left-rotate(v.left) then right-rotate(v)
  • If v is out of balance and right heavy, the case is symmetric

Multiple Rotations

  • In the worst case, two rotations are needed after an insertion
  • After insert, the insert increases heights by 1
  • Rotation reduces root height by 1, everything higher in the tree is unchanged
  • Rotation does not reduce the height for case 1: B is balanced
  • Case 1 is needed for deletes

Insert in AVL Tree

  • Insert the key in BST
  • Walk up the tree, at every step, check for balance
  • If out-of-balance, use rotations to rebalance and return
  • Can only fix the the lowest out-of-balance node
  • Only need at most two rotations to fix

Binary Search Tree Deletion

  • Three Cases for removal:
  • No children
  • 1 child
  • 2 children
  • If v has two children, swap v with its successor
  • Delete node v from binary tree and reconnect children
  • For every ancestor of the deleted node:
  • Check if it is height-balanced.
  • If not, perform a rotation.
  • Continue to the root.
  • Deletion may take up to O(log(n)) rotations

Delete in AVL Tree

  • Delete key from BST
  • Walk up tree
  • At every step, check for balance
  • If out-of-balance, use rotations to rebalance
  • Continue to root
  • It is not sufficient to only fix the lowest out-of-balance node in tree
  • Deletion reduces height
  • Rotations to rebalance also reduce height
  • Rebalancing does not "undo" the change in height caused by deletion

AVL Trees: Other potential modifications

  • Mark a node "deleted" and leave it in the tree for logical deletes
  • Performance degrades over time and cleanup is needed later for Amortized performance
  • Only store the difference in height from parent, to avoid storing the height in every node

Next Week

  • More Trees and Other Augmentations
  • Examples of other forms of trees
  • Introduction to hashing

Studying That Suits You

Use AI to generate personalized quizzes and flashcards to suit your learning preferences.

Quiz Team

Related Documents

AVL Trees Data Structures PDF

More Like This

Use Quizgecko on...
Browser
Browser