CS 261 FINAL
Perfect Full Binary Tree
1. All leaves are at the same depth and all internal nodes have two children 2. height of h will have 2^h leaves 3. height of h will have 2^(h+1)-1 nodes
Open Address Hashing (adding and linear probing)
1. All values are stored in an array 2. if hashed value is already filled use linear probing to find next empty spot 3. If probing reaches end of the array wrap around to the front of the array
Adding nodes to heap (process)
Add from left to right, or at the end of the array and percolate up maintaining the max or min heap property
Edges
An edge between two vertices indicates they have some sort of relationship/connected
Binary Tree Application
Animal game/20 Questions
Binary Search tree vs. Binary Tree
Binary search tree = ADT Binary Tree = Data Structure
Data structure used to implement a knowledge base as in the "guess the animal game"
Binary tree(not BST) order doesn't matter
HeapSort Time Complexity
Build heap = O(nlogn) HeapSort = O(nlogn) total: O(nlogn)
Binary search algorithm Psuedocode
Check middle index if its not value split array according to if value is bigger (right) or smaller (left)
Best data structure to represent Priority queue ADT
Heap b/c items are constantly being added or removed so overall time will be saved when using heap
AVL Trees, height difference, time complexity for operations
Height Balanced Trees 1. For each node, the height difference between the left and right subtrees is at most one 2. O(logn) for bag operations
Traversal that produces elements of an AVL tree in sorted order.
In-order
Tree Traversal that goes in parenthesis added order
In-order
Searching time for a reasonably full (well balanced) tree
O(logn)
Time complexity for Binary Search
O(logn) b/c splitting array in two everytime
Average time of add() for a SortedArray
O(n)
Tree traversal time complexity
O(n)
Worst-case search time for a binary search tree
O(n) potentially a linked list
edge list space complexity
O(v+e)
Adjacency Matrix space complexity
O(v^2)
How can search be sped up with arrays
Order them
Parent and children nodes
Parent nodes are nodes that point to other nodes, and those other nodes are children. Every node has at least one parent
Node's depth
Path length from the root to that node
Tree traversal that goes in polish notation
Pre-order
Three common tree traversals (Depth-first traversals)
Pre-order (Node, left children, right children) in-order (Left children, node, right children) post-order(Left children, right children, node)
Best table sizes
Prime Numbers
Type of Rotations and How to rotate depending on type of rotation
RR (Left rotation around root), RL (right rotation around child, then left rotation around root) LL(right rotation around the root) LR(Left rotation around child, then right rotation around root)
Removing nodes from heap (process)
Remove first node and return it at the end. put last filled node at the start and remove it from the end or decrement size. Then percolate down
descendants
consists of a node's children and their children and so on. All nodes in a tree are descendants of the root node (except the root node)
Subtree
consists of the root node (root of that subtree) and all of its descendants
Finding children at index i in heap
left child of a node at index i = 2i+1 right child of a node at index i = 2i+2
Node's height
max path length from that node to a leaf node. Height of a tree is the height of the root node
To find the index of a children's parent node
parent of a node at index i = (i-1)/2
Tree traversal that goes in reverse polish notation
post-Order
ADT used when implementing an in-order tree traversal iterator
stack
Inserting into an AVL tree with n nodes requires looking at O(log n) nodes
true, because AVL trees are balanced
Priority Queue and operations
- ADT -Associates a priority with each element -First element has the highest priority (lowest value for prio) -Ex: to do lists, graph searching -Operations: add(), getmin(), and removemin()
Breadth-First Search
- BFS like a wave flowing through a maze - May not find it as quickly, but will always find a solution -BFS is guaranteed to find a path containing the least steps from start to goal
Bucket/Chain
- Collections in table entries
Depth-First Search
- DFS is like a single person working a maze -May take a certain route and have to backtrack a long way -Can get lucky and find solution very quickly -in an infinite path maze can get stuck in the infinite path
Types of graphs
- Directed or undirected - weighted or unweighted
Hash Function Goals
- Fast(constant time) -Produce uniformly distributed indices -repeatable (same key always results in same index)
Chaining HashTable
- Maintain a collection at each table entry (Linked List)
Graphs
- Represent relationships or connections -Each node may have many predecessors -May have loops or cycles
Heap Description
-Data structure used to to implement priority queues -Complete binary tree -Usually represented as an array
AVL Tree Balance Factor
-Determine if double rotation is needed -Height(right subtree) - height (left subtree)
Rotation right pseudocode
-New top node is the current top node's left child -New top's right child is the current node -Current node's new left child is the new top node's right child
Rotation left Pseudocode
-New top node is the current top node's right child -New top's left child is the current node -Current node's new right child is the new top node's left child
Edge List
-Unweighted: Linked lists that stores only the edges -More space efficient for sparse graphs (few edges) -weighted: stores edges and weights
Removing from Open Address HashTable and benefits
-Use TOMBSTONE to mark that a node has been deleted -TOMBSTONE marks deleted entry, can be replaced when adding a new entry, doesn't halt search during contains or remove
Vertices
-nodes that represent something, each vertex is unique
Hash Tables Description about value and indices
1. Elements can be indexed by values other than integers using a hash function 2. Multiple values may share an index
Complete Binary tree
1. Full except for the bottom level which is filled from left to right 2. Longest path is ceiling(logn) for n nodes Ex: heap and priority queue implmentation
Minimally perfect hash functions
1. No collisions 2. Table size = # of elements
Perfect Hash function
1. No collisions 2. Table size >= # of elements
Two approaches to resolving collisions
1. Open address hashing: if a spot is full, probe for next empty spot 2. Chaining (or buckets); keep a collection at each table entry (LinkedList)
Requirements for a binary tree
1. Parent's have to have unique children, can't point to the same children 2. Can't have loops in tree
Requirements for performing a binary search (array)
1. Random access to elements 2. Elements are already in sorted order
Basic Mod operations
5 mod 6 = 5 8 mod 6 = 2 (remainder when dividing 8/6)
Shifting
<< 1 ->Multiply by 2 >> 1 ->divide by 2
Heap Time complexities for general heap operations
Add = logn getMin = 1 removeMin = logn
Binary Search (contains, insert) time complexity
Contains: O(logN) Insert: O(N) -> Need to adjust array
Casts
Convert a numeric type into an integer (turning a string into a number)
Full Binary Tree
Every node is either a leaf or has exactly 2 children
Min heap
Every node's value is less than or equal to the value of its children
Max heap
Every node's value is more than or equal to the value of its children
What can a BST produce in terms of ADT?
Faster bag implementations
Dijkstra's Algorithm
Finds the shortest/lowest cost path from a specified vertex in a graph to all other reachable vertices in the graph (weighted graph) -> priority queue -Cost-first search
Hash Table basic steps
Give a hash function a key and it turns it into an index value, and places the key value in the hash table
Does every key in a hash table need to be unique?
Yes, that is the entire point of storing key/value pairs.
Folding
Key partitioned into parts when are then combined using basic math operations
Load factor (Chaining)
Lambda = number of elements/size of the table -Represents the average number of elements in each bucket (can be more than 1) - If this reaches a certain MAX_LOAD_FACTOR(8) then resize
Load Factor (Open-address
Lambda = number of elements/size of the table -portion of the tables that is filled (between 0 and 1) -if this reaches a certain MAX_LOAD_FACTOR then we need to resize and rehash the table
Mapping
Map (a part of) the key into an integer
Does each key in the hash table need to hash to a unique value?
No, but we should use a hash function with as few collisions as possible
Binary Search Tree requirements
Node values are less than the parent in the left subtree and greater than the parent in the right subtree
Siblings
Nodes that have the same parent
Interior Nodes
Nodes with children
Leaf Nodes
Nodes with no children
Path's length
Number of arcs traversed
Average case to add for an unsorted dynArray
O(1)
Hash table bag operation time complexity
O(1) time for bag operations if uniformly distributed so no collisions occur
Average case to remove from an AVL tree
O(Logn)
Space complexity for BFS and DFS
O(V) -In practice BFS will take more space because it stores all the paths
Dijkstra's Algorithm Space Complexity
O(V+ElogE)
Root Node
Single node usually drawn at the top of the tree and has no parent
Singly Source Reachability
Stack -> Depth-First-Search
Issue with representing a heap when it is not complete or unbalanced
The dynarray will be filled with holes because several indices will have NULL values
Adjacency matrix (weighted and unweighted)
Unweighted: 2d array that stores a value of 1 if it has an edge with another vertex (x and y are both cities). Weighted: stores the weight instead of the number 1
Root or first element of a heap
Value with the highest priority indicated either with the highest priority value (max heap) or the lowest priority value (min heap)
Euler Tour
Walks around the tree's perimeter without crossing edges. 1st visit: left side of node (preorder) 2nd visit: bottom side of the node (inorder) 3rd visit: right side of node (postorder)
Worst case and Best case Open Address Hashing and chaining
Worst Case -> all values have the same position O(n) Best Case -> Uniformly distributes O(1)
Time complexity for Binary Search Tree (AVL Tree) bag operations (add, contains, remove)
add = logn contains = logn remove = logn
Time complexity for Ordered Array bag operations (add, contains, remove)
add = n contains = logn remove = n
What kind of data structure is binary search effective on
arrays or trees
Directed arcs
arrows pointing to children
The height of any binary tree with n nodes is always O(log n)
false, due to unbalanced trees that have height n
This tree is efficiently stored in an array.
heap
Does the hash table performance increase or decrease as the number of buckets increases?
increase, less collisions
To get an index value from a hashed value
int idx = hashfunction(data) % ht->capacity(size of table)