Hansung Flashcards
What is a heap?
A binary tree with two constraints
- It is complete
- Each child has a value “greater than or equal to” it sparent
How do you add to a heap?
- add the element to the next available space in the tree
- Percolate the value up the tree to maintain the correct ordering
What data structures use heaps?
Priority Queues
How does removeMin work on priority queues?
- Pop the root
- Replace it with the last element in the heap
- Percolate this element down to the bottom of the heap choosing the minimum child
How do we navigate a heap?
- The root of the tree is at array location 0
- The last element is size()-1
- The parent of a node k is at floor((k-1)/2)
- The children of node k are at array locations 2k + 1 and 2k + 2
What is the time complexity of perlocating in a heap?
Ɵ(1)
What is the time complexity of the height of the tree?
Ɵ(log(n))
What is the time complexity of add/removeMin on a heap?
Ɵ(log(n)) in worst case
Ɵ(1) normally
What is the worst case time complexity of Heap sort?
O(n log(n))
What is the space complexity of heap sort?
O(1)
What is a B-tree?
Balanced multiway trees made for fast search, finding successors/predecessors, insert, delete, maximum, minimum
What are M-way trees?
Trees where each non-leaf node has M children
What is the access time of a M-way tree?
Logm(n) = Log2(n)/Log2(m)
What makes B+ trees different to B-Trees
- Only leaf nodes contain data pointers, while internal nodes contain keys only
- Sequential access is possible like a linked list
- Searching is faster
- Insertion and Deletion are easier and faster
What makes B-Trees different to B+ Trees?
- All internal nodes and leaf nodes contain data pointers along with keys
- Sequential access of nodes is not possible
- Searching is slower
- Insertion and Deletion take more time
What values should you use for M in B+ trees?
Odd numbers
What is a Trie?
A multiway tree often used for storing large sets of words
How do tries work?
- They are trees with a possible branch for every letter of an alphabet
- All words end with a special letter “$”
- They compactify the edges in the tree in order to remove: internal and external one-way branching
What are the uses of Tries?
- Way of implementing sets
- Provide quick insertion, deletion and find
- Good for spell checkers, completion, algorithm
What are the disadvantages of Tries?
- Waste large amounts of memory
How are Binary Tries different from Normal Tries?
They speeds up finds but loses the advantage of multiway trees by reducing the depth
Why use Tries?
- Classic memory, time complexity trade-off
- Specialist
- Not too complicated to implement