Week 6 – CST370 – Design and Analysis of Algorithms

This week we learned about AVL trees, 2-3 trees, heap, and hashing.

An AVL tree is a binary search tree that has a balance factor for each node that is either 0, 1, or -1. This means that the height of a node’s left subtree minus the height of the node’s right subtree must be either 0, 1, or -1. When inserting a new node into an AVL tree, you must recheck that the balance factor of each node is still within range. If the tree is unbalanced, you must perform either right, left, double right left, or double left right rotations to balance the tree.

A 2-3 tree is a tree that can have nodes that are 2-nodes or 3-nodes. A 2-node contains a single key with two children. The left child is less than the key and the right child is more than the key. A 3-node contains two ordered keys and three children. The left child is less than the two keys, the right child is more than the two keys, and the middle child is between both keys.

A heap is a structure that is partially ordered and suited for priority queues. It is very efficient for finding the max value, deleting the max value, and inserting a new item. You can use an array to represent a heap. The max value will always be the value at the lowest index, typically we use index 1 for the first value in the heap. To find the children of a node in a heap, you multiply the index of that node by two for the left child and add 1 for the right child. If the index of a node multiplied by two equals the length of the array, that means that the node does not have a child.

Lastly, we covered hashing. Hashing is based on the idea of distributing keys among a one-dimensional array called a hash table. To distribute the keys, you must use a hash function which will assign an index to the key. When two keys are hashed to the same index, this is called a collision. You can deal with collision by using separate chaining, which sorts the keys in linked lists and each list contains all the keys hashed to that index. You can also use linear probing to deal with collisions. Linear probing allows you to store the key in the next available index if the initial hashed index is taken.

Comments

Popular posts from this blog