CS341 FINAL EXAM REVIEW

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

Array-based list approach

"list" of items, homogeneous typing, numerical ordering, fixed in size - until it's not!, possibly space-wasting, if dynamic - its memory allocation is costly (bc of resizing), easy access, have to remove all elements and insert in chosen order to insert/delete an element

Cuckoo hashing provides worst case O(1) lookup time - how is it able to do this?

Because there are 1 of 2 places that values can get mapped to - and if the # of keys is kept below half the capacity of the table, there will be no cycles and therefore no rehashing necessary.

Why do unsuccessful searches often require more time than successful searches?

Because unsuccessful searches could require searching the entire table.

What is a recursive condition? Base case?

Circumstances for which the function will call itself. The base case is the condition for which the recursion should stop.

Q20: T/F, does Cuckoo hashing have cycles?

T

Q8: T/F, 2-child BST deletion utilizes inorder successor?

T

what makes removing in an open-addressed hash table complicated?

when you remove an entry, you can't just leave a space open bc we could place an entry that doesn't belong in the spot bc of the probing technique. (have to stay consistent with the history of hashing/probing actions) THUS, occupied/empty/removed status

RBT left/right rotations

with parent x and child y, switches position of leaf child and its parent (assuming initial children aren't nullptrs)

Q21: What's the function for calculating load factor?

𝛼 = (# of current entries)/table size

Q15: What's the function for linear probing?

(key % size) + n

Q17: quadratic probing function?

(key % size) + n*n

Q16: What's the #1 downside of linear probing

(primary) clustering when data gets mapped in big chunks rather than spread out -> more likely to have collisions/decreases efficiency

preorder traversal

1) "root"/node -> left subtree -> right subtree 2) preOrder(node root) { if (root != null) { std::cout << root.getData( ) << " "; preOrder(root.getLeftChild( )); preOrder(root.getRightChild( )); } }

Q26: Rules of a RBT?

1) A RBT must be a type of balanced BST 2) ROOT node is BLACK 3) Every node begins as a RED node when inserted 4) If a node is RED, then it s children must be BLACK 5) every path from a node to a nullptr must contain the same number of black nodes 6) every leaf node (nullptr) must be BLACK [bonus: per #4, no adjacent RED nodes]

Full Binary Tree properties

1) Assuming a tree of height h, all nodes that are at a level less than than h have two children each 2) if the tree is empty, a full binary tree's height is 0 3) if a tree is not empty and has a height of h > 0, it's a full binary tree if its root's subtrees are both full binary trees of height h - 1 4) a full binary tree of height h ≥ 0 has (2ˆh) - 1 nodes 5) you can't add to a full binary tree without increasing its height because adding to a child/leaf node increases height

What methods do we need to define in our linkedNode class?

1) LinkedNode * getNext( ) [returns a pointer to the next Node in the chain] /void setNextNode(LinkedNode * nextNode) 2) LinkedNode * getPrev( ) [returns a pointer to the previous Node in the chain]/void setPrevNode(LinkedNode * prevNode)

Q4: 3 rules of Binary Search Trees?

1) Node n is greater than all values in its left subtree Tleft 2) Node n is less than all values in its right subtree Tright 3) Both Tleft and Tright must be Binary Search Trees

Q7: 3 conditions for removal of a node in a BST?

1) Node to be deleted is a leaf node 2) Node to be deleted has one child (switch child and parent position and delete parent as a leaf node) 3) Node to be deleted has 2 children (switch node to be deleted's position with its inorder successor -> delete inorder successor as a leaf node)

Tree Implementation Types

1) Representing Tree Nodes: must contain both data and pointers to node's children - where each node is an object 2) Array-based: pointers will be array indices 3) Link-based: uses C++ pointers

1-child RBT deletion

1) There are two connections to manage: one to parent, one to child and one to parent 2) need to reassign pointers to move the child up to existing parent node and link them together

basic RBT class construction

1) TreeNode inherits from Node and adds parent, left child, right child, and color storage 2) RBT is initially empty, so root (BT) should be set to nullptr 3) insertion (RBT version) must set newNode to red to start 4) if RBT is empty, set root (BT) pointer to newNode and set its color from insertion red to root black 5) insert secondary nodes as leaf nodes (red, but follows rules bc no adjacent red nodes and new node's children will be black) 6) [where recursion comes in!] left/right subtree (when root isn't a nullptr): newNode < root, get and set leftChild and setParent(root); else if(newNode > root), get and set rightChild and setParent(root)

What methods should be in a list class?

1) bool isEmpty( ) [if head_ == nullptr && tail_ == nullptr, returns true] 2) int getLength( ) [return linkedListLength_ ] 3) void insert(int element) [creates new LinkedNode(int data, *prev, *next) object, increases list length; if(head_ == nullptr), head_ and tail_ are set equal to new LinkedNode object; else, set tail_ to nextNode_ then set tail_ to a new LinkedNode object]

Tree Implementation - Key points

1) each TreeNode will maintain 3 pointers (parent, left child, and right child) 2) the root TreeNode will have no parent (parent = nullptr) 3) any leaf TreeNode will have no left or right child (both = nullptr) 4) connections must be maintained (destructed as needed) in order to avoid dangling pointers/references

general Binary Tree main properties

1) every node can have at most two children (left and right child) 2) max # of nodes a binary tree of height h can have is (2ˆh) - 1

Properties of a list

1) finite number of objects 2) duplicate entries may exist 3) homogeneous data type 4) ordered by position via user, not data structure (index)

inserting in a RBT

1) follow the path from the root to the appropriate Node, where an established Node will be the parent of the newNode 2) when the parent is recognized, the newNode is connected either as its left or right child 3) account for balancing via rules and rotation causes

cuckoo hashing operations details

1) insertion is O(1), as long as the # of keys is kept below half the capacity of the table (no chance of cycles) 2) deletion O(1), bc only need to search the two locations

What basic properties do we need to define in our linkedNode class?

1) int data_ 2) LinkedNode * nextNode_ 3) LinkedNode * prevNode_

Complete binary tree properties

1) it's a binary tree that is full down to level h-1, with level h being filled in from left to right 2) all nodes at level h-2 and above have two children each 3) when a node at level h-1 has children, all nodes to its left at the same level have two children each 3) when a node at level h-1 has one child it's a left child

inorder traversal

1) left subtree -> "root"/node -> right subtree 2) inOrder(node root) { if (root != null) { inOrder(root.getLeftChild()); std::cout << root.getData() << " "; inOrder(root.getRightChild()); } }

postorder traversal

1) left subtree -> right subtree -> "root"/node 2) postOrder(node root) { if (root != null) { postOrder(root.getLeftChild( )); postOrder(root.getRightChild( )); std::cout << root.getData( ) << " "; } }

basic removing in a RBT

1) need to change the appropriate child attribute (pointer) within node-to-be-deleted's parent to point to nullptr 2) delete node-to-be-deleted from heap to avoid memory leaks (happens through properly distributed destructors for Nodes!)

RBT properties (aside from 6 main)

1) the red and black coloring helps maintain balance, so also efficient operations 2) most BST operations take O(h) time (where h is height of tree), but can quickly become O(n) (where n is # of inputs) if the tree is unbalanced

Max height of a RBT formula

2 log (# of internal nodes) + 1) = max height

Q6: How many nodes in a BST of height 4?

2ˆ(4) - 1 = (16 -1) -> 15 nodes!

Q11: If a hash table's key is 5 and its size is 4, what's the key's bucket/index position?

5 % 4 = (position) 1

what is a trie?

AKA a prefix tree, it's a tree (ordered), but for strings. NOT a binary tree, as it can have many children. (Apart from BT) Could implement using a linked-list-like data structure to maintain connection to children nodes.

Explain the difference between a list and an array based on the structure of their data.

An array is sequential, while a list is not necessarily indexed. An array can be operated on arithmetically while a pure list cannot.

SLL vs DLL in separate chaining

DLLs don't need to iterate through whole list to operate on the data (O(1)) - just have to reassign pointers to proper links, while SLLs have to maintain links one way so iteration through list must be done to operate on a node (makes it O(n))

In a complete BT, nodes that are at level h-2 and above have how many nodes each? Why?

Each of the nodes have two children each bc at level h-1, nodes that have children fill to the left, but don't always have two children each. If level h-2 nodes don't have two each, the binary tree becomes incomplete. (can't have the next nodes because two or no children in a BT)

Q25: T/F, separate chaining is more optimal than cuckoo?

F, (ON AVG) SLL is O(N) and DLL is O(1), while cuckoo's worst-case is O(1)

Q23: T/F, is a trie a type of BST?

F, bc it can have as many children as it likes while a BST can have at most 2 children (l/r)

Q2: What makes Linked Lists problematic in optimization?

Have to maintain each link to look at all the data. Can't really look at whole picture at once, have to traverse.

do array-based list implementations have a getLength( ) method?

I'm sure there's a way, but shouldn't you already know the length if it's static?

Main issue with array-based list implementation

If we have (int items [100];), how much of that space is really going to be used..? There's likely a large waste of space with the fixed size (no re-sizing)

What is a vector?

It works the same as a dynamic (@ runtime) array in the sense that memory needs to be managed (on heap), but it can be resized (not static!). Data is stored contiguously (all one block) and accessed/traversed via iterators (fxns that point to particular values in the vector). Containers that can resize as needed during runtime or during insertions/deletions for optimization purposes.

Q22: What is load factor?

Measures how full as hash table is and therefore its efficiency bc if the load factor is exceeded, the table becomes inefficient and chance of collisions increases -> may need to increase size of table.

Link-based list approach

Not fixed size (in theory - only limited by OS memory capacity), only links between elements (only prev and next), takes up more memory bc links must be maintained, traversals are expensive bc a search requires maintaining each link and using pointers. But, operations are more efficient - no redoing like in array-based.

Q19: What is the run-time/time complexity for separate chaining using SLL?

O(n)

what is quadratic probing?

Quadratic probes eliminate the clustering of a linear probe. In a linear probe the primary hash index is x. Subsequent probes go to x+1, x+2, x+3...

Q12: T/F, does a perfect hash function maps each key to its own place?

T

Q18: T/F, is separate chaining a closed-addressing technique?

T, bc multiple values are allowed to be mapped to the same bucket

Q5: T/F, if tree T is empty, is it considered a full tree?

T; 2ˆ(0) - 1 is 0 nodes, as per the Full Binary Tree rules. (also note that h>=0 is in the proof)

How is hashing similar to a bit dictionary?

Taking a value and figuring out where it belongs in a table

Is it possible to have a node without data?

Technically no? If a node doesn't have data, then its only parameter is null and it's rendered useless. If it's linked and its pointers are null, it can't have connections but it still has its value. But, nodes can be nullptrs, like leaf nodes in BTs... They have pointers to them, so there's technically data.

T/F: anything you can write with a loop, you can write with recursion

True!

Q13: What's a hashing collision?

When 2 keys get mapped to the same location

Are DLL node deletions handled recursively?

Yes. 1) If the data-to-be-deleted is the head, old head -> setNextNode(nullptr) and the newHead -> nextNode, then newHead -> setPrevNode(nullptr) 2) If the data-to-be-deleted is the tail, old tail -> prevNode(nullptr) and the new tail -> prevNode, SET to nextNode(nullptr) 3) else, curNode -> head initially; WHILE curNode != null, POINT/SET nextNode -> prevNode and POINT/SET prevNode -> nextNode; curNode -> prevNode(nullptr) and other curNode -> nextNode(nullptr); else, curNode -> nextNode 4) Lastly, make sure to delete pointers

basic class construction of a binary tree

[BT/TreeNode] each node will have a pointer to the two types of children nodes (left/right), root, and parent. the nodes can have children or be null, but IF both children are pointing at a nullptr then we know we're dealing with leaf nodes.

what is the perfect hash function?

a function that maps each search key into a unique location of the hash table

what's the simplest hash table?

an array of records, where the key is the value to be hashed into an index of the array - the hash value of the key

what is double hashing?

applies a second hash function when a collision occurs, giving the mapped location and a semi-unique step value to go by

what is cuckoo hashing?

applies multiple choice (uses two hashing fxns) AND relocation (1 of 2 places) (worst-case O(1));

What are the two ways you can implement a list?

array-based and link-based

Q24: What color must leaf nodes be in a RBT (at its end point)?

black

Q9: What color is the root of an RBT?

black

bool LinkedNode::hasNextNode( ) concept

bool nextNode(false); if(nextNode_ != nullptr) { nextNode = true; } return nextNode; if(nextNode_ == nullptr) { return false; } return true;

bool LinkedNode::hasPrevNode( ) concept

bool prevNode(false); if(prevNode_ != nullptr) { prevNode = true; } return prevNode; if(prevNode_ = nullptr) { return false; } return true;

what are the main components of closed-addressing?

can have multiple values in a bucket at once, via restructuring of the table (LLs)

what is the main component of open-addressing?

can only have one value in a bucket at a time

How does link-based implementation begin?

create the node(int val), viewing it as a class

Q3: What 2 pieces of info need to be maintained in data structures like trees, linked lists, and tries?

data and a link (something like parent/child are part of data)

Vector vs. pure Array

dynamic sizing vs. static sizing, unordered vs. ordered, non-indexed vs. indexed, any type vs. one type, consumes more memory (bc metadata for resizing) vs. less memory, heap vs. stack

cuckoo hash functions

h1(key) = key % size of table h2(key) = (key / size) % size

how does linear probing work?

hash sequentially, starting from the original hash location until the next open spot (wraparound allowed);

Q1: What are the two class-level attributes needed in a Linked List?

head and tail

why is balancing a binary tree important?

if a tree becomes skewed, the search becomes inefficient and unoptimized to operate on. it allows for optimal search/insertion/deletion times within the tree

regular hashing function to map a key to a bucket

key % size of table

Q10: In BSTs, of the 3 traversal strategies, how does post-order work?

left subtree, right subtree, root

Q14: what's an example of an open-addressing collision scheme?

linear probing, quadratic probing, double hashing, increase size of hash table

What ADT has a head and a tail?

lists

what is a cycle?

no open buckets exist for any hashing function - technique maps one value back to its original place after collision being remedied -> whole data structure needs to be rehashed

Do you ever have to rehash when using separate chaining?

no!

What are internal nodes in a RBT?

nodes that have children

2-child RBT deletion

replace node-to-be-deleted with inorder successor (node with the next-highest value). find successor finding the largest node in the left subtree of the node-to-be-deleted

what collision techniques are closed-addressing?

separate chaining (using SLLs and DLLs)

what is separate chaining?

technique that avoids collisions altogether by chaining entries together within their given bucket.

what determines a fully balanced binary tree?

the rightmost subtree's height differs from the left subtree's height by no more than one

empty status

this indexed position has never been used by an entry

occupied status

this indexed position is currently being held by an entry in the hash table

removed status

this indexed position previously held an occupied entry but is now available (tells us to keep looking, as the original inserted value used a different hash to find this position when it was open)


Ensembles d'études connexes

Bio 180: EXAM #3: Pretest Questions

View Set

Module 3: 3.04, 3.06, and 3.07 Lessons

View Set

Sports Medicine Semester 1 Final

View Set