CMSC 132, Exam 2

0.0(0)
Studied by 0 people
call kaiCall Kai
learnLearn
examPractice Test
spaced repetitionSpaced Repetition
heart puzzleMatch
flashcardsFlashcards
GameKnowt Play
Card Sorting

1/33

encourage image

There's no tags or description

Looks like no tags are added yet.

34 Terms

1
New cards

Recursion

A method for solving problems where a method calls itself

2
New cards

Properties of Recursion

  • relies on the runtime stack as every call gets its own stack space

  • all problems can be solvable with iteration (and its usually more efficient)


3
New cards

Parts of recursion

  • The base case is a problem instance that’s simple or trivial

  • the recursive step is what recalls the function


4
New cards

Recursion Factorial example

public static int factorial(int n) {

If (n == 0)

return 1;

else return n * factorial(n - 1);

}

5
New cards

Binary Search

  • A type of Array search where you repeatedly split the array in half until you find the number

  • Array must be ordered


6
New cards

Big O

Represents an upper bound on the number of operations performed by an algorithm for a sufficiently large input size

7
New cards

Determining asymptotic complexity

As n increases, the highest-order term of a function dominates its value

Ex. 2n+100 is O(n)

Ex. Log3(n) is O(log(n))

Ex. n³ + 100n² + 28 is O(n³)


8
New cards

Analyzing Algorithms

  • Find the Critical Section (The part of the code that is run the most)

  • Ignore the less frequently executed parts

  • Determine how many times the critical section is executed


9
New cards

What are 2 important operations to know?

1 + 2 + 3 +…+ (n-1) + n → n(n + 1) / 2

1 + 2 + 3 +…+ (n-2)+(n-1) → n(n-1) / 2

<p>1 + 2 + 3 +…+ (n-1) + n → n(n + 1) / 2</p><p>1 + 2 + 3 +…+ (n-2)+(n-1) → n(n-1) / 2 </p>
10
New cards

Types of case analysis

  • Best Case

  • Worst Case

  • Average Case (average and expected)

  • Amortized Cost


11
New cards

Best Case

  • Smallest number of operations that it requires

  • Usually not that useful to know

  • Ex. First element in a linear search


12
New cards

Worst Case

  • Largest number of operations that an algorithm requires

  • Useful as an upper bound on performance

  • Ex. Last element in a linear search


13
New cards

Average Case

  • The “Typical amount of cases expected

  • Normal computed by finding the average case with all cases being the same likelihood

  • Usually not realistic but easy to compute and compare

  • The expected case is a weighted average over all possible cases

  • More accurate harder to compute


14
New cards

Amortized cost

applies to the worst-case sequences of operations

gives average running time per operation

15
New cards

Iterator interface

  • next() should return some element

  • hasNext() should return true if any elements haven’t been returned by next() yet

  • remove() is optional; can be called once after each call to next() to remove the element that next() just returned


16
New cards

How to implement it

add an iterator interclass if the class implements Iterator

17
New cards

Design Patterns

  • descriptions of reusable solutions to common software design points

  • captures the experience of experts


18
New cards

Collection interface

  • The root interface of Java’s collection hierarchy


19
New cards

Collection methods

  • add

  • clear

  • contains

  • isEmpty

  • iterator

  • remove

  • siF


20
New cards

foreach loop with iterators

  • You can use a foreach loop for any class that implements Iterable including all Collections

  • for each loops are handled in the JVM with hasNext() and next()


21
New cards

Stacks and Queues

  • Both simple data structures

  • Stacks FILO

  • Queues FIFO

  • Stacks are used in the runtime stack

  • Circular Array for queues uses front = (front + 1) % capacity


22
New cards

Some Design Patterns

  • Creational: Deal with the best way to create objects

  • Structural: Ways to bring together groups of objects

  • Behavioral: Ways for object to communicate and interact


23
New cards

The adaptor pattern

  • converts existing interfaces to a new interface

  • Helps match an interface

  • Increases transparency of classes

  • Ex: implementing a stack with an already implemented linked list


24
New cards

Binary trees

Tree with at most two children per element

25
New cards

Tree traversal

DFS: visits far ahead then backs up

BFS: visits elements according to how far away they are from the root, using a queue


26
New cards

DFS

Three types

  • preorder: MLR

  • In order: LMR, in a binary search tree leads to ordered list

  • Postorder: LRM


27
New cards

Polymorphic binary search tree

Uses NonEmptyTree and Empty Tree

  • Can use a singleton design pattern and create only one empty tree object


28
New cards

Binary Search Tree deletion

  1. Perform a search for value X

  2. If x is a leaf remove X

  3. a) replace X with the largest element In its left subtree or the smallest element in its right subtree

  4. Then delete the replacement value from the subtree recursively



29
New cards

Balanced and degenerated binary tree

  • a balanced binary tree has mostly 2 children per element

  • A degenerate binary tree has mostly one child per element


30
New cards

Binary tree height

A perfectly balanced tree number of elements is 2^(heights + 1) - 1

Finding the height of a balanced tree is O(log(n))

height of a degenerate tree is O(n)

31
New cards

Binary search tree operations

Lookup(average case)

  • Balanced tree: O(log(n))

  • Degenerate tree: O(n)

Insertion(average case)

  • B tree: O(log(n))

  • D tree: O(n)

Deletion: same as insertion


32
New cards

Factory Pattern

  • provides an abstraction for deciding which class should be instantiated based on parameters given

  • Used when a class cannot anticipate which subclass should be made,

  • or to separate a family of objects using shared interface

  • or to hide concrete classes from the client


33
New cards

Decorator pattern

Attaches additional responsibility or functionality to an object

  • provides a flexible alternative to subclassing

  • Add new functions to an I object without affecting other object

  • Make responsibilities easily added and removed dynamically and transparently to the object

  • Ex: decorator that adds toppings to pixxa


34
New cards

Maker interface

  • label semantic attributes of a class

  • Need to indicate attributes of a class

  • Allows identification of attributes of objects without assuming they are instances of any class