Binary Heap Data Structure

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

Which of the following statements accurately describes the 'shape property' of a binary heap?

  • It is a complete binary tree where all levels are fully filled except possibly the last, which is filled from right to left.
  • It is any binary tree, regardless of completeness, as long as it maintains the heap property.
  • It is a binary search tree where nodes are arranged based on their values.
  • It is a complete binary tree where all levels are fully filled except possibly the last, which is filled from left to right. (correct)

In a min-heap, which of the following must be true for any given node?

  • The node's value is less than or equal to the value of its parent.
  • The node's value is equal to the average of its children's values.
  • The node's value is less than or equal to the value of its children. (correct)
  • The node's value is greater than or equal to the value of its children.

Which of the following is NOT a typical application of heaps?

  • Balancing search trees. (correct)
  • Sorting data efficiently (Heapsort).
  • Finding the Kth largest element in a dataset.
  • Implementing priority queues.

During insertion into a binary heap, which step ensures the heap property is maintained after a new node is added?

<p>Bubble Up (Heapify Up). (A)</p>
Signup and view all the answers

What is the time complexity of finding the insertion point in a binary heap using level-order traversal?

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

In a binary heap, which element can be directly removed?

<p>Only the root element (min or max). (B)</p>
Signup and view all the answers

After swapping the root with the last node during deletion in a min-heap, what process is used to restore the heap property?

<p>Bubble Down (Heapify Down). (B)</p>
Signup and view all the answers

During the 'Bubble Down' operation in a min-heap, a node is swapped with which of its children?

<p>The smaller child. (C)</p>
Signup and view all the answers

What is a significant advantage of using an array to represent a binary heap compared to a tree with nodes and pointers?

<p>Arrays reduce memory usage due to no need to manage node objects and pointers. (B)</p>
Signup and view all the answers

If a node is at index i in an array representing a binary heap, which of the following is the index of its left child?

<p>2 * i + 1 (A)</p>
Signup and view all the answers

Which of the following is a critical requirement for a good hash function?

<p>It should distribute keys evenly across the table. (D)</p>
Signup and view all the answers

What is the term for the situation when two different keys hash to the same index in a hash table?

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

In the context of hash tables, what is 'chaining'?

<p>A collision resolution strategy where each bucket stores a linked list of entries. (C)</p>
Signup and view all the answers

Which of the following describes 'open addressing' in the context of hash tables?

<p>Finding the next available slot in the table itself when a collision occurs. (A)</p>
Signup and view all the answers

In linear probing, how is the next potential index calculated after a collision?

<p>(i + k) % tableSize, where k is the number of collisions. (B)</p>
Signup and view all the answers

What is the primary disadvantage of linear probing in hash tables?

<p>Primary clustering. (A)</p>
Signup and view all the answers

What is the purpose of a 'tombstone' in the context of hash table operations?

<p>To indicate a slot that was occupied but is now deleted. (B)</p>
Signup and view all the answers

What is 'rehashing' in the context of hash tables?

<p>Creating a new, larger table and re-inserting all elements. (C)</p>
Signup and view all the answers

What is one reason to use a prime number for the table size when designing a hash table?

<p>To ensure even distribution and minimize clustering. (C)</p>
Signup and view all the answers

Which collision resolution strategy typically requires the most extra memory?

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

Flashcards

What is a Binary Heap?

Binary tree with shape and heap properties.

Complete Binary Tree

Every level is filled except possibly the last, filled left to right.

Min Heap Property

Each node is less than or equal to its children.

Max Heap Property

Each node is greater than or equal to its children.

Signup and view all the flashcards

Insertion in a Binary Heap

Adding an element while maintaining tree structure and heap property.

Signup and view all the flashcards

Insertion Point

Find the insertion point (left to right on the last level).

Signup and view all the flashcards

Bubble Up (Heapify Up)

Move the inserted node up the tree to restore heap property.

Signup and view all the flashcards

Deletion in a Binary Heap

Removing element while maintaining tree structure and heap property.

Signup and view all the flashcards

Heap Deletion Steps

Swap root with last node, remove last node, bubble down.

Signup and view all the flashcards

Bubble Down (Heapify Down)

Restore heap property by moving node down the tree.

Signup and view all the flashcards

Heaps as Arrays

Using an array instead of tree w/ nodes and pointers for heaps.

Signup and view all the flashcards

getLeftIdx(int i)

Gives you the left child of the parent i

Signup and view all the flashcards

getRightIdx(int i)

Gives you the right child of the parent i

Signup and view all the flashcards

getParentIdx(int i)

Gives you the parent of the child i

Signup and view all the flashcards

getMin()

Returns the minimum element of the min heap

Signup and view all the flashcards

deleteMin()

Deletes the minimum element of the min heap

Signup and view all the flashcards

insert(T k)

Inserts a given element at the appropriate index

Signup and view all the flashcards

What is Hashing?

Mapping data to a fixed-size value (hash code).

Signup and view all the flashcards

Hash Function

Takes a key and returns an index in the hash table.

Signup and view all the flashcards

Collisions

Two different keys hash to the same index.

Signup and view all the flashcards

Study Notes

Binary Heap

  • Binary Heap is a binary tree with two main properties.

Shape Property

  • Must be a complete binary tree
  • Complete binary trees have all levels fully filled except possibly the last level, which is filled from left to right.

Heap Property

  • In a Min Heap, every node is less than or equal to its children.
  • In a Max Heap, every node is greater than or equal to its children.

Applications of Heaps

  • Can implement priority queues
  • Selecting the Kth element
  • Efficient sorting (used in heapsort)

Insertion in a Binary Heap

  • An element is inserted into the heap while maintaining the complete tree structure and heap property.

Insertion Steps

  • Find the insertion point, going from left to right on the last level
  • Traverse the tree level-order until the first node with an available child spot, this is O(n)
  • Insert the new node as the left or right child, depending on what's available
  • Bubble Up (Heapify Up) while the inserted nodes value is less than its parent like in a Min Heap
    • Swap the two nodes
    • Move up to the parent and repeat

Deletion in a Binary Heap

  • An element is removed from the heap while maintaining the complete tree structure and heap property.
  • Only the minimum in min heap or maximum in max heap element can be removed.

Deletion Steps

  • Swap root with the last node in the tree, this is the rightmost node on the last level
  • Remove the last node, formerly the root
  • Bubble Down (Heapify Down), starting at the new root
    • If the node is greater than either of its children
    • Swap it with the smaller child like in a Min Heap
    • Repeat until it's smaller than both or is a leaf

Heaps as Arrays

  • Binary heaps can be implemented more easily using an array instead of a tree with nodes and pointers.
  • Binary heaps are complete binary trees, which means they have a predictable, compact structure that fits perfectly into a flat array
  • Tree representations require level-order traversal to find where to insert, which is already an O(n) operation
  • Managing node objects and pointers is required with trees.
  • Heapifying requires complex swapping logic with parent/child nodes in trees.
  • If a node is at index i:
    • The left child is at 2i + 1
    • The right child is at 2i + 2
    • The parent is at floor((i-1)/2)

Methods

  • int getLeftIdx(int i) gives the left child of the parent i
  • int getRightIdx(int i) gives the right child of the parent i
  • int getParentIdx(int i) gives the parent of the child i
  • T getMin() returns the minimum element of the min heap
  • void deleteMin() deletes the minimum element of the min heap
  • void insert(T k) inserts a given element k at the appropriate index

Hashing

  • Hashing is a technique used to map data to a fixed-size value, called a hash code or hash value and is used for fast data access, insertion, and deletion.
  • Hashing is the backbone of hash tables, which allow for average-case constant time (O(1)) operations.

Hash Functions

  • A hash function takes a key and returns an index in the hash table.

Requirements of a Good Hash Function

  • Deterministic: Same input always gives the same output.
  • Efficient: Fast to compute.
  • Uniform: Distributes keys evenly across the table.
  • Minimizes collisions: Two different keys should ideally not hash to the same index.

Collisions

  • A collision occurs when two different keys hash to the same index.

Collision Resolution Strategies

  • Chaining (Separate Chaining)
    • Each bucket stores a linked list, easy to implement and table size doesn't limit the number of elements
    • Potentially slower if many items hash to the same index.
  • Open Addressing
    • Finds the next available slot in the table itself.

Common Open Addressing Strategies

  • Linear Probing: Check the next index = ((i + k) % tableSize) continuously until a free spot is found, where i is the index the key was originally hashed to and k is the number of previous collisions for that key
  • Quadratic Probing: Check index = (i + c1 * k + c2 * k²) % tableSize continuously until a free spot is found, where i is the original index from the hash function, k is the number of previous collisions for that key, and c1 and c2 are constants
  • Double Hashing: Use a second hash function to decide the step size using index = (i + k * h2(key)) % tableSize. where h2 is the second hash function and k the number of collisions thus far

Load Factor

Load Factor (a) = number of elements / table size

  • Helps measure how full the table is.
  • As a approaches 1, collision chances increase.
  • Rehashing is the process of creating a new table, typically with the nearest prime number to double the original table size and then re-inserting all elements using the new table and hash function in order to keep operations efficient as the data grows.

Picking a Table Size

  • Using key % size when the size is not prime can lead to clustering.
  • Clustering is when keys hash to nearby locations to form large blocks, causing slower operations.
  • With quadratic probing, non-prime sizes can cause repeated or skipped indices, leaving some slots unreachable.
  • Table sizes with prime numbers minimize these modular arithmetic patterns, ensuring a more uniform spread of keys.

Picking a Collision Resolution Strategy

  • Chaining (Separate Chaining)
    • Each bucket holds a list of entries, easy to implement, handles high load factors, but requires extra memory
  • Open Addressing: All entries are stored in the table itself.
    • Linear Probing: probes next slot: (i + k) % tableSize which is simple but suffers from primary clustering, degrading performance as the table fills.
  • Quadratic Probing: Probes using: (i + c1 * k + c2 * k^2) % tableSize which reduces primary clustering and offers a better spread, but suffers from secondary clustering and is harder to implement deletions.
  • Deleting an item in open addressing can break the probe sequence, leading to incorrect search results.
  • A deleted slots must be marked with a tombstone placeholder to fix this and indicate that the slot was occupied but is now deleted, needing probing to continue when looking for different keys.
  • Tombstones preserve the integrity of the probe sequence but add complexity, as the table must now distinguish between: empty (slot never used), tombstone (deleted slot), occupied (active key)
  • Double Hashing: Probes using a second hash function: (i + k * h2(key)) % tableSize, minimizing clustering and offering unique probe sequences, requiring two good hash functions.

Collision Resolution Strategy Comparison

  • Chaining: High Extra Space, Low Clustering Risk, Unlimited Load Factor Limit, Easy Deletion Handling
  • Linear Probing: None Extra Space, High Clustering Risk, ~0.7 max Load Factor Limit, Tricky (tombstones) Deletion Handling
  • Quadratic Probing: None Extra Space, Medium Clustering Risk, ~0.5-0.7 Load Factor Limit, Tricky (tombstones) Deletion Handling
  • Double Hashing: None Extra Space, Low Clustering Risk, ~0.7 Load Factor Limit, Tricky (tombstones) Deletion Handling

Runtime of Hash Map Operations

In ideal scenarios, hash tables provide constant-time operations.

  • Search: O(1) Average/Best Case, O(n) Worst Case
  • Insert: O(1) Average/Best Case, O(n) Worst Case
  • Delete: O(1) Average/Best Case, O(n) Worst Case

Applications of Hashing

  • Dictionaries/Maps
  • Database indexing
  • Caching
  • Symbol tables in compilers
  • Password storage

Studying That Suits You

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

Quiz Team

Related Documents

More Like This

Heap Sort Algorithm
10 questions
Data Structures: Binary Heap
10 questions

Data Structures: Binary Heap

IncredibleGalaxy1978 avatar
IncredibleGalaxy1978
Heaps Data Structure
19 questions

Heaps Data Structure

BraveSimile9417 avatar
BraveSimile9417
Binary Heap Data Structure
19 questions

Binary Heap Data Structure

ReverentIridium156 avatar
ReverentIridium156
Use Quizgecko on...
Browser
Browser