0% found this document useful (0 votes)
32 views3 pages

COMP 250 Midterm 2 Crib Sheet

content for comp250 midterm 2

Uploaded by

christinacui841
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
32 views3 pages

COMP 250 Midterm 2 Crib Sheet

content for comp250 midterm 2

Uploaded by

christinacui841
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

COMP 250 Midterm 2 crib sheet

Stacks: Linear LIFO (last in, first out) data structure with a reference to the element at the top. There are two
possible implementations: array-based and linked list based (singly or doubly), with the linked list version being
preferred. Possible operations: Push (adds element to the top), pop (remove the top element) and peek (get the
element at the top), isEmpty(check if the stack is empty). Applications: Balancing parentheses problem, graphics,
call stack, browsing history, etc. All operations are O(1) with linked list implementation.

Stack overflow/underflow: Stack overflow (runtime error) occurs when we attempt to push elements to a full
stack (when it has a finite size), and stack underflow (runtime error as well) occurs when we attempt to pop an
empty stack.

Queues: Linear FIFO (first in, first out) data structure with an underlying circular array and two pointers, one to the
head and the other to the tail of the queue. Possible operations: enqueue (add to the back), dequeue (remove from
the front). Move the pointers accordingly when performing operations! Increase the length of the array when the
array is full and keep track of the head and the tail. Index of tail = (index of head + queue size – 1) % array length.
Resize array if we call enqueue while the array is full.

ADT (Abstract data type): Model for a data type that defines a data type by its behavior from the user’s
perspective only. It describes the possible values and the set of possible operations on the data type. It ignores the
specific details of the implementation and is more abstract than a data structure. Data structure = concrete
representation of data which includes the implementation details.

Interfaces: They can be declared public or package-private like classes and can have fields and methods, but the
former are by default public and abstract, and the latter are by default public, static and final. CANNOT be
instantiated! Declared using the interface keyword and is implicitly abstract. Defines new data types

Implements: To use an interface, create a class that implements it (interface ≈ blueprint of class), specifies what a
class must do but not how. Class can implement ≥ 1 interfaces (used for subtyping), if it doesn’t implement all
methods in the interface then class must be abstract. Interfaces can inherit from another using the extends
keyword just like in classes. Can use an instance of a class that implements an interface as an instance of that
interface. A class can extend ≤ 1 class but can implement multiple interfaces.

Interface (1) vs abstract class (2): (2) not all methods abstract, must use abstract keyword, can contain
instance vars and implemented methods while (1) all methods abstract by default, no need to use keyword, no
implemented methods and only final static fields

Generic types: Class/interface parametrized over types, use angle brackets <> to specify the type parameter.
When we want to restrict the types that can be used as parameter, use extends keyword (bounded type). It will
limit the types we can use as well as allowing us to use the methods defined in the bounds.

Comparable: Interface used to define an ordering on user-defined classes that contains one method: int
compareTo(). To use, + implements Comparable in the header of class and implement compareTo() method. Defines
a natural ordering for the object.

Iterable/Iterator: Objects of type iterable are representation of series of elements that can be iterated over while
those of type iterator allow you to iterate through objects that represent a collection. Class that implements iterable
needs to implement iterator() that returns an Iterator object that allows iteration through elements of this instance,
while a class that implements iterator needs to implement the methods hasNext() and next(). iterator() returns
iterator to the start of collection, using next and hasNext to move forwards. Need new iterator obj to traverse the
collection again. When writing a class that implement Iterable, also write one that implements Iterator (often the
inner class the class that implements Iterable). Application: for each loop with arrays (by internally calling iterator()
on the object)
n n n 2
n (n+1) n(n+1)(2n+ 1) n(n+1)
∑ i= 2 , ∑ i2=
Math: Arithmetic:
6
and ∑ i =(
3
2
) ; Geometric:
i=1 i=1 i=1
n
1−x n +1 x n+1−1
∑ x = 1− x = x−1 (Series)
i

i=0

Solving recurrences: Use back substitution (e.g. T(n) = n + T(n-1) = n + n + T(n-2)…, T(n-1) = n + T(n-2) is
2
O(n )) Get general form + solve
Mathematical induction: Proof technique used to prove statements of the form: for all n ≥ k, P(n) is true. Start by
showing that base case/s (BC/s) (n=k) is true, then show that P(n+1) is true assuming P(n) is true for all n ≤ k
(induction step). Weak: 1 BC, Strong: ≥ 2 BC

Recursive algorithm: Function that calls itself within its definition, consists of one or more BCs (terminating
scenario that DOES NOT use recursion) and recursion/induction steps. If no BC or BC never reached, infinite
recursion, stack overflow error (RT)! Every algorithm can be written iteratively or recursively. Simple algorithms:
iteration easier + faster. Complex problems: recursion + elegant and simpler to code. Use the easier one.
Memoization = technique storing data making recursion faster (- unnecessary recursion calls).

Binary search: Search algorithm that finds an element in a sorted list using O(log n) time, as opposed to linear
search’s O(n) time but it needs a sorted list. Idea: Compare the element with element at index middle. If <
middle, continue searching on left (l) half. If > middle, continue searching on right (r) half. If element = middle,
return its index. Repeat until element found or l = r. To implement binary search, create three pointers, one for left,
l+r
one for right, one for middle m= . Pseudocode for it. (left) and rec. (right) implementations below
2

Merge sort: Divide & conquer sorting algorithm with O(n logn) time complexity (TC). Idea: Partition list into 2
halves, sort each recursively and merge sorted half maintaining the order. The partition and merging are done
recursively as well. More space efficient than Quick sort because QS takes 1 extra list. Below is the pseudocode for
merge sort (left) and the merge function (right)

Quick sort: Divide & conquer sorting algorithm with O(n log n) average/best TC and O(n 2) worst TC. Idea: Pick
element as pivot, partition list moving pivot to correct pos making sure elem < pos on its left and those > pos on its
right (placeAndDivide), sort L and R rec. until nothing left to sort. Implementation: Method swapping 2 elements
(temp var.), way refer 2 parts of list, P&D and implementation of QS using methods above (Pick pivot, P&D, QS L
and QS R). Use same idea as BS to denote part of list. Pseudocode below (QS left, P&D right)
Trees/Binary trees: Nonlinear data structures that’s a collection of nodes/vertices, with the top node being the
root. Implementation like a linked list, but more general (multiple children allowed), with pointer to root node. Tree
is binary (binary tree, BT) if it has ≤ 2 children (left/right). We can traverse a tree using depth-first pre (middle,
12345), in (BT only, 21435) or post (right, 24531) order traversal. In-order traversal for BT: recursive call to root.left,
visit node, recursive call to root.right, if root not empty.

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy