CS2110 Prelim #2 - BST, Heaps, Priority Queue, Maps, Sets

0.0(0)
Studied by 0 people
call kaiCall Kai
learnLearn
examPractice Test
spaced repetitionSpaced Repetition
heart puzzleMatch
flashcardsFlashcards
GameKnowt Play
Card Sorting

1/29

encourage image

There's no tags or description

Looks like no tags are added yet.

30 Terms

1
New cards

BST find()

Use compareTo()

2
New cards

BST add() - value isn’t there

follow comparisons and add normally

3
New cards

BST add() - value is there + right subtree is open

add same value at the right subtree after comparisons are followed

4
New cards

BST add() - value is there, but right subtree is occupied

  • Go right once and then as far left as possible (also called a sucessor)

  • Insert at empty left subtree


5
New cards

BST remove() general first step

Find the node you want to remove using find()

6
New cards

BST remove() ~ its right subtree is empty

Slide its left subtree up to replace it (supplanting)

**also works if the node is a leaf

7
New cards

BST remove() ~ its right subtree is non empty

  • Copy sucessor’s value into node you’re trying to remove

  • Slide og sucessor’s right subtree up


8
New cards

priority queue

thing with the highest priority gets removed first

9
New cards

heap

a data structure we can use to implement it

10
New cards

2 rules of max heaps

  • Shape: fill levels from top to bottom, left to right

  • Every parent must be >= each of its children

    • Siblings don’t need to be sorted


11
New cards

What is a heap stored in and how?

A heap is stored in an ArrayList level by level, left to right

12
New cards

Parent(i) =

(i-1)/2

13
New cards

leftChild(i) =

2 * i + 1

14
New cards

rightChild(i)

2 * i + 2

15
New cards

Maximum is always at what index?

0

16
New cards

Adding to the max heap [add()]

  1. Put the new value in the next open position

  2. Swap with its parent if its larger (called bubbleUp, repeat as many times as needed)


17
New cards

Removing from the max heap [remove()]

  1. Remove root and replace with the last element

  2. Compare the new root with its smaller child (repeat as many times as needed and swap if needed)

  3. Called bubbleDown()


18
New cards

Priority queue stores what…

item + priority

19
New cards

Heap sorted by what…

priority #

20
New cards

Heapsort

  1. heapify: rearrange array into a max heap

  2. repeatedly move maximum to the end of remaining heap


21
New cards

Basic rules for sets

Distinct elements, duplicates don’t count; sets also have no meaningful positions.

22
New cards

Union

Set with everything in either set (no repeated elements)

23
New cards

Restriction

Only keep items that pass a predicate

24
New cards

Intersection

keep only elements in BOTH sets

25
New cards

Maps

Use a key to look up a value

26
New cards

Keys MUST be what ___ and values can what ____

unique, repeat

27
New cards

What happens if you try to add a duplicate key

the existing key will be replaced

28
New cards

Searching examines each entry’s ___, not its value

key

29
New cards

Dynamic priority queues

Combine maps with heaps so you can change an element’s priority after adding it.

30
New cards

How do DPQs work

  1. use map to find its heap index

  2. change its priority

  3. if priority increased, bubble up

  4. if priority decreased, bubble down