Bird
Raised Fist0
Data Structures Theoryknowledge~5 mins

B-trees for databases in Data Structures Theory - Cheat Sheet & Quick Revision

Choose your learning style10 modes available

Start learning this pattern below

Jump into concepts and practice - no test required

or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Recall & Review
beginner
What is a B-tree in the context of databases?
A B-tree is a balanced tree data structure that keeps data sorted and allows searches, sequential access, insertions, and deletions in logarithmic time. It is widely used in databases to organize and quickly access large amounts of data stored on disk.
Click to reveal answer
intermediate
Why are B-trees preferred for database indexing over binary search trees?
B-trees are preferred because they reduce the number of disk reads by having nodes with many keys and children, which means the tree is shorter and wider. This structure minimizes slow disk access, making data retrieval faster compared to binary search trees that have many levels.
Click to reveal answer
beginner
What does it mean that a B-tree is 'balanced'?
A B-tree is balanced because all leaf nodes are at the same depth. This means every path from the root to a leaf has the same length, ensuring consistent and predictable access times for all data entries.
Click to reveal answer
intermediate
How does a B-tree handle insertions when a node is full?
When a node is full, the B-tree splits the node into two nodes and moves the middle key up to the parent node. This splitting may propagate up the tree, keeping the tree balanced and maintaining its properties.
Click to reveal answer
beginner
What is the main advantage of having multiple keys in each node of a B-tree?
Having multiple keys per node reduces the tree's height, which means fewer disk accesses are needed to find a key. This improves performance because accessing disk is slower than accessing memory.
Click to reveal answer
What property ensures that all leaf nodes in a B-tree are at the same level?
ARecursion
BBalance
CUniqueness
DOrdering
Why do B-trees have nodes with multiple keys?
ATo reduce tree height and disk reads
BTo make the tree binary
CTo store duplicate keys
DTo increase tree depth
What happens when a B-tree node becomes full during insertion?
AThe node splits and the middle key moves up
BThe node deletes the oldest key
CThe tree becomes unbalanced
DInsertion is rejected
Which of these is NOT a typical operation supported efficiently by B-trees?
ASearch
BDeletion
CSorting all keys in linear time
DInsertion
What is the main reason B-trees are used in databases?
ATo simplify programming
BTo use less memory
CTo store images
DTo minimize slow disk access
Explain how a B-tree maintains balance and why this is important for database performance.
Think about how the tree grows and stays even.
You got /4 concepts.
    Describe the advantages of using B-trees over binary search trees in database indexing.
    Focus on how data is stored and accessed on disk.
    You got /4 concepts.

      Practice

      (1/5)
      1. What is the main purpose of a B-tree in databases?
      easy
      A. To compress data to save disk space
      B. To keep data sorted and balanced for fast searching and updating
      C. To encrypt data for security
      D. To store data in a linear list for quick access

      Solution

      1. Step 1: Understand B-tree structure

        B-trees organize data in a sorted and balanced tree structure.
      2. Step 2: Identify the purpose in databases

        This structure allows fast searching, insertion, and deletion by minimizing tree height and disk reads.
      3. Final Answer:

        To keep data sorted and balanced for fast searching and updating -> Option B
      4. Quick Check:

        B-tree purpose = fast, balanced data access [OK]
      Hint: B-trees balance data for speed, not encryption or compression [OK]
      Common Mistakes:
      • Confusing B-trees with simple lists
      • Thinking B-trees encrypt data
      • Assuming B-trees compress data
      2. Which of the following correctly describes a property of B-tree nodes?
      easy
      A. Each node can contain multiple keys and multiple children
      B. Nodes contain only keys but no children
      C. Each node contains exactly one key and two children
      D. Nodes are always leaf nodes without children

      Solution

      1. Step 1: Recall B-tree node structure

        B-tree nodes hold multiple keys to reduce tree height.
      2. Step 2: Understand children count

        Each node has one more child than the number of keys it holds.
      3. Final Answer:

        Each node can contain multiple keys and multiple children -> Option A
      4. Quick Check:

        Multiple keys and children per node = C [OK]
      Hint: B-tree nodes hold many keys and children, not just one [OK]
      Common Mistakes:
      • Thinking nodes have only one key
      • Assuming nodes have no children
      • Confusing B-trees with binary trees
      3. Consider a B-tree of order 3 (each node can have at most 2 keys). If a node currently has keys [10, 20] and a new key 15 is inserted, what will happen?
      medium
      A. The key 15 will be discarded as duplicates are not allowed
      B. The node will hold keys [10, 15, 20] without splitting
      C. The node will split because it exceeds the max keys, promoting a key up
      D. The tree will become unbalanced and require rebalancing later

      Solution

      1. Step 1: Check node capacity for order 3 B-tree

        Max keys per node = 2. Current keys are [10, 20]. Inserting 15 adds a third key.
      2. Step 2: Understand insertion rules

        When a node exceeds max keys, it splits and promotes the middle key to the parent.
      3. Final Answer:

        The node will split because it exceeds the max keys, promoting a key up -> Option C
      4. Quick Check:

        Node over capacity causes split and promotion [OK]
      Hint: If keys exceed max, node splits and middle key moves up [OK]
      Common Mistakes:
      • Thinking node can hold 3 keys without splitting
      • Assuming duplicates are discarded here
      • Believing tree becomes unbalanced without immediate fix
      4. A B-tree node is supposed to split when it exceeds its maximum keys. Which of the following is a common mistake that can cause the tree to become unbalanced after insertion?
      medium
      A. Not promoting the middle key to the parent node after splitting
      B. Always inserting keys in sorted order
      C. Using nodes with multiple keys and children
      D. Searching for keys before insertion

      Solution

      1. Step 1: Understand node splitting in B-trees

        When a node splits, the middle key must be promoted to keep the tree balanced.
      2. Step 2: Identify the error impact

        If the middle key is not promoted, the tree structure breaks and becomes unbalanced.
      3. Final Answer:

        Not promoting the middle key to the parent node after splitting -> Option A
      4. Quick Check:

        Missing promotion causes imbalance [OK]
      Hint: Always promote middle key on split to keep balance [OK]
      Common Mistakes:
      • Skipping promotion step after split
      • Thinking sorted insertion causes imbalance
      • Confusing node structure rules
      5. You have a B-tree of order 4 (max 3 keys per node). After several insertions, a leaf node has keys [5, 10, 15] and you want to insert 12. Describe the sequence of steps the B-tree will perform to maintain balance.
      hard
      A. Insert 12 and increase the node capacity temporarily
      B. Discard 12 because the node is full
      C. Insert 12 and rebalance by merging with sibling nodes without splitting
      D. Insert 12 into the leaf node, then split the node and promote the middle key to the parent

      Solution

      1. Step 1: Attempt to insert 12 into leaf node

        The leaf node has max 3 keys [5, 10, 15]. Inserting 12 adds a 4th key, exceeding capacity.
      2. Step 2: Split the node and promote middle key

        The node splits into two nodes, and the middle key (10) is promoted to the parent to maintain balance.
      3. Final Answer:

        Insert 12 into the leaf node, then split the node and promote the middle key to the parent -> Option D
      4. Quick Check:

        Insert, split, promote middle key = balanced B-tree [OK]
      Hint: Insert, then split and promote middle key if node is full [OK]
      Common Mistakes:
      • Discarding keys when node is full
      • Merging instead of splitting on insertion
      • Temporarily increasing node capacity