1/32
Looks like no tags are added yet.
Abstract data type (ADT)
Public operation that determines how data structure behaves
Contract - agreement on data behavior
Implementation - how to actually code contract: different methods
ADT Property: Abstraction
Hide low-level details to focus on the essentials
ADT Property: Encapsulation
Protect internal state behind methods
ADT Property: Modularity
Top-down approach/step-wise refinement: develop and test components independently
ADT Property: Reusability
Use same contract in other applications
Linked list
Chain of nodes, not necessarily stored contiguously
Node - data + pointer to another node
Head - start
Tail - end, pointer to null
Sequential access: time complexity O(n)

Linked list types
Singly linked - unidirectional
Doubly linked - bidirectional
Circular - tail points back to head instead of null

Linked list insertion at head
Move head to be the node after the inserted node
Set inserted node to be the head

Linked list insertion in middle
Node that is currently at position that node needs to be inserted into will be referred to as current node
Set the node after the current node to be the node after the inserted node (new.next = current.next)
Set the node after the current node to be the inserted node (current.next = new)

Linked list insertion at end
Set the node after the inserted node to be null (new.next = null)
If the head is null, set the inserted node to be head
Else set node after current tail to be inserted node (tail.next = new)
Linked list deleting node
If deleting head: set node after head to be new head (head.next = head)
Else: Let the node after the deleted node to be after the node before the deleted node (previous.next = deleted.next)
Old node becomes unlinked, to be reclaimed by garbage collector
Linked list evaluation
+Dynamic size
+O(1) insertion time complexity
+Insertion doesn’t require shifting of later elements
-O(n) access time complexity
-Cannot jump directly to index in O(1)
-Extra memory required for pointers/link field
-Difficult to identify specific location in memory (poor locality)
Binary search tree (BST)
Tree with values so that left subtree values are smaller and right are greater
Root: first node
Parent: node with subnodes
Child: submode
Edge: connection between parent and child
Subtree: smaller tree
Leaf: node with no child
Height/depth: longest edge from root to last leaf

BST build from input sequence
Start at root
If inserted node is smaller, go left. Else, go right.
Continue until there’s no more subnodes
Unbalanced if insert in ascending order: best to start with median

Inorder traversal
Left, main, right: visit nodes in ascending order
Can be used for infix notation

Preorder traversal
Main, left, right
Can be used for prefix notation (operators before operands)

Postorder traversal
Left, right, main
Can be used for postfix notation (operator after operands)

BST min and max
Go left/right respectively until no more subnodes
BST Insertion
If BST is empty: insert at root
Else: follow root until finding parent node that:
If larger than inserted: insert as right child
If smaller than inserted: insert as left child
BST duplicate values
Either ban or modify selection (time consuming, have to traverse entire tree if searching for value)
BST deletion
If deleted has no child: just delete
If it has 1 child: connect deleted’s parent directly to child
If it has 2 children: replace deleted node with largest value in left subtree OR smallest value in right subtree

Set
All elements must be unique, order doesn’t matter
Subset - all elements are in a parent set
Superset - parent set
Implemented using hash tables

Comparing sets
Union (U): all in either
Intersection: only elements in common
Subtraction: what don’t have in common

Hash table
Set that stores and retrieves data using a hash function
Stores in key-value pairs
Uses underlying array to organize entries
Key - identifier of record
Index - numerical position in underlying array
Bucket - storage location in hash table that hold 1/+ entries
Value - info associated with key
Well designed = average time complexity O(1) for searching, inserting, deleting
Hash table hash function
Convert key into array index to find which bucket in underlying array to store pair
Assign key-value pair to bucket of number (key % size) (search for a pair in same way)
Allows keys to be calculated instead of having to iterate sequentially

Hash table Collision
Multiple different pairs have same calculated bucket
Collision solution: Chaining
Have each bucket point to chain (linked list of entries)

Collision solution: Open addressing
Search for another bucket within table
Open addressing: Linear probing
Check directly next available bucket
may cause primary cluster (all values clustered in 1 area): ^ clustering = ^ likelihood of further collisions
Open addressing: Quadratic probing
Increasingly spaced offsets based on square increments
Reduces some clustering
Open addressing: Double hashing
Use second hash function to find bucket
^ complexity, better distribution
Hash table Load factor
Calculated threshold: how full table is (full buckets / size)
Hash table Rehashing
When reach threshold/resizing: allocation larger table and calculate new buckets for existing keys