Intro to Data Structures - C243 - Midterm II

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

Explain what a hashing function is good for.

A hashing function will map the key to a subscript between 0 and capacity -1. The object will then be inserted in the array at that precise position.

How do you implement Table ADT?

A table is an abstract data type (ADT) that stores a collection of elements, each identified by a unique key. A table supports operations for adding, retrieving, and removing elements by their keys. There are several ways to implement a table ADT, but one common approach is to use a hash table. Here's a general overview of how a hash table can be implemented as a table ADT: -Define a structure to represent a key-value pair, such as a struct or a class. -Choose a hash function that maps each key to an index in the hash table. The hash function should distribute the keys evenly across the table to minimize collisions. -Define an array to store the elements in the hash table. Each element in the array should be a linked list to handle collisions. -Implement the add operation by computing the hash value of the key and adding the key-value pair to the linked list at the corresponding index. -Implement the retrieve operation by computing the hash value of the key and searching the linked list at the corresponding index for the key. If the key is found, return its value. Otherwise, return an error or a default value. -Implement the remove operation by computing the hash value of the key and removing the key-value pair from the linked list at the corresponding index if it exists. -Handle resizing of the hash table when the load factor exceeds a certain threshold. To resize the hash table, create a new larger array, rehash all the elements, and insert them into the new array. -Optionally, implement additional operations such as size, clear, and iteration. -By using a hash table, we can achieve efficient O(1) average-case performance for adding, retrieving, and removing elements, as long as the hash function and the load factor are well-chosen.

What is a Hash Table?

An efficient implementation of a table in an array with a particular structure.

Explain height-balanced search trees, when are they considered height-balanced?

Height-balanced search trees are a type of binary search tree in which the heights of the left and right subtrees of every node differ by at most one. These trees are designed to provide efficient search and insertion operations while avoiding the worst-case scenario of a degenerate tree, which can result in poor performance. A height-balanced search tree is considered balanced when the difference in height between its left and right subtrees is at most one. More formally, we can define a balanced tree as follows: For a node n in the tree, let H(n) be the height of its subtree. Then, the tree is height-balanced if for every node n, the absolute difference of H(n.left) - H(n.right) is at most 1. When a height-balanced search tree is constructed, the goal is to ensure that the height of the tree remains as small as possible while maintaining balance. This is typically achieved through the use of self-balancing algorithms that adjust the tree structure as nodes are added or removed. Some examples of height-balanced search trees include AVL trees, red-black trees, and B-trees. These trees are commonly used in a variety of applications that require efficient search, insertion, and deletion operations, such as databases and file systems. Overall, height-balanced search trees are considered balanced when the heights of their left and right subtrees differ by at most one. By maintaining balance, these trees provide efficient search and insertion operations while avoiding the worst-case scenario of a degenerate tree.

Explain these three ways to deal with collision handling: Linear Probing, Quadratic Probing, Double Hashing.

Linear probing is a technique used in hash tables to handle collisions between keys. When two or more keys map to the same slot in the hash table, a collision occurs. In linear probing, if the slot is already occupied, the algorithm checks the next slot in the table, and if that slot is also occupied, it continues to check subsequent slots until it finds an empty slot. Here's an overview of how linear probing works in a hash table: Choose a hash function that maps each key to an index in the hash table. Initialize an array to store the key-value pairs. Each element of the array represents a slot in the hash table. When inserting a key-value pair, compute the hash value of the key and use it to determine the index of the corresponding slot in the hash table. If the slot is empty, add the key-value pair to the slot. If the slot is occupied, use linear probing to search for the next available slot. Starting from the original index, check each subsequent slot in the array until an empty slot is found. Add the key-value pair to the first empty slot that is encountered. When retrieving a value for a given key, compute the hash value of the key and use it to determine the index of the corresponding slot in the hash table. Search the slot at the corresponding index for the key. If the key is found, return its value. Otherwise, continue searching the subsequent slots until an empty slot is encountered or the key is found. When deleting a key-value pair, compute the hash value of the key and use it to determine the index of the corresponding slot in the hash table. Remove the key-value pair from the slot at the corresponding index if it exists. Linear probing is a simple and efficient way to handle collisions in a hash table. It has a relatively low overhead and can provide good performance for small to medium-sized hash tables. However, it may suffer from clustering, where keys that map to the same index tend to be clustered together in subsequent slots, leading to slower retrieval times. To address this issue, other probing techniques such as quadratic probing or double hashing can be used.

How do you choose the Capacity M in a hash table?

Load Factor = N/M is the estimated number of records in a table. Load Factor = 80% full, 20% empty, never exceeds .80 M = 1.25N and M next prime number.

What is a splay tree?

Splay trees are a type of self-adjusting binary search tree that provides efficient access to recently accessed or frequently accessed items. The tree is designed to restructure itself on every access operation such that the accessed item moves to the root of the tree, thus reducing the cost of subsequent accesses to the same item. In a splay tree, each node has a key value and two children: a left child and a right child. The key value of a node is used to determine its position in the tree, such that all nodes in the left subtree of a node have keys less than the node's key, and all nodes in the right subtree have keys greater than the node's key. The restructuring of the tree is done by performing a sequence of "splaying" operations. A splay operation is essentially a set of rotations that bring a node to the root of the tree. During a splay operation, the tree is restructured such that the path from the root to the accessed node becomes shorter, and the accessed node becomes the new root of the tree. Splay trees have several advantages over other types of binary search trees. First, they provide efficient access to recently accessed or frequently accessed items. Second, they are self-adjusting, meaning that the tree structure is automatically optimized to reduce the cost of access operations. Finally, splay trees have good amortized time complexity for many operations, including access, insertion, and deletion, making them a useful data structure for a wide range of applications.


Ensembles d'études connexes

FINN 3160- Ch. 7 Equity Markets and Stock Valuation

View Set

PrepU: Bladder and Lower Urinary Tract (Chapter 35)

View Set

Chapter 31: Family Planning 4 & 6

View Set

ATI Medical-Surgical: Cardiovascular and Hematology questions

View Set