Self-Balancing Trees: AVL and B-Trees

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

In the average case, what is the time complexity for searching in a Binary Search Tree (BST)?

  • O(log n) (correct)
  • O(n)
  • O(n^2)
  • O(1)

What is a primary limitation of BSTs that self-balancing trees aim to address?

  • Inability to store duplicate keys
  • Degenerate tree leading to linear time complexity. (correct)
  • Difficulty in implementing deletion
  • High memory usage

Which of the following is a characteristic of a self-balancing tree?

  • It becomes degenerate over time.
  • It is always a complete tree like a heap.
  • It ensures the worst-case search time remains logarithmic. (correct)
  • Requires global rebalancing after each insertion.

Which condition must be true for all nodes in an AVL tree?

<p>The height difference between the left and right subtrees must be no more than 1. (C)</p> Signup and view all the answers

What does the 'balance factor' in an AVL tree represent?

<p>The difference in height between the left and right subtrees. (A)</p> Signup and view all the answers

Why are alterations to an AVL tree structure after insertion or deletion considered 'on-the-fly'?

<p>Because no expensive rebalancing of the whole tree is ever required. (C)</p> Signup and view all the answers

What is the range of possible balance factors for a node in an AVL tree immediately after an insertion but before rebalancing?

<p>{-2, -1, 0, 1, 2} (D)</p> Signup and view all the answers

Why is the AVL tree considered superior to the BST?

<p>It guarantees logarithmic time complexity for worst-case scenarios. (D)</p> Signup and view all the answers

What is the computing cost for insert and delete operations once the appropriate tree position is reached in an AVL Tree?

<p>Constant, O(1) (D)</p> Signup and view all the answers

How is the height of a subtree used in AVL trees?

<p>To calculate balance factors for each node. (D)</p> Signup and view all the answers

Considering the four imbalance scenarios (LL, RR, LR, RL) in AVL trees, which cases require a single rotation to rebalance the tree?

<p>Only LL and RR (B)</p> Signup and view all the answers

Who are the computer scientists credited with the proposal of the first self-balancing tree, known as the AVL tree?

<p>Adelson-Velskii and Landis (B)</p> Signup and view all the answers

What is the primary purpose of rotations in AVL trees?

<p>To balance the height of the left and right subtrees. (D)</p> Signup and view all the answers

What is the maximum number of data items that a node can hold in a B-Tree if the max out degree is m?

<p>m - 1 (A)</p> Signup and view all the answers

Which of the following scenarios can lead to the split of a node during insertion in a B-Tree?

<p>The node is full. (B)</p> Signup and view all the answers

What is the primary characteristic of leaf nodes in a B-Tree?

<p>All leaf nodes are on the same level. (B)</p> Signup and view all the answers

Why are B-Trees widely used in database systems?

<p>The height is very small for the amount of data held because each node holds multiple items. (A)</p> Signup and view all the answers

What does the term 'locality of reference' refer to in the context of B-Trees?

<p>The probability of a search finding the item in the currently accessed node (D)</p> Signup and view all the answers

What is the return value when the search algorithm in a B-Tree reaches a leaf node without finding the key?

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

In a B-Tree, which nodes must be at least half full?

<p>The interior and leaf nodes. (C)</p> Signup and view all the answers

Which step is performed first in the B-Tree insertion routine?

<p>Finding the correct position to insert the value. (B)</p> Signup and view all the answers

What action is taken during B-Tree insertion if the added value causes a node overflow?

<p>The node is split into two siblings. (C)</p> Signup and view all the answers

In B-Tree deletion, when can a value be borrowed from a sibling node?

<p>When the node would remain at least half full after borrowing. (A)</p> Signup and view all the answers

What happens during B-Tree deletion if borrowing isn't possible and an underflow occurs?

<p>The underfilled node is combined with a sibling to form a full node. (A)</p> Signup and view all the answers

What is the time complexity for insert, search, and delete operations in a B-Tree?

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

Which B-Tree variant requires that all nodes, except the root, are at least 2/3 full?

<p>B* Tree (A)</p> Signup and view all the answers

In which B-Tree variant is data stored only in the leaf nodes, with interior nodes storing only keys?

<p>B+ Tree (D)</p> Signup and view all the answers

What is the advantage of storing data only in leaf nodes in a B+ tree?

<p>Higher locality of reference and efficient sequential access (B)</p> Signup and view all the answers

Which of the following data structures maintain O(log n) time complexity for insert, search, and delete operations?

<p>B-Tree (D)</p> Signup and view all the answers

The B-Tree data items inside each node are

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

What is the purpose of the counter in a B-Tree node?

<p>To indicate how many data items the node currently holds. (D)</p> Signup and view all the answers

What is a key difference between B-Trees and Binary Search Trees (BSTs) in terms of node capacity?

<p>B-Tree nodes can hold multiple keys, while BST nodes can hold only one. (B)</p> Signup and view all the answers

Which factor is essential for determining whether a rotation is needed in an AVL tree after an insertion or deletion?

<p>The balance factor of the nodes. (A)</p> Signup and view all the answers

Which factor contributes most to the performance benefits of B-Trees when working with large datasets on disk?

<p>The reduced tree height and high locality of reference. (C)</p> Signup and view all the answers

When referring to the 'max out degree' of a B-Tree, what aspect of the tree's structure is being described?

<p>The maximum number of children a node can have. (C)</p> Signup and view all the answers

In the context of data structures and algorithms, what does the phrase 'constant differences in performance' highlight?

<p>Algorithms where the performance difference is independent of input size. (B)</p> Signup and view all the answers

What is indicated when the current node is determined to be a leaf during a B-Tree search operation?

<p>The search terminates, and a value indicating 'not found' can now be returned. (C)</p> Signup and view all the answers

What is the significance of ensuring data items inside each B-Tree node are internally sorted?

<p>Speeds up the process of searching for a specific key within the node. (C)</p> Signup and view all the answers

In terms of algorithmic analysis, what does Big-O notation primarily focus on?

<p>How an algorithm's performance scales with increasing input size. (A)</p> Signup and view all the answers

Flashcards

BST Average Case Performance

BSTs have good average case performance because the number of steps to reach any node is logarithmically related to the number of nodes, O(log n).

BST Degenerate Performance

When a BST becomes degenerate, its worst-case performance is linearly related to the number of nodes, O(n), like a linked list.

Self-Balancing Trees

Self-balancing trees use additional logic during insertion and deletion to prevent degeneration, maintaining a broad, triangular shape.

Examples of Self-Balancing Trees

Two well-known types of self-balancing trees are AVL trees and B-Trees.

Signup and view all the flashcards

What are AVL Trees?

The first self-balancing tree, named after its Soviet inventors Adelson-Velskii and Landis, proposed in 1962

Signup and view all the flashcards

AVL Tree Rule

AVL trees are BSTs where the height difference between the left and right subtrees for all nodes is less than or equal to k, where k = 1.

Signup and view all the flashcards

Balance Factor in AVL Trees

In an AVL tree, each node stores a balance factor indicating the height difference between its left and right subtrees.

Signup and view all the flashcards

Advantage of AVL Tree

The AVL tree is superior to the BST because the worst-case number of hops is logarithmically related to the number of nodes.

Signup and view all the flashcards

AVL Tree Adaptability

Alterations to the AVL tree structure that follow insert or delete are made on-the-fly

Signup and view all the flashcards

Efficiency of insert and delete in AVL Trees

Once you reach the appropriate position, insert and delete have constant computing cost independent of the total number of nodes in the AVL tree.

Signup and view all the flashcards

Balance Factor Calculation

The balance factor of a node is calculated as the height of its right subtree minus the height of its left subtree.

Signup and view all the flashcards

AVL Imbalance Scenarios

AVL tree imbalances fall into four scenarios: Left-Left (LL), Right-Right (RR), Left-Right (LR), and Right-Left (RL).

Signup and view all the flashcards

What are B-Trees?

B-Trees were introduced in 1972 by Bayer and McCreight of Boeing; used in databases, filesystems, and search engines.

Signup and view all the flashcards

What is m?

The number 'm', called the max out degree, of the B-Tree

Signup and view all the flashcards

B-Tree leaf nodes

All leaf nodes are on the same level in B-Trees

Signup and view all the flashcards

B-Tree Sorting

Data items inside each B-Tree node are internally sorted.

Signup and view all the flashcards

B-Tree Use Cases

If the storage of B-Tree nodes requires frequent searching (e.g., in a database), B-Trees are widely used

Signup and view all the flashcards

Node Capacity in B-Trees

Nodes in B-Trees hold multiple items; results in a small height for the amount of data held which are benificial for database storage

Signup and view all the flashcards

B-Tree Memory Access

Locality of reference of a B-Tree node is matched in size to a disk read block

Signup and view all the flashcards

Contents of a B-Tree Node

A counter to indicate how many data items the node holds; a flag if this node is a leaf or not; space for pointers to child nodes.

Signup and view all the flashcards

B - Tree Node Content Space

The B-tree node contains space for space for m – 1 data items.

Signup and view all the flashcards

B-Tree Search Algorithm

Set current node to root, scan node items until finding the key/larger key, and traverse to the appropriate child, or return -1 if a leaf.

Signup and view all the flashcards

B-Tree insert routine

The B-Tree is searched, then the value is inserted into the list in maintaining sorted order. If the added value overflows, then the current node splits into sibling and values get put in their sibling.

Signup and view all the flashcards

B-Tree Delete - Interior Node

In a B-Tree, If deleting a value from an interior node Borrow the value (largest item from the right node or the smallest item from the left node).

Signup and view all the flashcards

B-Tree Leaf- Delete

If the remaining number of items in the node is < D, then an UNDERFLOW has occurred. Then Borrow from neighbour.

Signup and view all the flashcards

B-Tree Performance

A B-tree has O(log n) time complexity in the average and worst cases for insert, search and delete.

Signup and view all the flashcards

Big-O Limitations

Big-O analysis may not differentiates between data structures and algorithms that have constant differences in performance between each other

Signup and view all the flashcards

What are B* Trees?

B* Trees are Similar to B-Trees, except that nodes other than the root must be at least 2/3 full instead of 1/2 full

Signup and view all the flashcards

What are B+ Trees?

B+ Trees can store data only in leaf nodes, with interior nodes storing only keys

Signup and view all the flashcards

B+ Trees - All Data

Storing all leaf nodes sequentially on disk and hopping from one leaf to the next will visit all data

Signup and view all the flashcards

Study Notes

BST Performance Recap

  • BSTs offer efficient performance for average-case scenarios
  • The number of steps to reach any node is logarithmically related to the number of nodes i.e. 𝒪(log 𝑛)
  • Average-case performance is superior to linked lists, as linked lists require a linear number of steps i.e. 𝒪(𝑛)
  • Degenerate BSTs lead to worst-case performance, which, like linked lists, become linearly related i.e. 𝒪(𝑛)

Self-Balancing Trees

  • Self-balancing trees address the issues of degenerate trees
  • Two well-known types of self-balancing trees include AVL trees and B-Trees
  • Self-balancing trees use special logic during insert and delete to ensure the tree remains broad and triangular, avoiding degeneration
  • This optimization keeps the steps to reach a node logarithmically related to the number of nodes, denoted as 𝑛, even in the worst-case scenario

AVL Trees

  • The "Adelson-Velsky Landis" tree was the first self-balancing tree
  • AVL trees were introduced in 1962
  • They are named after Georgy Maximovich Adelson-Velskii and Evgenii Mikhailovich Landis, two Soviet computer scientists

AVL Tree Rules

  • An AVL tree is a BST where the height difference between the left and right subtrees for all nodes is ≤ 1
  • Expressed as |ℎ(subtreeright) − ℎ(subtreeleft)| ≤ 𝑘, where ℎ represents the height of a tree
  • For AVL trees, 𝑘 = 1, where k is called the order of the tree
  • Every node in an AVL tree has a balance factor, along with its key and data
  • The balance factor is ∈ ℤ: −2 ≤ 𝑥 ≤ 2, contingent on whether the right subtree's height is >, =, or than the left subtree

AVL Tree Advantages

  • AVL trees are better than BSTs
  • The worst-case number of hops needed to reach any node in a search are logarithmically related to total nodes
  • Alterations to the tree's structure following insertions or deletions are made on-the-fly
  • Once the node position is determined, insertions and deletions maintain a constant computing cost, regardless of the tree's size

Subtree Heights & Balance Factors

  • Subtree heights are used to determine the balance factors of each node
  • balance factor = ℎ subtreeright − ℎ(subtreeleft )
  • A balance factor of -1: the height of the left subtree is greater than the right subtree by 1
  • A balance factor of 0: the height of the left subtree is equal to the height of the right subtree
  • A balance factor of +1: the height of the left subtree is less than the height of the right subtree by 1

Imbalance Scenarios

  • There are four imbalance scenarios that can occur in AVL trees
  • These scenarios are Left-Left (LL) case, Right-Right (RR) case, Left-Right (LR) case and Right-Left (RL) case.

Performance Summary of AVL Trees

  • AVL trees have 𝒪(log 𝑛) time complexity for search, insert and delete
  • Time complexity applies to both the average and the worst case
  • Degenerate trees cannot occur in AVL trees
  • Since costly total tree re-balancing isn't needed, re-balancing takes a constant time

Summary Table of AVL Tree Performance

  • Successful insert, delete and search operations all have a complexity of 𝒪(log 𝑛) for best, average, and worst cases
  • Successful search operations have a best case complexity of 𝒪(1)
  • Unsuccessful insert operations have a complexity of N/A for best, average, and worst cases
  • Unsuccessful delete and search operations all have a complexity of 𝒪(log 𝑛) for best, average, and worst cases

B-Trees

  • B-Trees were introduced in 1972 by Bayer and McCreight, both working at Boeing
  • They are commonly used in databases, file systems, and search engines

B-Tree Rules

  • The maximum out degree of the B-Tree is defined as 𝑚
  • The root node can have between 1 and 𝑚 − 1 number of data items
  • Interior and leaf nodes must be at least half full, holding up to 𝑚 − 1 items
  • All leaf nodes reside on the same level
  • B-Tree nodes can have a range of 0 to 𝑚 children, This makes them a 𝑘-ary tree
  • Data items are internally sorted with the B-Tree node, which is managed during the insert process

B-Tree Properties

  • B-Trees’ height is minimize through the increased items held in each node
  • Widely used for searching large datasets, especially within databases, and with secondary disk storage
  • Exhibit high locality of reference, where similar data is stored nearby, often inside the same node

B-Tree Node Structure

  • Each B-Tree node holds approximately 𝑚 − 1 data items
  • Each B-Tree node holds a counter to keep track of how many data items the node currently holds
  • Each B-Tree node uses flag if the node is a leaf or not
  • Each B-Tree node uses holds 𝑚 pointers to point towards up to 𝑚 child nodes

B-Tree Nodes

  • With 𝑚 = 6, the maximum data items each node can hold is 5
  • Given 𝑚 = 6, the maximum child nodes for an interior node is 6

B-Tree Search Operation

  • Start at the root node to initialize the search
  • Sequentially search for items in the current node until the target key is found
  • If the current node is a leaf, and after failing to locate key, return -1, thus indicating search failure
  • Set the current node to the child node to the left of the item that surpasses the sought item based on the key

Successful Search Operation

  • The target key of 41 is being searched for in the example B-Tree
  • Since 30 is less than the search target 41, the next key is advanced to in the array of keys
  • Key 38 is also less than the target, advancing the key again
  • The next key 42 is larger than the search, so the child pointer is trailed between 38 and 42, since the root node is not a leaf
  • Advancing again, the process is preformed on the current node, where key of 40 is smaller than the search target
  • The next key in the current node matches the target, so the stop is a successful event

Unsuccessful Search Operation

  • The target is a search of 36 in the example B-Tree
  • The second position of the root is larger, at 38, so the child node is followed, since it is not a leaf
  • Since 32 is smaller, proceed to key 34, where the process is repeated, and it determined that there is a failure to locate the target
  • Having now reached the array’s end, since the node is marked as the process is ended

B-Tree Insert Operation

  • A B-Tree insert operation is demonstrated for new data, assuming that 𝑚 = 5
  • Perform the initial inserts: 20, 40, 10
  • Inserting 30 and 50 splits the root into two siblings
  • Push the middle key up into the new root after sorted-order
  • Continue by filling the child nodes by inserting 25, 42, and 44
  • The next insert of a target of 41 ends up splitting the existing full node into two siblings
  • Ensure the parent node is not full, such that the median key is pushed up into the parent

B-Tree Insert Routine

  • Perform Insertion by the means of searching to determine to correct value position
  • Add a value into a list in the correct position, after array shuffling as required
  • Handle a value entry overflow, by preforming the sequence: Split the current node towards two siblings. Assign smaller values assign to the left sibling, and assign the higher numbers into the right sibling
  • With the above nodes in the case were they a root node, after splitting, make an additional root node push the previous element into the new root
  • If not a root node during splitting, assign the middle value, in this case, push it to the parent node, and follow to step B to continue or create an extra root node if vacant space if needed

B-Tree Delete Scenarios

  • After deleting 56 and 7, the tree removes and shuffles remaining items
  • If possible, you can borrow an item from either sibling with the goal of having the node stay approximately half full
  • Then the node gets borrowed from it's left sibling
  • With the target of deleting 34, there is no way borrow a respective sibling, so both the left and right siblings are then combined, and then 38 can be pulled from original parent

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 Trees in Data Structures
8 questions

AVL Trees in Data Structures

AppropriateQuatrain3057 avatar
AppropriateQuatrain3057
AVL Trees: Self-Balancing Binary Search Trees
42 questions
Use Quizgecko on...
Browser
Browser