Study Notes

Data Structures and Algorithms First Exam - Cheatsheet

Caden Jones
0 imports

Free ยท 2 imports included

Study Notes Preview

4 sections locked
Section 1

Data Structures and Algorithms First Exam - Cheatsheet

STUDY GUIDE

๐ŸŽ“ Data Structures and Algorithms First Exam - Study Guide

๐Ÿ“‹ Course Structure

code
๐Ÿ“š Data Structures and Algorithms โ”œโ”€โ”€ ๐Ÿ“– Chapter 1: Fundamental Data Structures โ”‚ โ”œโ”€โ”€ ๐Ÿ”น Linked Lists โ”‚ โ”œโ”€โ”€ ๐Ÿ”น Arrays โ”‚ โ””โ”€โ”€ ๐Ÿ”น Comparison of Linked Lists and Arrays โ”œโ”€โ”€ ๐Ÿ“– Chapter 2: Abstract Data Types (ADTs) โ”‚ โ”œโ”€โ”€ ๐Ÿ”น Stacks โ”‚ โ”œโ”€โ”€ ๐Ÿ”น Queues โ”‚ โ””โ”€โ”€ ๐Ÿ”น Iterators and Iterable ADT โ”œโ”€โ”€ ๐Ÿ“– Chapter 3: Lists and Trees โ”‚ โ”œโ”€โ”€ ๐Ÿ”น Index Lists โ”‚ โ”œโ”€โ”€ ๐Ÿ”น Positional Lists โ”‚ โ””โ”€โ”€ ๐Ÿ”น Trees โ”œโ”€โ”€ ๐Ÿ“– Chapter 4: Asymptotic Analysis โ”‚ โ”œโ”€โ”€ ๐Ÿ”น Counting Primitive Operations โ”‚ โ”œโ”€โ”€ ๐Ÿ”น Big O Notation and Growth Rates โ”‚ โ””โ”€โ”€ ๐Ÿ”น Running Time Analysis with Control Statements โ””โ”€โ”€ ๐Ÿ“– Chapter 5: Formulas โ”œโ”€โ”€ ๐Ÿ”น Summation Formula โ”œโ”€โ”€ ๐Ÿ”น Geometric Series Formula โ””โ”€โ”€ ๐Ÿ”น Logarithmic Summation Formula
Section 2

๐Ÿ“– Chapter 1: Fundamental Data Structures

What this chapter covers: This chapter explores the fundamental data structures: linked lists and arrays. It details their properties, advantages, and disadvantages concerning insertion, access, and memory usage. Understanding these structures is essential for constructing more complex data structures and algorithms.

๐Ÿ”‘ Essential Concepts & Formulas

Concept/FormulaDefinition/EquationWhen to UseQuick Check
Linked List InsertionNodes with value and pointer to next node.Frequent insertions/deletions in the middle.Traverse to check node order.
Array AccessContiguous memory blocks.Efficient random access.Check index boundaries.
Array InsertionRequires shifting elements.When insertions are rare.Verify element positions after insertion.
Linked List Memory UsageExtra memory for pointers.When memory usage is flexible.Check for memory leaks.

๐Ÿ› ๏ธ Problem Types

Type A: Insertion in a Singly Linked List

Setup: "When you need to insert a new node at a specific position in a singly linked list."

Method: "Traverse the list to the node before the insertion point, update the next pointer of the new node to point to the next node in the list, and update the next pointer of the previous node to point to the new node."

Final Answer
Example: "Given a linked list 1 -> 2 -> 3, insert 4 after 2. Result: 1 -> 2 -> 4 -> 3."

Type B: Deletion in an Array

Setup: "When you need to delete an element from a specific position in an array."

Method: "Shift all elements after the deletion point one position to the left to fill the gap. Update the size of the array."

Final Answer
Example: "Given an array [1, 2, 3, 4, 5], delete the element at index 2 (value 3). Result: [1, 2, 4, 5]."

๐Ÿงฎ Solved Example

Problem: Implement a function to insert a node with value x at the beginning of a singly linked list.

Given: A singly linked list with head node head.

Steps:

  1. Create a new node with value x.
  2. Set the next pointer of the new node to point to the current head.
  3. Update head to point to the new node.
"
โœ…
Answer: ```python def insert_at_beginning(head, x): new_node = Node(x) new_node.next = head head = new_node return head
code
### โš ๏ธ **Common Mistakes** **โŒ Mistake 1:** Forgetting to update the `head` pointer when inserting at the beginning of a linked list. **โœ… How to avoid:** Always ensure that the `head` pointer is updated to point to the new first node. **โŒ Mistake 2:** Incorrectly shifting elements when deleting from an array, leading to gaps or out-of-bounds errors. **โœ… How to avoid:** Carefully manage the loop indices and array size during deletion. ### ๐Ÿ’ก **Study Tip** Draw diagrams of linked lists and arrays to visualize insertion and deletion operations. This helps in understanding pointer manipulation and array indexing. ---

๐Ÿ“– Chapter 2: Abstract Data Types (ADTs)

What this chapter covers: This chapter introduces fundamental Abstract Data Types (ADTs) such as Stacks and Queues. It explains their principles, main methods, and implementation costs using both array-based and linked list-based approaches. Understanding ADTs is essential for designing efficient algorithms.

๐Ÿ”‘ Essential Concepts & Formulas

Concept/FormulaDefinition/EquationWhen to UseQuick Check
Stack (LIFO)Last-In-First-OutExpression evaluation, backtracking.Verify top element after push/pop.
Queue (FIFO)First-In-First-OutTask scheduling, breadth-first search.Verify front element after enqueue/dequeue.
IteratorObject for traversing a collection.Looping through data structures.Check hasNext() and next() methods.
Stack Push (Array)Add element to the top: stack[top++] = element;Adding elements to a stack.Ensure top index is within bounds.
Queue Enqueue (Circular Array)Add element to rear: queue[rear] = element; rear = (rear + 1) % capacity;Adding elements to a queue.Check for queue overflow.

๐Ÿ› ๏ธ Problem Types

Type A: Implementing a Stack with a Linked List

Setup: "When you need to implement a stack using a linked list data structure."

Method: "Use the head of the linked list as the top of the stack. The push operation adds a new node at the head, and the pop operation removes the node from the head. Both operations should take O(1) time."

Example: "Implement a stack with push(1), push(2), pop(), push(3), pop(). The stack should output 2, 3."

Type B: Implementing a Queue with a Circular Array

Setup: "When you need to implement a queue using a circular array to avoid shifting elements."

Method: "Use two pointers, front and rear, to track the front and rear of the queue. When enqueuing, add the element at rear and increment rear modulo the array size. When dequeuing, remove the element at front and increment front modulo the array size."

Example: "Implement a queue with enqueue(1), enqueue(2), dequeue(), enqueue(3), dequeue(). The queue should output 1, 2."

๐Ÿงฎ Solved Example

Problem: Implement a stack using an array.

Given: An array stack and a top index.

Steps:

  1. Initialize top to -1.
  2. push(x): Increment top and set stack[top] = x.
  3. pop(): Return stack[top] and decrement top.
"
โœ…
Answer: ```python class ArrayStack: def init(self, capacity): self.stack = [None] * capacity self.top = -1 self.capacity = capacity
code
def push(self, x): if self.top == self.capacity - 1: raise Exception("Stack Overflow") self.top += 1 self.stack[self.top] = x def pop(self): if self.top == -1: raise Exception("Stack Underflow") item = self.stack[self.top] self.top -= 1 return item
code
### โš ๏ธ **Common Mistakes** **โŒ Mistake 1:** Stack overflow when pushing onto a full array-based stack. **โœ… How to avoid:** Check if the stack is full before pushing. **โŒ Mistake 2:** Queue underflow when dequeuing from an empty queue. **โœ… How to avoid:** Check if the queue is empty before dequeuing. ### ๐Ÿ’ก **Study Tip** Visualize the behavior of stacks and queues with diagrams. Practice implementing these ADTs using both arrays and linked lists to understand their trade-offs. --- ## ๐Ÿ“– Chapter 3: Lists and Trees **What this chapter covers:** This chapter covers Index Lists, Positional Lists, and Trees. It explains how elements are accessed and manipulated in these structures. Understanding these concepts is crucial for building more complex data structures and algorithms. ### ๐Ÿ”‘ Essential Concepts & Formulas | **Concept/Formula** | **Definition/Equation** | **When to Use** | **Quick Check** | |---------------------|-------------------------|-----------------|-----------------| | **Index List** | Access by index. | When element order matters and index-based access is needed. | Verify index boundaries. | | **Positional List** | Access by position. | When relative element order matters and position-based access is needed. | Check `before()` and `after()` methods. | | **Tree** | Hierarchical data structure. | Representing hierarchical relationships. | Verify parent-child relationships. | | **ArrayList Add(i, e)** | Insert element at index i, O(n) | Inserting into an index list. | Confirm correct element shifting. | | **Doubly Linked List AddBefore(p, e)** | Insert element before position p, O(1) | Inserting into a positional list. | Check pointer updates. | ### ๐Ÿ› ๏ธ Problem Types **Type A: Implementing an Index List with an Array** *Setup:* "When you need to implement an index list using an array and analyze the time complexity of its operations." *Method:* "Use an array to store the elements. The `get(i)` operation takes O(1) time. The `add(i, e)` and `remove(i)` operations take O(n) time because elements need to be shifted." *Example:* "Implement an index list with `add(0, 1)`, `add(1, 2)`, `get(0)`, `remove(0)`. The list should output 1, and the remaining element should be 2." **Type B: Implementing a Positional List with a Doubly Linked List** *Setup:* "When you need to implement a positional list using a doubly linked list and analyze the time complexity of its operations." *Method:* "Use a doubly linked list to store the elements. The `before(p)`, `after(p)`, `addBefore(p, e)`, `addAfter(p, e)`, and `remove(p)` operations all take O(1) time because nodes can be inserted and removed by updating pointers." *Example:* "Implement a positional list and perform operations to add and remove elements at specific positions." ### ๐Ÿงฎ Solved Example **Problem:** Implement a function to find the height of a binary tree. **Given:** The root node of a binary tree. **Steps:** 1. If the root is None, return 0. 2. Recursively calculate the height of the left subtree. 3. Recursively calculate the height of the right subtree. 4. Return the maximum of the left and right subtree heights, plus 1. > [!success] **Answer:** ```python def height(root): if root is None: return 0 left_height = height(root.left) right_height = height(root.right) return max(left_height, right_height) + 1

โš ๏ธ Common Mistakes

โŒ Mistake 1: Forgetting to handle the case where the root is None when calculating the height of a tree.

โœ… How to avoid: Always check for the base case (empty tree) in recursive functions.

โŒ Mistake 2: Incorrectly updating pointers when inserting or removing elements in a positional list.

โœ… How to avoid: Carefully manage the pointer updates to maintain the integrity of the list.

๐Ÿ’ก Study Tip

Draw diagrams of trees and lists to visualize their structure and operations. Practice implementing these data structures to reinforce your understanding.

4 more sections

Create a free account to import and read the full study notes โ€” all 6 sections.

No credit card ยท 2 free imports included

    Data Structures and Algorithms First Exam - Cheatsheet โ€” Cheatsheet | Evrika | Evrika Study