CST-370 Week 6
This week, I learned about two different types of balanced trees and their associated algorithms: AVL Tree and 2-3 Tree. I also learned how to use the Heap data structure to efficiently find maximum and minimum values. Heap can be represented by the bottom-up algorithm. Finally, I learned to use Hashing for storing values in a dictionary/array to perform efficient search operations.
AVL Trees
Problem: Unbalanced trees result in operations that take O(n) time.
AVL Tree: A balanced binary search tree. The difference between the heights on the left and right subtrees is either -1, 0, or 1. (balance factor = height of left subtree - height of right subtree)
Ex: Since each node has a balance factor of -1, 0, or 1, it is an AVL tree.
AVL Tree Rotations
Rotation: A local transformation of a subtree whose balance factor has become either +2 or -2.
Four types of rotations: R(right)-rotation, L(left)-rotation, LR-rotation, RL- rotation
Ex:
2-3 Tree
A 2-3 tree is a search tree.
- Each node can have one or two values (2 children's nodes or 3-children's nodes)
- Height balanced: All leaves are at the same level.
- If there is a node with more than 2 values, promote the middle child, creating a new level.
Heap
Complete Binary Tree: A binary tree in which every level, except possibility the last, is completely filled, and all nodes in the last level are as far left as possible.
Heap: A binary tree with the following two conditions.
1. It should be a complete binary tree.
2. Max heap: The number of each node is >= to the number of its children.
or
Min heap: The number of each node is <= to the numbers of its children.
Ex for max heap:
Using Max Heap to find the maximum value, the root value will always be the maximum value. Found in O(1)
Max Heap Operations
Insert example:
Time complexity for inserting: O(logn)Remove max from heap:
1. Replace root with the last leaf
2. Heapify down (swapping with the larger child if the parent is smaller.
Heapsort - Basic idea
- Construct a max heap with a given list of n numbers
- Apply the delete operation of max numbers (n - 1) times.
-> The order of deleted numbers will be the sequence of numbers in sorted order
Overall time complexity: O(nlogn)
Heap Representation and Construction
Using an array (size of n+1) to represent a heap with n nodes:
- Store the heap in the array from index 1 to n.
- The root node is assigned index 1 of the array. All the following nodes are stored from index 2 to n
Parental nodes are in the first [n/2] positions of the array and leaf keys occupy the last [n/2] positions (nodes with no children). Children of a key in the position i are in positions (2*i) and (2*i+1). Parent of a key in position i will be in position [i/2]
Heap construction with n random input numbers:
Top-down heap construction: Add n numbers one by one
Hashing
Worst case for chaining: O(n)
Average: O(1)
Load Factor
n = number of keys, m = size of hash table.
Load factor = n/m
Load factor too high? May result in too many collisions and less efficiency in searching for a key.
Load factor too low? May not be an efficient use of space.
- Load factor increases as more elements/keys are added to a hash table.
Objective for rehashing:
- Reduce the load factor to be within a pre-defined load factor. i.e. if load factor exceeds this pre-defined load factor, trigger rehashing.
Rehashing: Increase the hash table's size to be the first prime number greater than twice the previous table size.



Comments
Post a Comment