0% found this document useful (0 votes)
2 views

Midterm Study Guide

The document outlines various concepts in data structures and algorithms, including Big O, Big Omega, and Big Theta notations for analyzing time complexity. It covers different data structures such as arrays, linked lists, stacks, queues, trees, binary search trees, AVL trees, heaps, and hash tables, detailing their operations and complexities. Additionally, it explains tree traversal methods and the properties of heaps and priority queues.

Uploaded by

Albert Zweistein
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

Midterm Study Guide

The document outlines various concepts in data structures and algorithms, including Big O, Big Omega, and Big Theta notations for analyzing time complexity. It covers different data structures such as arrays, linked lists, stacks, queues, trees, binary search trees, AVL trees, heaps, and hash tables, detailing their operations and complexities. Additionally, it explains tree traversal methods and the properties of heaps and priority queues.

Uploaded by

Albert Zweistein
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

Big O : the worst case scenario in terms of number of operations.

Big Omega: The lower bound for the worst case scenario.
Big Theta: The exact complexity or a useful range of values.

Might need to review the first few lectures; how to find big omega, theta, from a given T(N) function.

Rules for complexity:


1)​ T(N) = T1(N) +T2(N) Where T1(N) = g(N), T2(N) = t(N)​
T(N) = O(max(g(N), t(N))
2)​ If T(N) is a polynomial of degree k, then T(N)= O(N^K)
3)​ log(k) N = O(N) for any constant K​
T(N) = T1(N) *T2(N)​
T(N) = O( f(N) * g(N))​
T(N) = O(f(N^2))

Online Algorithms: algos that require only constant space and run in linear time.

O(log(N)) : are algos that take constant time to cut the problem size by a fraction.

Abstract Data Type: set of objects together with a set of operations.


EX: lists, sets, and graphs.

ADT operations: add, remove, contains, union, find.

List: A linear sequence of arbitrary number of items, of the form A0, A1, A2,... An-1.
●​ For every element there is only one next element.
●​ Every element holds its own position.
●​ For any list(but the empty list which has size 0)
●​ Ai follows Ai-1 where (i<N)
●​ Ai-1 precedes Ai (i >0)
●​ The end and start of a list is NULL
●​ Implemented with Array

Array operations:
PrintList → O(N)
Find → O(N)
Findkth → O(1)
Insert and Remove → O(N)
●​ ALL important operations are linear(this is bad)
Linked List: consists of nodes, which are not necessarily adjacent in memory.
●​ Each node consists of the element(data) and a link to a node containing its successor.
●​ The last link has a reference null.
PrintList is O(N)
FindKth O(N) (Requires traversing the entire list)
Remove O(1) : Only needs to change the pointer
Insert O(1) : Only needs to change the pointer

Double Linked List: has two pointers per node, one to the next node and one to the previous node.
●​ Solves the problem of removing the first element.

Note : for (int i=0; i< N; i++)​ ​ Where 1st is an ArrayList


​ ​ 1st.add(0, i);
Is O(N^2) because add(0,i), adding to the beginning of an arrayList is an O(N) operation.

For a LinkedList add is O(1) so the above operation is O(N)

For a get method it is the opposite; LinkedList get O(N), ArrayList get is O(1)

Stack: A LIFO (last in first out) data ADT, is a type of list.


●​ Adds elements sequentially “stacked” on top of each other.

InFix: regular math operations read as a human would


PostFix: computery way of writing math operations. Ex: abc*+de*f+g*t
●​ Used in stacks to increase the efficiency arithmetic operations by eliminating the need for
parenthesis
●​ The parenthesis are implied by the ordering of elements.
From infix to postfix : a+b*c+(d*e+f)*g → abc*+de*f+g*t

How to read PostFix:


1)​ When an operand is read; place to output
2)​ Operators and parens are not immediate output; they are pushed to the stack.

Queues: FIFO(first in first out) ADT.


2 ←|2 | 3 | 4| ← 4
Can be implemented with Arrays, or Linked lists.

With Arrays:
●​ Hold the position front and back, which represent the front and ends of the queue respectively.
●​ the number of current elements entered into the queue (current size).
●​ Circular array implementation using MOD is used to prevent index out of bound errors.

Trees: collection of N nodes, one of which is the root, and N-1 edges. They are a one to many
relationship, each node can have multiple roots.
Leaf : nodes with no children.
Siblings: nodes with the same parent.
Path: from node n1 to nk, the sequence of nodes, N1,N2,..Nk, such that Ni is a parent of Ni+1 for 1<= i
<k
●​ There are unique paths from root to leaf.
Depth: The depth of a node is the number of edges from the node to the tree's root node.
A root node will have a depth of 0.
Height: The height of a node is the number of edges on the longest path from the node to a leaf.
A leaf node will have a height of 0.

How to:
●​ Keep the children of each node in a linked list of tree nodes.
●​ Note: Trees lack random access.

Methods of tree Traversal:


Pre-order : traverse from the root, to the leaf. Tracing a path from every root to every leaf. Starting left
to right.
Post-order : traverse from the children, to the root. Travel from root to leftmost leaf.
●​ Both methods must visit all the nodes.

Pre-order : → ( Root → Most left subtree →…→ most right subtree)


Post-order → ( Most left subtree →… → Most right subtree, root)
●​ Both are O(N) complexity.

Generic trees have no restrictions on the number of children per node.

Binary Trees: must have at most two children per node.


●​ Max number of leaves : 2^n
●​ Max number of nodes : 2^(n+1) - 1
●​ Min number of leaves: 1, Min number of nodes: n+1

Inorder Binary tree traversal → (left→ root → right)

For trees find(x) is O(| V |)

Binary Search Tree: For every node holding element X:


I.​ The values of all items in its left subtree are smaller than the element X.
II.​ The values of all items in its right subtree are larger than the element in X.

Insert works by using comparisons until a leaf is reached.


Deletion:
I.​ If the node is a leaf it can be deleted immediately.
II.​ If the node is a child, the node can be deleted after its parent adjusts a link to bypass the node.
III.​ If the node has two children.​
*Replace the data of the removed node with the smallest data of the right subtree,then
recursively delete the target node.

BST complexity:
Find O(N)
Insert O(N) ​
Remove O(N)

Worst case is a Linear Tree.


Inserting N elements is N^2 (Sorted elements inserted sequentially.)

●​ Adding elements via median first, then left media, right media, etc. Creates a balanced tree.
●​ For a balanced tree, Insert, Del, Find are O(log N) in the average case.

AVL tree: a binary search tree which has been balanced.


def: For every node in the tree, the height of the left and right subtree can differ at most by one.
Note: a ^ shaped tree is not balanced.

AVL tree with N nodes has O(logN) height.

AVL tree complexity:


Find : O(logN)
Insert: O(logN)
Delete O(logN) (Lazy delete)

Lazy Delete: each node contains a boolean field, indicating if the node is deleted or not. The nodes are
still part of the tree, they just get ignored.

●​ The balancing condition must be checked every time an element is added.

AVL tree Insert:


I.​ Move child of unbalanced node into the parent’s position
II.​ Parent becomes the “other” child(This is always ok in a BST)
III.​ Other subtree move only in ways BST allows
IV.​ There are many algo to handle the rotations required to balance the tree, all these rotations are
O(1) complexity.
Review rotation algos.
Heaps(Priority Queues): a specialized tree-based data structure which is essentially an
almost complete tree that satisfies the heap property: in a max heap, for any given node
C, if P is a parent node of C, then the key (the value) of P is greater than or equal to the key of
C.

Heaps allow:
BIG NOTE: Heaps do not require binary tree sorting, sorting is done by level, each level has to
be greater(or less than) than the lower one.

Insert(add)
DeleteMin: finds, returns, and removes the minimum element in the priority queue.
●​ There is no find operation
●​ Each element has a “priority”
●​ the min/max element in each respective type of heap has the priority. (min for min
queues, max for max queues.)
Ex. a Heap with {7,5,6}
​ deletemin →deletes 5.
​ deletemin → deletes 6.

Priority Queue Linked-List implementation:


●​ Insertion at the front (O1)
●​ DeleteMin O(N)
With sorted Linked List:
●​ Insertion → O(N)
●​ DeleteMin →O(1)
With Binary Search Tree:
●​ Insertion → O(logN) AVG
●​ DeleteMin → O(logN) AVG

Binary min-heap : a binary tree that is completely filled. (With the possible exception of the
bottom level.
●​ The priority of every(non-root) node is less important than the priority of its parent.
●​ The key of a node <= the keys of its children
●​ Note: A heap is NOT a binary search tree.

Heaps can be represented as an array with no links.


●​ The first element of the array must always be empty.
●​ The ith element is the root. Each child is 2i or 2i+1 element.
●​ {null, A, B, C} Array → A(i=1) is the parent, B(i=1*2 )and C(i=2*1 +1) are the children.
This array represents a Height 1 heap.
Operations of heaps:
1.​ Find min: return root data
2.​ DeleteMin: answer = root data
●​ Move rightmost leaf in the last row to the root to restore structure.
●​ If necessary, percolate down to restore heap-order property.
●​ To percolate down: Keep comparing priority of element with its children
●​ If priority is lower, swap with the largest priority child and go down to the next level.
●​ End if both children have a lower priority than their parent, or we’ve reached a leaf
node.

Insert:
I.​ Put the new node in the next position on the bottom row.
II.​ Percolate up to restore heap-order property.
●​ On average it has been down that heaps use 2.607 comparisons

Heaps complexity:
Delete → O(logN)
Insert → O(logN) but O(1)

Hash Tables:

Size should be:


Prime number for Linear probing
2N for quadratic probing
N for separate chaining.

You might also like