Week 7 Flashcards
Max Heap
Binary tree data structure in which the value of any node is always greater than that of its children (heap property)
Largest value is the root of the tree
COMPLETE binary tree. Each level is filled before the next. filling from left to right.
Add/Remove : log n
Find largest: O(1)
Heap underlying implementation
Formulat to find children/parent
Heap with max height h will have an array size of 2^h -1 Root of three at index 0 Node at index k: - left child: index 2k +1 -right child: index 2k+2; - parent at index (k-1)/2
Adding value to heap, process
Place in the rightmost available spot of the deepest level of tree -> Bubble up
Removing value from heap
Place right most element at deepest level at root position (can only remove elements from root) -> bubble down
When bubbling down it always switches with the larger to the two children nodes
Is Heap a BST
NO, it is a BT but not a BST
Futhermore, children of a given node must be smaller than node but not necessary that smaller one is left and the other right
How do we know we are done with bubbling down
Heap has not null leaves. Hence we know we are one if the indices of the left and right child of a given node are outside max capacity of array
What is a Priority Queue
Java imlementation of Heap. But Min Heap and not Max Heap.
Why do we require that heap uses a complete BT
Increase efficiency, as we have a interest to minimize height of BT
What can be used in Priority Queue
Any Java object can be used as key as long as there exists means to compare any two instances in a way that defines a natural order of the keys
Trie - Main appplication
Information retrieval
Tree like data structure that is efficient at storing strings in order to support fast pattern matching, in which each node holds a single character, an the nodes that are children of a node represent characters that follow that character in string
Why Trie?
Storing strings in BST inefficient, every node start character by character comparison from beginning of string. would only need to check last added character
Is Trie a Binary Tree
No
Trie Definition
Tree-based data structure for storing strings in order to support fast pattern matching.
Trie Properties - Structure
- Each node, except the root is labeled with a character
- The children of an non-leaf node have distinct labels
- Each leaf is associated with a string. Concatenate characters along path
- Height = length of longest string
- # leaves = # strings stored
- total number of nodes at most = n+1, n = total length of all strings
- Use sentinel, e.g. null to mark end of string
Trie - Time complexity
o(m), whre m is the length of the string, assuming a nodes stores its child nodes in an o(1) data strucutre.
if children stored e.g. in LL. need to traverse all X children of that node. complexity X*m