AVL Trees: Balancing and Rotations

Choose a study mode

Play Quiz
Study Flashcards
Spaced Repetition
Chat to Lesson

Podcast

Play an AI-generated podcast conversation about this lesson
Download our mobile app to listen on the go
Get App

Questions and Answers

When does an AVL tree perform rebalancing (rotation)?

  • When a node has a balance factor outside the range of [-1, 1]. (correct)
  • Upon deletion of a leaf node.
  • When a node has a balance factor outside the range of [-2, 2].
  • When inserting a node.

What is the primary goal of balancing trees?

  • To minimize memory usage.
  • To reduce the number of nodes in the tree.
  • To optimize for specific hardware configurations.
  • To maintain O(log n) time complexity for search operations. (correct)

What is a 'balance factor' in the context of AVL trees?

  • The number of nodes in a subtree.
  • The ratio of nodes in the left subtree to the right subtree.
  • The difference in height between the left and right subtrees of a node. (correct)
  • The number of rotations performed on a node.

When inserting a node in a Red-Black tree, what color is the node initially?

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

What is the maximum number of elements a node in a 2-4 tree can hold?

<p>3 (B)</p> Signup and view all the answers

What is the time complexity for searching in a balanced AVL tree?

<p>O(log n) (D)</p> Signup and view all the answers

What is the term used when a node in a 2-4 tree temporarily holds more elements than its maximum capacity during insertion?

<p>Overflow (B)</p> Signup and view all the answers

Which of the following is a property of Red-Black Trees?

<p>No red node can have a red child. (A)</p> Signup and view all the answers

What operation is primarily used to maintain balance in AVL trees?

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

In a 2-4 tree, what happens when a node becomes full during insertion?

<p>The node splits, and the middle element is promoted to the parent node. (D)</p> Signup and view all the answers

What is the 'black height' of a Red-Black tree?

<p>The number of black nodes on any path from the root to a leaf node. (A)</p> Signup and view all the answers

In AVL tree rotations, if the imbalance occurs due to an insertion in the left subtree of the left child of a node, which rotation is required?

<p>Right rotation (B)</p> Signup and view all the answers

Which of the following tree types allows nodes to have between 2 and 4 children?

<p>2-4 Tree (C)</p> Signup and view all the answers

What is the time complexity for inserting n elements into an AVL tree?

<p>O(n log n) (B)</p> Signup and view all the answers

When a 2-4 tree node overflows during insertion, which element is promoted to the parent node?

<p>The middle element (C)</p> Signup and view all the answers

What is the primary purpose of color assignment (red or black) in Red-Black trees?

<p>To ensure that the tree remains balanced after insertions and deletions. (A)</p> Signup and view all the answers

In an AVL tree, which of the following operations might trigger a rotation?

<p>Insertion (B)</p> Signup and view all the answers

In a 2-4 tree, an internal node has three elements. How many children does it have?

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

Consider a Red-Black tree. What can you say about the number of black nodes on all paths from the root to leaf nodes?

<p>They must be the same. (B)</p> Signup and view all the answers

You are constructing a 2-4 tree by inserting the following values: 11, 14, 19, 25. After the insertion of 25, what operation must occur?

<p>A node split (promotion). (A)</p> Signup and view all the answers

Which scenario would necessitate a Double Left (Right Left) rotation in an AVL tree?

<p>Insertion into the right subtree of the left child. (B)</p> Signup and view all the answers

In a Red-Black tree, what is the implication if a node is red?

<p>The parent must be black. (D)</p> Signup and view all the answers

A 2-4 tree contains nodes with the values [10, 20, 30]. After inserting the value '25', which action is performed?

<p>The node splits, promoting the median and distributing the remaining values. (A)</p> Signup and view all the answers

When implementing an AVL tree, why is it important to update the balance factor of nodes after an insertion?

<p>To detect imbalances and trigger rotations. (B)</p> Signup and view all the answers

Which of the following is a potential outcome of inserting a new node into a Red-Black tree?

<p>A red node might become the parent of another red node. (A)</p> Signup and view all the answers

What is the maximum possible height of an AVL tree with N nodes?

<p>1.44log2(N) (C)</p> Signup and view all the answers

Suppose you insert the following elements into a 2-4 tree in the specified order: 5, 10, 15, 20, 25, 30. How many times will a node split occur?

<p>2 (D)</p> Signup and view all the answers

During the construction of a Red-Black tree, what immediate action is typically performed if a newly inserted red node has a red parent?

<p>A single or double rotation and/or recoloring are performed. (D)</p> Signup and view all the answers

An AVL tree is frequently used when:

<p>Data is rarely modified. (C)</p> Signup and view all the answers

In a 2-4 tree, an internal node has two elements. How many external nodes (leaves) should it have?

<p>3 (B)</p> Signup and view all the answers

If you are given the task of designing a search structure where frequent insertions and deletions are expected, which tree structure might be preferred?

<p>Red-Black tree (C)</p> Signup and view all the answers

Given a sorted array, which of the following tree structures would most closely resemble a completely unbalanced tree if the elements were inserted in order?

<p>Binary search tree (D)</p> Signup and view all the answers

How does the concept of 'promotion' apply in 2-4 trees during insertion?

<p>It involves moving a median value from a full child node up to its parent. (C)</p> Signup and view all the answers

While constructing a Red-Black tree, the number of comparisons is $O(n log(n))$. What factor dominates this complexity?

<p>Searching the correct location, and balancing afterwards for node insertions (B)</p> Signup and view all the answers

Which of the following statements best describes the primary difference in approach between AVL trees and Red-Black trees?

<p>AVL trees always ensure perfect balance at the cost of more rotations, while Red-Black trees allow slight imbalances for fewer adjustments. (C)</p> Signup and view all the answers

You are using a 2-4 tree to store a large dataset. As the tree grows, you notice frequent node splits occurring. Which strategy below would likely REDUCE the number of node splits?

<p>Increasing the maximum number of elements allowed in each node (B)</p> Signup and view all the answers

In Red-Black tree terminology, what is Case 1 used for with Red-Black trees?

<p>Recoloring. (C)</p> Signup and view all the answers

Flashcards

Balancing Trees

Trees that automatically rearrange themselves during insertion to maintain a reasonable balance, preserving binary search tree properties.

AVL Trees

A self-balancing binary search tree that uses balance factors to ensure the tree remains balanced.

Balance Factor

The calculation on each node which determines the difference in heights between the left and right subtrees.

Tree Rotations

Restructuring the tree to maintain balance after insertion or deletion, performed when a node's balance factor is outside the range of [-1, 1].

Signup and view all the flashcards

Single Left (SL) Rotation

A type of rotation where the tree is restructured to address an imbalance.

Signup and view all the flashcards

Single Right (SR) Rotation

A type of rotation where the tree is restructured to address an imbalance.

Signup and view all the flashcards

Double Rotation

A rotation involving two steps to rebalance the tree.

Signup and view all the flashcards

Double Left (Right Left) Rotation

Double rotation where the tree moves down to the right and then to the left from the unbalanced node.

Signup and view all the flashcards

Double Right (Left Right) Rotation

Double rotation where the tree moves down to the left and then to the right from the unbalanced node.

Signup and view all the flashcards

2-4 Trees

A self-balancing tree where each node can hold 1 to 3 elements and can have 2 to 4 children.

Signup and view all the flashcards

Overflow

Occurs in 2-4 trees when a node tries to hold more than 3 elements, requiring a split.

Signup and view all the flashcards

Promotion

The process of moving the middle element of an overfull node up to the parent node, splitting the node into two.

Signup and view all the flashcards

Red-Black Trees

A self-balancing binary search tree where each node is colored red or black, ensuring balance by obeying specific rules.

Signup and view all the flashcards

Red-Black Tree Rule 1

The root node must always be black.

Signup and view all the flashcards

Red-Black Tree Rule 2

No red node can have a red child or parent.

Signup and view all the flashcards

Red-Black Tree Rule 3

All paths from a given node to any of its descendant NIL nodes contain the same number of black nodes.

Signup and view all the flashcards

Black Height

The number of black nodes on any path from the root to a leaf, including the root and the leaf.

Signup and view all the flashcards

Red-Black Insertion

New nodes are typically inserted as red.

Signup and view all the flashcards

Recoloring (Red-Black Trees)

Adjusting node colors to maintain red-black tree properties after insertion.

Signup and view all the flashcards

Search Time Complexity

O(log n)

Signup and view all the flashcards

Insertion Time Complexity

O(log n)

Signup and view all the flashcards

Building Tree Time Complexity

O(n log n)

Signup and view all the flashcards

Deletion Time Complexity

O(log n)

Signup and view all the flashcards

Study Notes

Balancing Trees Overview

  • Balancing aims to maintain the properties of binary search trees
  • Insertion into balancing trees automatically rearranges the tree to maintain a balance
  • Three balancing trees include: AVL Trees, 2-4 Trees, and Red-Black Trees
  • Balanced search trees help maintain a time complexity of O(log n)

AVL Trees

  • AVL Trees calculate a "balance factor" for each node
  • Rotations are performed based on insertions or tree modifications
  • Rotations are done to update the trees balance factor
  • Reference material for visualization of AVL Trees: AVL Tree Notes (slide 110) from Abdul Bari Notes

Balance Factors & Rotations

  • Every node has a “balance factor” that is calculated as: balancing factor = hL - hR
  • “hL” represents the height of the left subtree and “hR” represents the height of the right subtree.
  • AVL Tree rebalances itself (performs a rotation) when a node's balance factor is outside of the inclusive range from [-1, 1]
  • Common rotation types include: Single Left(SL), Single Right(SR), Double Left/Right(DL/RL) , Double Right/Left(DR/LR)

Tree Rotations

  • Tree rotations can be visualized like strings pulling the unbalanced tree in a direction and the nodes following
  • Rebalancing occurs as nodes are inserted
  • Rebalancing occurs is an operation on a tree that has only just become unbalanced

Single Left (SL) Rotations

  • After inserting a node and seeing a balance factor that is outside of the range [-1,1], the tree moves downward to the right
  • A Single Left Rotation is performed on the tree and a balanced state is achieved
  • The unbalanced node is observed and the string is pulled downward to the left of the unbalanced node

Single Right (SR) Rotations

  • A balance factor that is outside of the range [-1,1] is observed
  • Because the tree moves downward to the left, a single right rotation on the tree is performed
  • To perform this rotation, the unbalanced node (C) is looked at, and the string is pulled downward and to the right of C

Double Rotations

  • Two single rotations are performed
  • One single rotation moves the node to the point where another rotation can be performed
  • The final rotation balances the tree.

Double Left (Right Left) Rotations

  • On the unbalanced node (A) the tree moves downward to the right, and then down to the left
  • This action results in a double left rotation, or a right rotation that is followed by a single left rotation.
  • Imagine “nailing” the string between A and C, A will not move
  • If we pull a string that is over B and C to the right and downward, B will follow upward to C, and C will move to the right and down.

Double Right (Left Right)

  • On the unbalanced node (C) the tree moves down to the left, and from there, down to the right
  • This action results in a double right rotation, or a left rotation followed by a single right rotation
  • Imagine nailing the string between C and A, and it will not move
  • If the string is pulled over A and B to the left and downward, then B will follow upward to A, and A will move to the left and down

Building AVL Trees

  • An extremely important concept to understand is building an AVL tree with given values ​​and performing balances as necessary.
  • Given n input values in a specific order to be inserted, one must construct the tree and balance whenever necessary
  • When visually or definitionally understanding what rotations are done, one needs to know how to recognize them as they're happening.
  • AVL Tree Insertion (slide 118)

2-4 Trees Main Properties

  • A 2-4 tree has multiple nodes possessing up to 3 elements
  • If a node has 1 element, it is considered a 2-node
    • If internal, a 2-node has two children nodes
  • If a node has 2 elements, it is considered a 3-node.
    • If internal, a 3-node has three children nodes.
  • If a node has 3 elements, it is considered a 4-node
    • If internal, a 4-node has four children nodes
  • All external (leaf) nodes possess the same depth

2-4 Tree Construction

  • Insertion results in elements being placed in the same node
  • To construct a tree with the following elements in order: 11, 14, 19, 25, 1, 3, 5, 18, 7, 96

2-4 Insertion & Overflow

  • You can only have a maximum of three elements
  • Inserting a fourth element is an overflow.
  • To deal with an overflow, promote the third and split the sides
    • The first and second element moves to a new tree to the bottom left
    • The third element will be promoted and become the new root of the tree
    • The fourth element move to the a new node to the bottom right
  • The process of insertion is continued on the bottom level

2-4 Tree Examples

  • Following a series of performed “promotions” in order to make sense of the current 2-4 tree, you can evaluate as follows:
    • Evaluate values in reference to each other by the parent element's value:
      • Values 1, 3 which are <5
      • Values 7 between 5-11
      • Values 14, 18 between 11-19
      • Values 25 which are >19

Red Black Trees Rules

  • The root is always black
  • No red node has a child/parent that is also red
  • The path from the root to any black leaf node should be the same for all paths to any given black leaf node
    • Can be found using the "black height" of a tree

Black Height

  • Black height is the total number of black nodes on any given path from the root to a black leaf node, this includes the root and leaf.
  • Compare this value of nodes to any other path to a black leaf node, you should also find the same number.
  • This will validate the third condition of the red black tree

Red Black Trees Construction Cases

  • When inserting a node, it is always inserted as red
  • Three cases include:
    • Recoloring
    • Double Rotation
    • Single Rotation & Recolor

Red Black Trees Recolor Case

  • When inserting a node with an initially red parent and the sibling node is also a red:
    • The siblings are red
    • Swap the colors of the siblings and their parent

Red Black Trees Double Rotation Case

  • If inserting a node and it has a red parent and a black sibling (or null in this case)
    • Perform a single left or single right rotation (which turns it into a Case 3)
    • Perform another rotation to balance the tree (you will see this is Case 3)
    • Update the new root as black

Red Black Trees Recolor Case

  • If the tree has a red parent:
    • Rotate the tree by performing a single left or single right rotation (depending on the case)
    • Properly recolor the tree (C1)

Red Black Trees: Constructing

  • The number of comparisons building a red-black tree with n items is O(nlog(n))

Time Complexities

  • Search has a Big O of O(log n)
    • Time complexity of finding a max / min value is O(log n)
  • Insertion to first insert a node, one must know where to position the node, which requires a O(log n) search operation, and then can place the node
    • Overall, it is an O(log n) operation in itself
    • To insert n elements, it would be O(n log n) as for nodes one must search (log n) for where to put those nodes, and then place them
  • Deletion:
    • Similar to insertion, deleting a node requires finding it, and has a Big O of O(log n).

Big O Extra Notes

  • Make these make intuitive sense to you.
  • The differing types of balancing trees have pros and cons - insertion and deletion complexity may vary slightly.
  • AVL trees will likely use a lot of operations to maintain balance.
  • red-black tree, may not be as balanced, uses less operations to maintain structure.

Studying That Suits You

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

Quiz Team

Related Documents

More Like This

AVL Search Trees Overview
38 questions

AVL Search Trees Overview

IntriguingLogic6172 avatar
IntriguingLogic6172
AVL Trees and Balance Criteria Quiz
45 questions
AVL Trees in Data Structures
8 questions

AVL Trees in Data Structures

AppropriateQuatrain3057 avatar
AppropriateQuatrain3057
Use Quizgecko on...
Browser
Browser