CST 370 - Module 6
Week 6,
This week's module covered more material on tree structures. We previously talked about binary search trees, however, a problem with that is that it can become unbalanced, making the operation take linear time. To solve this problem, we just make sure we keep the binary search tree balanced. To do this, we make use of an AVL Tree, where we keep track of the levels below each node. In short, (and probably with lack of detail) we keep the number of children to each node equal from one side to the other as much as possible, within a difference of 1. If any of the nodes are unbalanced, we rotate the node with the first violation with its children, either left or right.
Another way of keeping a tree balanced is by using a 2-3 tree. Using a 2-3 tree is different from your usual tree as each node can contain either 1 or 2 values, and either 2 or 3 children. You insert values in a binary fashion. However, if you insert a third value into a node, the value between moves to the parent node. The result of the 2-3 tree has all leaf nodes on the same level.
The next data structure that was introduced was the Max Heap. The max heap is a complete binary tree, where every level, except possible the last is completely filled, and all nodes in the last level are as far left as possible. In addition, for a max heap, the number of each node is greater than or equal to the numbers of its children. When inserting a number to the max heap, you insert the value to the left most leaf. Next, you compare its number with its parent. We want the parent to be larger than the child. So, if necessary, we swap the nodes. The swap keeps chaining if necessary. There are other operations discussed, however, that will be left for you to research.
Hashing and using a Hash Table is the final bit of material covered for the week. This is an interesting way of keeping data, as accessing a value can be done in constant time O (1). To access or insert an item, you use a hashing function to determine where in the table the value goes to. In our case, we used the value mod table size, where the table size is a prime number. However, because we are using the modulo operation, there can be scenarios where two different values can be assigned the same index, causing a collision. To solve this issue, we have two solutions. One solution is using separate chaining, where the hash table is made using linked lists, so if there is a collision, we simply have the value already located in the index, point to the value with the same index. The other solution is linear probing, where the hash table is made using an array, however, instead of assigning to values in the same index, we simply insert the conflicting value to the next free index.
Comments
Post a Comment