CS235 Final Review

Réussis tes devoirs et examens dès maintenant avec Quizwiz!

Unified Model

4 Phases: Each is a mini-waterfall Inception Elaboration Construction Transition

Procedural Abstraction

Figure out WHAT your function needs to do without coding it up -

Amortized Big-O

Focuses on run time in practice/average performance

Template classes

'typename' list<Data_Type> class 'class_name' {} vector <int> myVector Templates are a way of making your classes more abstract by letting you define the behavior of the class without actually knowing what datatype will be handled by the operations of the class. In essence, this is what is known as generic programming; this term is a useful way to think about templates because it helps remind the programmer that a templated class does not depend on the datatype (or types) it deals with. To a large degree, a templated class is more focused on the algorithmic thought rather than the specific nuances of a single datatype. More reusable

Red-Black trees

-Root always black -Inserted ones come in red -All red need to have 2 black children -Null is black -Number of black nodes from root to any leaf is the same for all leaves -Black can have black children -CASE1: Red uncle -CASE2: CASE3:

2-3-4 Trees

-Same as 2-3 but can have 4

primary clusters

- tendency for certain open-addressing hash tables collision resolution schemes to create long sequences of filled slots. It is most commonly referred to in the context of problems with linear probing.

Loop invariants

-3 Conditions for Invariants -Must be true before loop execution begins -Must be true at the beginning of each repetition of the loop -Must be true after loop exit

Iterators

-Can advance forward of backward -Used to process each element in a list when the operator[] does not apply -Used like a pointer -returns a reference

Huffman Trees

-Code for symbols -length of 0101010 depends on frequency -Full, not complete -Huffman tree implemented using a binary tree and a priority_queue - Huffman tree Insert lowest two numbers The sum above those

Friends

-Declaring 'friend' gives same right as a member function of a class -->can use private member data

quick sort

-Given an array to sort, rearrange this array into two parts so that all the elements in the left sub-array are less than or equal to a specified value(the PIVOT) -rearranging == partitioning -apply quicksort recursivly to the two sub-arrays on either side of the PIVOT -O(n*log n) -WORST: O(n^2) -To partition: use pointers up and down. move them until up is greater than PIVOT and down i less than PIVOT, then swap them. -if up passes down, swap PIVOT with down -Use median of 3 to avoid worse case scenario (one sub-array is size 0): arrange 1, middle, and last elements and return middle index -

Self-Balancing Search Trees

-Have O(log n) performance-->b/c normal BST could potentially have O(n) time

Interface Implementation

-Interface describes the capabilities of a class without committing to a particular design

Shell sort

-O(n^(3/2)) or better -Divide and conquer approach to insertion sort -Don't sort entire array at once! sort sub-arrays (of size 2 or 3) using insertion -then sort the sub-arrays together one by one -do one final insertion sort -EFFICIENT ON SMALL data sets -2.2 -Better than normal insertion sort b/c each new insertion will be on a SORTED array-->optimizing O() -

collisions

-Occur in Open addresssing of hash functions -collision: two keys referencing the same location in the hash table -reduce the # of collisions by increasing table size and re-hashing -Also reduce collisions by Qudratic probing

The function call operator ( operator() )

-Operator that can be overloaded by a class -Function Class: a class that overloads this operator -Function object: object of a class that overloads this operator -NO named function really, when using it in the main just have Object_Name(whateveryouwant); -->not need for Name.This_Functoin();

Merge sort

-Performed on TWO sequences in which the object of both are ordered by the same comparison operator --->result is to create a third sequence that contains all of the objects from the first two sorted sequences -B/c the two sequences are already ordered, you just take the first from one sequence until the second is larger -O(n) to merge -Split full array into smaller and smaller pieces until size = 1, then merge up; -B/c splitting array in half the # of lines that need merging is log n. --->TOTAL effort to reconstruct sorted array is O(n * log n)

Exceptions

-Use a try() loop -throw errors if certain conditions are met/not met

2-3 Trees

-When node has 3 children, pop the middle one up

Hash functions

-any function that can be used to map digital data of arbitrary size to digital data of fixed size. The values returned by a hash function are called hash values, hash codes, hash sums, or simply hashes. -Use of prime numbers decreases the number of collisions -Transform an items key value into an int. which will be transformed into an index value -Deterministic: Will always return the same value for a particular key -

Heap sort

-doesn't require n storage locations like MERGE sort -reminder: max heap is ADT with largest value on top -AFter removal of all items from heap: min-heap -In HEAP: CAN ONLY REMOVE TOP ELEMENT -After each heap removal, the next-largest item remains on the top while the largest-removed item is moved to the bottom -Consider first item to be a heap of 1 --->insert one item at a time and Restore heap property -Heap of size n, has log n levels with log n search -----O(n log n) -

Comparison vs Exchanges

-exchange requires to swap two objects using a third at a temp. -comparison will probably be more than an exchange b/c of the overhead to call and execute -exchange requires physically moving the info -cost of exchange is proportional to the size of the objects-->more costly on large objects

The "const" keyword

-must be assigned a value at declaration -Values can't be altered by user or function if CONST is after a function name, THEN it tells the compiler that the function can't change the variables

ADT's and operations

-set of data items, operations performed on them -Encapsulation of a set of objects A CLASS Stack, linked list, Class oriented programming!

Recursion compered to iteration

. Iteration is generally faster, some compilers will actually convert certain recursion code into iteration. Recursion is often more elegant than iteration. CAN ALWAYS write an iterative solution to a recursive fuction, However the recusion may be easier to write/debug -

Waterfall Method

1.REquirements 2.Analysis 3.Design 4.implementation 5.Testing

Chaining

A Way to organize a hash -Alternative to open addressing -Each table element refers to a linked list of values that all hash to the same index -When collisions occur, instead of going to the next slot, as in open addresssing, simply add the value to the linked list -In open addressing, search chains overlap with other hash indexes, this does not. All the values looked at will hash to the same value. -Can store more elements than the number of slots/indexes-->can insert without increasing size -

OPen Addressing

A Way to organize a hash table -each hash table element points to a single key-value pair -can apply linear probing to access an itme -if that element is full, we increment the hash value by 1 continually until finding the key we are looking for or NULL -If we reach NULL, the key value is not in the table -table wrap-around OR increase table size -hash_function_int % prime_number_size_of_table = key -list values in an arbitrary order -If no collisions O(1) -CANNOT set deleted item to NULL b/c there may be values after it that during linear probing will not be accounted for b/c NULL was reached. -fill deleted space with a 'dummy value' --Dummy values dcrease efficiency b/c no new value can be planced there and makes traversal at worse O(n)

Sets

A collection that contains NO duplicate elements and at most one NULL element -set ADT is not ordered but the representation of it IS ordered -Key_Type: the type of the item contained in the set, it is a template -Compare: A function class that determines the ordering of the keys, by default the less-than operator --Vectors require a location to store a value, sets do not! implemented with Balanced BST

SUbset

A is a subset of B if every element in A is also in B

Polymorphism

A pointer of base-class (parent) type can point to all derrived class (child class) objects. Only members of the base class can be accessed. important feature of OOP

Sorting

A sorting algorithm is an algorithm that puts elements of a list in a certain order. The most-used orders are numerical order and lexicographical order. Efficient sorting is important for optimizing the use of other algorithms (such as search and merge algorithms) which require input data to be in sorted lists; it is also often useful for canonicalizing data and for producing human-readable output. More formally, the output must satisfy two conditions: The output is in nondecreasing order (each element is no smaller than the previous element according to the desired total order); The output is a permutation (reordering) of the input. Further, the data is often taken to be in an array, which allows random access, rather than a list, which only allows sequential access, though often algorithms can be applied with suitable modification to either type of data.

Stable sort

A sorting algorithm is called stable if it keeps elements with equal keys in the same relative order in the output as they were in the input. For example, in the following input the two 4's are indistinguishable: 1,4a,3,4b,2 And so the output of a stable sorting algorithm must be: 1,2,3,4a,4b Bubble sort, merge sort, counting sort ,insertion sort are stable sorting methods. Most implementations of quicksort are not stable sorts. BASICALLY: if there are two occurrences of an item in the unsorted array, the first one will be first in the sorted array

tree sort

A tree sort is a sort algorithm that builds a binary search tree from the keys to be sorted, and then traverses the tree (in-order) so that the keys come out in sorted order. Its typical use is sorting elements adaptively: after each insertion, the set of elements seen so far is available in sorted order. -O(n*log n)

Virtual functions

A virtual function is a member function that you expect to be redefined in derived classes. When you refer to a derived class object using a pointer or a reference to the base class, you can call a virtual function for that object and execute the derived class's version of the function.

Union

Adding all values from Set A to Set B (disinclude duplicates)

Structured walkthrough

Aim is to detect errors NOT correcting them hand-tracing

Tree Fullness

All internal nodes have 2 children -Two children or NO children

Tree Completeness

All levels each Node has 2 children, except the last level, where the last element is pushed as far left as possible.

B-Trees

Allows cap - 1 in each node -Each node has between (cap -1)/2 to (cap -1) -Root can have less than (cap -1)/2 -Designed for building indexes to very large databases stored on a hard disk

Abstract Classes

An abstract class is, conceptually, a class that cannot be instantiated and is usually implemented as a class that has one or more pure virtual (abstract) functions. A pure virtual function is one which must be overridden by any concrete (i.e., non-abstract) derived class. This is indicated in the declaration with the syntax " = 0" in the member function's declaration.

Deep copy

An object is copied and has it's own variables/data fields. Changing data in one object will not affect the other because they are seperate.

AVL Trees

Balance can't be more than +- one

copy constructor

Default: class_name(const class_name&); purpose: to create an independance copy of the object.

Binary Search Tree

EAch Node has two possible sub-trees -No need to sort elements b/c Tree sorts through insertion

Queues

FIFO

REcursive Call

Function calls itself with altered parameters -Unwinding the REcursion: process of returning up out of recursion from a base case;

B+Trees

Has keys

Comparison of Quadratic Sort

Insertion/Selection/Bubble Sorts -O(n^2) average -Best case is O(n) -Insertion gives best performance for most arrays --->takes advantage of any partial sorting -Best case occurs when array is already sorted -Bubble is usually worse -

Stacks

LIFO

Tail Recursion

Last Line is the recursive call

Struct

Like a class but with no operators

Function overriding

Method overriding is the ability of the inherited class rewriting the virtual method of the base class.

Recursion

Must have a Base CAse Function that contains a call to itself Can break down a problem into smaller versions of itself

Base Case of Recursion

NEEDED

Problems with Waterfall Model

No going back to redo steps

Trees

Nonlinear: heirarchal, recursive Node: multiple successors, only 1 predecessor

Postfix

Operators are written after their operands. The infix expression given above is equivalent to A B C + * D / The order of evaluation of operators is always left-to-right, and brackets cannot be used to change this order. Because the "+" is to the left of the "*" in the example above, the addition must be performed before the multiplication. Operators act on values immediately to the left of them.

Prefix,

Operators are written before their operands. The expressions given above are equivalent to / * A + B C D Operators act on the two nearest values on the right.

Infix

Operators are written in-between their operands. This is the usual way we write expressions. An expression such as A * ( B + C ) / D

Operator overloading

Overloading a method (or function) in C++ is the ability for functions of the same name to be defined as long as these methods have different signatures (different set of parameters).

Shallow copy

POinter of two objects (one copied from another) still point to the same values, so changing one object ALSO changes the other.

Insertion sort

Quadratic Sort! -Based on technique used by card players -start with sorted sub-aray consisting of first element only -insert each new element one at a time in it's proper place. -have to shift all the values larger than it down WORST CASE: O(n^2) comparisons BEST CASE: O(n) comparisons

Bubble sort

Quadratic Sorting Algorithm -Compared adjacent array elements, and exchanges their values if they are out of order. -THUS the smaller values "bubble up" to the top -At the end of each pass, only one element(the heaviest) is placed in the correct place. --->one less pair to test after each pass - -WORKES BEST when an array is nearly sorted; Worse case: O(n^2) Best Case: O(n2) comparisons and O(1) exchanges (the array is already sorted)

Selection sort

Qudaratic Sort! -sorts an array by making several passes through the array -searches through the entire array and selects the next smallest item in the array and placing it where it belongs. -O(n) -O(n^2) comparisons - IF ARRAY IS ALREADY SORTED: -Comparisons: O(n^2) -Exchanges: O(1)

bucket hashing

Same as chaining

refactoring

Solves abmbiguity of multiple inheritance

Multiple Inheritance

The ability of a class to extend to more than one derrived class. Creates lots of abiguity as one object inherits from two/more base-classes

Cardinality

The number of elements in a set/group Size

string::npos

This value, when used as the value for a len (or sublen) parameter in string's member functions, means "until the end of the string". string::npos is a constant ( -1) representing a non-position. It's returned by method find when the pattern was not found

Traverssals: Pre/Post/IN order

To traverse a non-empty binary search tree in pre-order, perform the following operations recursively at each node, starting with the root node: 1. Visit the root. 2. Traverse the left sub-tree. 3. Traverse the right sub-tree. To traverse a non-empty binary search tree in in-order (symmetric), perform the following operations recursively at each node: 1. Traverse the left sub-tree. 2. Visit the root. 3. Traverse the right sub-tree. To traverse a non-empty binary search tree in post-order, perform the following operations recursively at each node: 1. Traverse the left sub-tree. 2. Traverse the right sub-tree. 3. Visit the root.

Useful Recursion examples

Towers of Hanoi, Solving mazes

Hash table

Turn the items Key value into an int (it's hash code)

Big O of common ADT

Vector: -insertion/deletion -- O(n) -add/remove -- O(1) Binary Search: -- O (log n); WORSE CASE: O(n) Heapification: - O (n * log n ) Set: - intersection -- O(n) Map: -retreval -- O(1) -insertion -- O(1) Hash TAble: - if no collisions O(1) - worse case -- O(n)

Maps

a map facilitates use-cases of a set by allowing you to introduce an external key (map::key_type) to determine ordering of the element A set of ordered pairs of Key_Value and Data; The Key MUST be unique while the data does not; implimented with Balanced BST Map[k] = value

Equivalence classes

a subset of the form, natural grouping of elements that are related to one another,

Heaps

complete binary tree where the value in each node is greater than or equal to all values in the node's subtrees , models the ADT Priority Queue Root (sub-root) is the largest value, and 2 children Complete Binary Tree: only lowest level can A node have 1 child, pushed left Insertion: insert at lowest level, farthest left and then swap, swap, swap up till all sub-trees have smaller values CAN ONLY REMOVE ROOT

Deques

double ended Queue -

Difference

elements that belong ONLY to A or B

Intersection

elements that belong to BOTH A and B

quadratic probing

index = start_index*prob_num*prob_num % table.size() ; -clusters do not overlap as much -Simplify quadratic probing with index = (k + index) % table.size() -PROBLEM: can get stuck in infinite loop looking for an insertion spot! --->can never happen if table size is always 1/2 empty AND prime number size (but this wastes a lot of space) -

maps v indexed collections

key is like the element in a map, indexes select elements in a vector the keys for a map however, can have arbitary values (not restricted to 1, 0, 2) The subscript operator is overloaded : v = a_map[k]; //assign to v the value for key k map functions are all implemented by delegation to set functions map class overloads the subscript operator such that the key is used as an index

Balance

longest_right - longest_left path

Information Hiding

making data fields private Make a function to get dadta

Big-O

measurement of execution of an algorithm given the problem size n

Nibling

niece/nephew to aunt/uncle

Associative Container

not indexed do not reveal the order of insertion enable efficient search and retrieval of info allow removal of element without moving other elements around include SET and MAP

predecessor

one left, and then right, rigth right

successor

one right, and then left, left, left

priority Queues

priority queue : represented by a heap (like how a stack is represented by a linked list) is a data structure in which only the highest-priority item is accessible (as opposed to the first item entered) largest item always removed first

Capacity

refers to how many children! THe number of values in a node is CAP -1

max heap

root stores largest value

min heap

root stores smallest value

Multiset

same as SET but allows for duplicates


Ensembles d'études connexes

ECON 102: Ch. 1 - Ten Principles of Economics

View Set

Test 2 M&B - GI, fetal assessment, musculoskeletal, normal newborn assessment, neuro

View Set

Management 3720 Organizational Behavior

View Set