COMP 151 FINAL

Ace your homework & exams now with Quizwiz!

What's the benefits of using a dictionary?

A dictionary can keep track of duplicate entries -Utilized hashing

Dictionary implementation of a chain of nodes

- A chain of nodes that each references a search key and a value - Pointer pointing to the next node

What is an ADT SortedList?

- A collection of objects in sorted order and having same data type - Extends the capabilities of a list - Keeps list sorted in numerical or alphabetical order while the list is being established.

Tree Terminolgy

- A tree is a set of nodes connected by edges - The edges indicate relationships among nodes Nodes arranged in levels: - Top level is a single node called a root - Nodes at a given level are children of nodes of previous level - Node with children is the parent of those nodes - Nodes with same parent are sibilings - Node with no children is a leaf node

What is an ADT Tree?

- A way to organize data (family tree) Hierarchical organization - Data items have ancestors, descendants - Data items appear at various levels

What are Weighted Graphs?

- A weighted graph has values on its edges - A path in a weighted graph asl has a weight or cost

Operations of ADT List

- Add item at end (although can add anywhere) - Remove an item (or all items) - Replace an item - Look at an item (or all items) - Search for an entry - Count how many items in the list - Check if list is empty

Dictionary Operations

- Add new entry, given search key and associated value - Remove an entry, given associated search key - Retrieve value associated with given search key See whether dictionary contains a given search key - Traverse all search keys in dictionary - Traverse all values in dictionary - Detect whether dictionary is empty - Get number of entries in dictionary - Remove all entries from dictionary

In a dictionary, iterators specified allow traversal of...

- All search keys in dictionary without traversing values - All values without traversing search keys - All search keys and all values in parallel

Dictionary implementation of a an array

- An array of objects that encapsulate each search key and corresponding value - Worst Case efficiency on unsorted data: O(n)

Open addressing with Quadratic probing

- Avoid primary clustering by changing the probe sequence - Alternative to going to location index + 1 - Go to index + 1, then index + 4, then index + 9 In general go to index + (probe sequence)2 for probe sequence = 1, 2, 3, ... Formula: (hash index + probe sequence^2) % table size

How can we search through an Unsorted Array Iteratively?

- Boolean flag set to false to signal we found our specified entry - Iterate through the number of entries or if flag == true if statement: - if anEntry equals the index in the unsortedArray - Set boolean flag to true - Break from loop

Specifications for the ADT SortedList

- Duplicate entries allowed - Must determine where in list element is added - Can ask if list contains specified entry - Must be able to remove an entry

What is Separate Chaining?

- Each location of a hash table can represent multiple values called a "Bucket" - To add, hash to bucket, insert data in first available slot there - To retrieve, hash to bucket, traverse bucket contents - To delete, hash to bucket, remove item

What is a binary tree?

- Each node has at most two children - Every non-leaf in a full binary tree has exactly two children - A complete binary tree is full to its next-to-last level

What is an Iterator?

- Enables you to step through, or traverse a collection of data in a certain predefined order - Can tell whether next entry exists - Each data item is considered once during traversal

Level-order traversal with a queue

- First enqueues the root While the queue is not empty: - dequeues a node - visits the node - enqueues the node's children

What is an ADT Dictionary?

- Has search "keys" paired with corresponding "values" -Does not allow for duplicate entries -Method add() must deal with finding a duplicate search key -Method remove() and getValue() must deal with the duplicates

What is Binary Search of a sorted array?

- Ignoring one half of the data when the data is sorted - midpoint = first + (last - first) / 2

What is an ADT Graph?

- Is a collection of distinct vertices and distinct edges - Edges can be either directed or undirected - When edges are directed it is called a digraph

What is load factor?

- Measures table fullness - When dictionary is empty load factor is 0 - Maximum load factor depends on type of collision resolution used - To maintain efficiency load factor should be < 50% Formula: load factor = (# of entries in the dictionary / # of locations in the hash table)

How to resolve Collisions?

- Open addressing - Linear probing - Quadratic probing - Double hashing

Open addressing with Double Hashing

- Resolves collision by examining locations at original hash index and determines a step index to examine next. - Can reach all locations in the hash table if the table size is prime - Avoids both primary and secondary clustering

Search operation to add an entry to a dictionary

- Search probe sequence same as for retrieval Note first available slot: Use available slot if the key is not found

Search operation to retrieve an entry in the dictonary

- Search the probe sequence for the key - Examine entries that are present, ignore locations in available state - Stop search when key is found or null reached

Search operation to remove an entry from the dictionary

- Search the probe sequence same as for retrieval - If key is found, mark location as available

When is a tree's height balanced?

- The left and right subtrees' heights differ by at most one, AND - The left subtree is balanced, AND - The right subtree is balanced

How do we remove with an Iterator?

- This entry is the one returned by the next(). - You must call next() before you can call remove()

How to traverse a binary tree recursively?

- Visit the root - Visit all nodes in root's left subtree - Visit all nodes in root's right subtree

How to compute Step Index

1: Compute the key - key % table size 2: Compute step index - constant - ( key % constant ) Note: constant is a prime number and smaller than the array size

Two steps of the Hash Function

1: Convert the search key into an integer called the hash code 2: Compress the hash code into the range of indices for the hash table

What is an ADT List?

A collection of objects in a specific order and having the same data type -Can be implemented in an array or linked list

What is a binary search tree?

A search tree that organizes its data so that a search is more efficient - A node's data is greater than the data in the node's left subtree - A node's data is less than the data in the node's right subtree - Assuming no duplicates for now

What is a subtree?

A tree rooted at a child of that node

What are adjacent vertices

Also called neighbors - Two vertices are adjacent in an undirected graph if they are joined by an edge - In a directed graph below vertex A is adjacent to B, but B is not adjacent to A

What is level-order traversal?

Begin at root and visit nodes one level at a time

Efficiency of a Sequential Search of a chain of nodes?

Best Case: O(1) Worst Case: O(n) Average Case: O(n)

Efficiency of a Binary Search of an array

Best case: O(1) Worst Case: O(log n) Average Case: O(log n)

How do we traverse a tree?

Can visit root before, between or after subtrees

What are connected graphs, complete graphs, and disconnected graph?

Connected graph: Has a path between every pair of distinct vertices Complete graph: Has an edge between every pair of distinct vertices Disconnected graph: Not connected

How does a dictionary differ from a sorted list?

Dictionary: - Doesn't allow duplicates - Has keys corresponding to values - Has an iterator Sorted List: - Keeps list sorted in numerical or alphabetical order - Duplicates allowed - All objects have same data type

How are Hash Codes computed?

Hash code for a primitive type: - Use the primitive type key itself as int (cast byte, short, or char to int) Manipulate internal binary representations: - For example use folding (use exclusive-OR on parts of the key):

What is a heap?

Heap is a complete binary tree - Nodes contain Comparable objects

Why Are Iterator Methods in Their Own Class?

Inner class iterators have direct access to structure containing ADT's data: - Execute faster than separate class iterators Quick traversal but: - Only one traversal can be in progress at a time - Resulting ADT has too many operations

How does binary search algorithm compare with interpolation search algorithm?

Interpolation search works better than Binary Search for a sorted and uniformly distributed array.

What is Open Addressing?

Locates alternate location - New location must be open, available

What are Max/Min Heap?

Max: Object in a node are >= it descendant objects Min: Object in a node are <= descendant objects

What is Hashing?

Method to locate data quickly: - Ideally has O(1) search times - Yet cannot do easy traversal of data items Technique that determines index using only a search key

What is a Path, Cycle, and Weights?

Path: between two vertices(sequence of edges) Cycle: is a path that begins and ends at the same vertex (A graph with no cycles is acyclic) Weight: Shortest, fastest, cheapest cycle

What is Heapsort?

Place array items into a maxheap - Then remove them - Items will be in descending order - Place them back into the original array and they will be in order Efficiency: O(n log n)

What is an Adjacency List?

Represents only edges that originate from the vertex Space not reserved for edges that do not exist: - Uses less memory than corresponding adjacency matrix Adjacency list more often used than adjacency matrix

What is the interface Iterable?

Returns an iterator for a collection of objects of type <T>. - Can use a for-each loop to traverse objects in an instance of the class

Efficiency of BST

Searching a binary search tree of height (h) is O(h)

When should you choose between Sequential Search and Binary Search

Sequential Search: - Suitable for smaller array - Objects must have appropriate equals() method Binary Search: - Suitable for larger collections - Objects must have appropriate compareTo() method

What is the hash function?

The hash function receives the search key: - Returns the index of an element in an array called the hash table - The index is known as the hash index A perfect hash function maps each search key into a different integer suitable as an index to the hash table

Given a graph, how can you find a path between two given vertices that has the fewest edges?

The idea is to perform BFS from one of given input vertex(u). At the time of BFS maintain an array of distance[n] and initialize it to zero for all vertices.

sort a list containing multiple entries of 0, 1 , 2

The problem was posed with three colours, here `0′, `1′ and `2′. The array is divided into four sections:

How to compress a Hash Code?

Typical method for compressing a hash code c is to compute (c%n): - Where (%) designates the modulo operator - (n) is the size of the hash table - Index will then be between 0 and n-1 - (n) should be a prime number

What is an Adjacency Matrix?

Uses fixed amount of space: - Depends on number of vertices - Does not depend on number of edges Typically the matrix will be sparse Presence of an edge between two vertices can be known immediately All neighbors of a vertex found by scanning entire row for that vertex

What is Postorder traversal?

Vising the root after visiting the subtrees also called depth-first traversal left -> right -> root

What is Preorder traversal?

Visiting the root before the subtrees root -> left -> right

What is Inorder traversal?

Visiting the root between visiting the subtrees left -> root -> right

Given a graph, how can you check if a path exists between two given vertices?

We can either use Breadth First Search (BFS) or Depth First Search (DFS) to find path between two vertices.

What is Re-hashing?

When load factor gets too high: - Resize array to a prime number at least twice former size - Must rehash to different locations (key % new table size), based on new size, n, of array - utilizes method add

What is Collision?

When more than one search key is mapped to a single index

Can we use a for-each loop on any collection?

Yes, A client of the class can use a for-each loop to traverse objects in an instance of the class

Recursive algorithm for binary search of a sorted array

binarySearch ([]a, first, last, desiredItem) mid = first + (last - first) / 2 // approximate midpoint if (first > last){ return false } else if (desiredItem equals a [mid]) { return true } else if (desiredItem < a [mid]) { return binarySearch (a, first, mid - 1, desiredItem) } else{ return binarySearch (a, mid + 1, last, desiredItem) }

What is "perfect hash function"?

for a set S is a hash function that maps distinct elements in S to a set of integers, with no collisions.

How to recursively sequential search and unsorted array

if (there are no elements to search){ return false } else if (desiredItem equals a[first]){ return true } else{ return the result of searching a[first+1] through a[last] }

What is Linear Probing?

if when adding to the hash table contains the key and it's associated value: - We need to perform a sequential search to find another open slot in the table

How can we search through an Unsorted Array recursively?

int recSearch(int arr[], int left, int right, int x) { if (r < l) return -1; if (arr[l] == x) return left; return recSearch(arr, left+1, right, x); }

How do you swap two numeric variables in Java without using a third one?

int x = 10, y = 5; // Code to swap 'x' and 'y' x = x + y; // x now becomes 15 y = x - y; // y becomes 10 x = x - y; // x becomes 5

What is Huffman's Encoding algorithm? Why is it important?

is a lossless data compression algorithm. The idea is to assign variable-length codes to input characters, lengths of the assigned codes are based on the frequencies of corresponding characters.

What is a subgraph?

is a portion of a graph that itself is a graph

What is dynamic perfect hashing?

is a programming technique for resolving collisions in a hash table data structure

What is the length of the path in a tree?

the number of edges that compose it

What is the height of a tree?

the number of levels in the tree


Related study sets

General Surveying II (Control Survey)

View Set

Series 7- Chapter 3 Equity Securities

View Set

7.04 The Global Impact of Human Ingenuity Quiz

View Set

CHAPTER 9 - READING QUIZ QUESTIONS

View Set

Chapter 5-Assessment, Nursing Diagnosis and Planning

View Set