CST370 - Week 6
CST370 Week 6
This week, we learned more about tree balancing and hashing.
AVL trees are self-balancing binary search trees where the balancing factor of each node is either 1 (left), 0, or -1 (right). The balancing factor is calculated by how many 'edges' traveled from the node to the last leaf it can reach (or the height of the subtree):
Balancing factor = height of left subtree - height of right subtree
To maintain AVL trees, rotations may need to be made on nodes. For left-heavy trees (or subtrees), R-rotations are typically used, while right-heavy trees (or subtrees) use L-rotations. Rotations can be used multiple times in a row, like two R-rotations needed.
2-3 trees are binary search trees that must have at least 2 or 3 children to each node. Each node can contain one value or two values - if it has one value, it will have 2 children, and if it has two values, it will have 3 children. The children can contain one or two values regardless of the parent. It is also height-balanced where all leaves are at the same level.
Heaps are complete binary trees (all levels besides the last level must be filled). There are two variations: min heap has the smallest value as its root node, and each subtree node must be smaller than its children. Max heap has the largest value as its root node, and each subtree node must be larger than its children. In heaps, the children do not necessarily need to be sorted.
Hashing is the process of taking a value, hashing it against an algorithm, and then placing it in a hash table using the generated hash. We can store and retrieved hashed data from the table using that algorithm. A big issue hashing can run into is collisions, where different entries are given the same hash (leading to the same index in the hash table) through the algorithm. We can handle collisions by either creating a linked list of all entries with the same hash (chaining), or by finding the next open slot in the hash table for the entry (linear probing). To maintain an optimal time complexity in hash tables, we use a load factor:
Load factor = (number of current entries) / (number of total slots in the hash table).
The optimal hash table size is a prime number. We maintain this load factor by resizing the table to twice its size, then find the next largest prime number. Then, we rehash the values if needed to maintain the hash table (usually for situations where the size of the table is used in the hash algorithm).
Isopod Update: They are in a new room currently so I have to monitor over time if anything's different.
Comments
Post a Comment