Presentation is loading. Please wait.

Presentation is loading. Please wait.

Binary Search Trees Implementing Balancing Operations Hash Tables

Similar presentations


Presentation on theme: "Binary Search Trees Implementing Balancing Operations Hash Tables"— Presentation transcript:

1 Binary Search Trees Implementing Balancing Operations Hash Tables
AVL Trees Red/Black Trees Hash Tables Reading: , Appendix E

2 Implementing Balancing Operations
Knowing rotations, we must know how to detect that the tree needs to be rebalanced and which way There are 2 ways for tree to become unbalanced By insertion of a node By deletion of a node There are two mechanisms for detecting if a rotation is needed and which rotation to perform: AVL Trees Red/Black Trees It is best for both to have a parent reference in each child node to backtrack up the tree easily

3 Implementing Balancing Operations
AVL trees (after Adel’son-Vel’ski and Landis) keep a balance factor attribute in each node that equals the height of the right sub-tree minus the height of the left sub-tree Each time an insertion or deletion occurs: The balance factors must be updated The balance of the tree must be checked from the point of change up to and including the root If a node’s balance factor is > +1 or < -1, the sub-tree with that node as root must be rebalanced

4 Implementing Balancing Operations
If the balance factor of a node is -2 If the balance factor of its left child is -1 or zero* Perform a right rotation If the balance factor of its left child is +1 Perform a leftright rotation If the balance factor of a node is +2 If the balance factor of its right child is +1 or zero* Perform a left rotation If the balance factor of its right child is -1 Perform a rightleft rotation Note: Error in L&C on page 315  zero is not mentioned, but zero can happen in some cases of remove operations

5 AVL Tree Right Rotation
Initial Tree After Add 1 Node is -2 Left Child is -1 7 (-1) 7 (-2) 5 (0) 9 (0) 5 (-1) 9 (0) 3 (0) 6 (0) 3 (-1) 6 (0) 1 (0)

6 AVL Tree Right Rotation
After Right Rotation 5 (0) 3 (-1) 7 (0) 1 (0) 6 (0) 9 (0)

7 AVL Tree Rightleft Rotation
Initial Tree After Remove 3 10 (1) 10 (2) Node is +2 Right Child is -1 5 (-1) 15 (-1) 5 (0) 15 (-1) Remove 3 (0) 13 (-1) 17 (0) 13 (-1) 17 (0) 11 (0) 11 (0)

8 AVL Tree Rightleft Rotation
After Right Rotation After Left Rotation 10 (2) 13 (0) 5 (0) 13 (1) 10 (0) 15 (1) 11 (0) 15 (1) 5 (0) 11 (0) 17 (0) 17 (0)

9 AVL Tree Right Rotation
Initial Tree After Remove 9 Node is -2 Left Child is zero! 7 (-1) 7 (-2) 5 (0) 9 (0) 5 (0) 3 (0) 6 (0) 3 (0) 6 (0) Note: This is the case that the L&C text misses in its discussion and examples 9

10 AVL Tree Right Rotation
After Right Rotation 5 (+1) 3 (0) 7 (-1) 6 (0) Note: This is the case that the L&C text misses in its discussion and examples 10

11 Red/Black Trees Red/Black Trees (developed by Bayer and extended by Guibas and Sedgewick) keep a “color” red or black for each node in the tree This approach is used in the Java class library binary search tree classes The maximum height of a Red/Black tree is roughly 2*log n (not as well controlled as an AVL tree), but the height is still O(log n) The rules for insert, delete, and rebalance are more complicated than our textbook section covers, so we won’t study this technique

12 Hash Tables Binary search trees can be used for both sorting in O(N log N) time and searching in O(log N) time However, if you have an application that requires only searching but not sorting, a hash table is a data structure that provides O(1) performance for searching A hash table optimizes time performance at the expense of using more memory

13 Hash Tables Some programming languages include direct language support for hash tables - such as PERL (hashes) and Python (dictionaries) Java does not - hash tables are implemented in library classes such as HashMap<K,V> or you can write a class yourself if necessary You should understand how to implement a hash table in any language such as C where there is no language or library support

14 Hash Tables The base of a hash table can be an array
A hash function operates on the search key to produce an integer index into the array All data elements whose hash of their keys result in the same index are stored starting at that position in the array Essentially, a hash table is structured like a comb with a long “spine” and short “teeth” Only a few elements start at the same index

15 Hash Tables In Java, one reasonable way to implement a hash table is with a parameterized class that encapsulates an array of ArrayList<K> objects ArrayList<K> hashTable = new ArrayList<K>(); Hash Value hashTable ArrayList(s) for key K1 as an index to one ArrayList . . . K

16 Hash Tables To add an element, the add method:
Hashes the key to get an index into the table Instantiates an ArrayList (if needed) Adds the key object to that ArrayList To find an element, the find method: If there is no ArrayList there, it returns not found It searches the ArrayList to find the Key If the Key is not there, it returns not found

17 Hash Tables The hash table lookup is faster because a search quickly finds the correct ArrayList which is short for searching relative to N If as you add elements to the Hash Table the ArrayLists get too large, you need to expand the capacity of the array as we did previously for stacks and queues and then rehash every key into the new larger array with new ArrayLists

18 Hash Functions The hashing function is critical for random distribution of the key elements For example, if your keys are telephone numbers in Massachusetts, using the first 3 digits (the area codes) would be a poor choice because there are only a few area codes in use in the state (“clustering”) The last 4 digits would be a better choice, but is still not ideal because the array size must be 10,000 (not a prime number)

19 Hash Functions The mathematics don’t work well unless the array size is a prime number (P) A better choice would be to select a prime number for the size of the array and use the whole phone number modulo the array size to get an array index from 0 to P-1 If you need to expand the array capacity, you can’t just double the size of the array because that would not be a prime number

20 Hash Functions Implementing a good hash function is not easy
There is a hashCode() method in the Object class that is the parent class of all classes Although it is not considered to generate the best hash codes, you should probably use it if you don’t know how to generate a good one yourself


Download ppt "Binary Search Trees Implementing Balancing Operations Hash Tables"

Similar presentations


Ads by Google