1 / 54

Chapter 12

Chapter 12. Advanced Associative Structure. Outline. Hash Function Hash table Open probe addressing Chaining with separate lists Hash Iterator Efficiency of Hash Methods 2-3-4 Tree Insertion of 2-3-4 tree Red-Black Trees Converting 2-3-4 tree to Red-Black tree

tamma
Download Presentation

Chapter 12

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Chapter 12 Advanced Associative Structure

  2. Outline • Hash Function • Hash table • Open probe addressing • Chaining with separate lists • Hash Iterator • Efficiency of Hash Methods • 2-3-4 Tree • Insertion of 2-3-4 tree • Red-Black Trees • Converting 2-3-4 tree to Red-Black tree • Four Situations in the Splitting of a 4-Node: • Building a Red-Black Tree • Red-Black Tree Representation

  3. Associative container • Ordered associative containers • Binary search tree • Unordered associative containers • Hash table • Open probing • Chaining with separate lists

  4. Hashing • Hash table • Store elements uniquely identified by their key • Hash function: take a key as an argument and returns an entry point to the table

  5. Using a hash function • hf: UN • U = set of all possible keys for the type of item • N is the integer set • Then take hf(Key)%m to get the hash table index • m is the table size (number of entries in the hash table) • Collision: hf(Key1) %m = hf(Key2) %m • different items map to the same hash table index

  6. Example Hash Function

  7. Example Hash Function 22

  8. Design Hash functions • the hash function is Easy to compute • Minimize the collision • Uniform distribution of keys over hash table

  9. Function objects • A function object is an object of a class that behaves like a function • Can be created, stored, and destroyed • Can have associated data members and operations

  10. Hash function: Key is an integer • Identity function • Use mod operation (mod m) to get hash table index • Collision • If m=10b, then collisions will occur for all keys that are the same in their rightmost b digits • If m=2b, then collisions will occur for all keys that are the same in their rightmost b bits (binary digits) • Solution: • choose m as a prime number • Make sure the table is large enough to reduce the probability of collisions • Choose m as the smallest prime number greater than m_min • Mix up the digits in key (eg. MidSquare technique)

  11. Hash function: Key is a string • Suppose string is c0c1c2…cn-1 • Method 1: • Method 2:

  12. Hash function: Key is not numbers or strings • Design a custom hash function object type for the key • Example

  13. Design Hash Tables • Two basic approaches to handling collision in hash table • Open addressing: when collision occurs, rehash to a different location • Separate chaining: colliding elements are stored on a list for each has value

  14. Open addressing • h: K × N -> M • K: Key set, N {0,1,2,…} : number of attempts, M {0,1, …, m-1} : the range of table size • h(k,i) is the hash function for key k on the i-th attempt, each attempt is called a “probe”. • Linear probe • Quadratic probe

  15. Hash Table Using Open Probe Addressing Example

  16. Hash Table Using Open Probe Addressing • insert an item • Clustering: • Find an item • Remove an item

  17. Chaining with separate lists • The hash table is defined as an indexed sequence of containers, such as vector or lists • Each container, called a bucket, holds a set of data items that has to the same table location

  18. Chaining with Separate Lists Example

  19. Hash table performance • Chaining: • Unsuccessful search • Successful search

  20. Hash table size = m, Number of elements in hash table = n, Load factor  = Average Probes for Successful Search Average Probes for Unsuccessful Search Open (linear) Probe Chaining  Efficiency of Hash Methods

  21. The hash class • Implementation of hashing by using chaining with separate lists • Stree class implements ordered sets and maps • Hash class implement unordered sets and maps • Applications

  22. Hash Iterator hIter Referencing Element 22 in Table ht

  23. Comparing search algorithm • Sequential search • Binary search • Binary search tree • hashing

  24. Binary Search Tree, Red-Black Tree and AVL Tree Example

  25. Two Binary Search Tree Example • 5, 15, 20, 3, 9, 7, 12, 17, 6, 75, 100, 18, 25, 35, 40

  26. 2-3-4 Tree Method • 2-3-4 tree: each node has two, three, or four links (children) and the depths of the left and right subtrees for each node are equal (perfectly balanced) • 2 node: a node containing a data value and pointers to two subtrees. • 3 node: a node containing two ordered data values A and B such that A < B, as well as three pointers to subtrees • 4 node: a node containing three ordered data values A < B<C, along with four pointers to subtrees.

  27. 2-3-4 Tree Example: Search item Search 7, 30?

  28. Insertion Top-down approach to slitting a 4-node: split the 4-node first, then do insertion C

  29. Example of Insertion of 2-3-4 Tree Insertion Sequence: 2, 15, 12, 4, 8, 10, 25, 35, 55, 11, 9, 5, 7 Insert 8

  30. Example of Insertion of 2-3-4 Tree (Cont…) Insertion Sequence: 2, 15, 12, 4, 8, 10, 25, 35, 55, 11, 9, 5, 7 (4,12,25 )

  31. Insert 7 Example of Insertion of 2-3-4 Tree (Cont…) Insertion Sequence: 2, 15, 12, 4, 8, 10, 25, 35, 55, 11, 9, 5, 7

  32. Running time for 2-3-4 Tree Operations • Time complexity • In a 2-3-4 tree with n elements, the maximum number of nodes visited during the search for an element is int(log2n)+1 • Inserting an elements into a 2-3-4 tree with n elements requires splitting no more than int(log2n)+1 4-nodes and normally requires fare fewer splits • Space complexity • Each node can have 3 values and 4 pointers to children. • Each node (except root) has a unique parent, tree has n-1 edges (pointer in use) • The number of unused pointers is 4n-(n-1)=3n+1.

  33. Red-Black Trees • A red-black tree is a binary search tree in which each node has the color attribute BLACK or RED. • It is designed as a representation of a 2-3-4 tree.

  34. Property 1: The root of a red-black tree is BLACK • Property 2: A RED parent never has a RED child-never two RED nodes in succession • Property 3: Every path from the root to an empty subtree has the same number of BLACK nodes, called black height of the tree (the level of 2-3-4 tree) Converting a 2-3-4 Tree to Red-Black Tree Example

  35. Inserting nodes in a Red-Black tree • Difficulty: must maintain the black height balance of the tree • Maintain the root as a BLACK node • Enter a new node into the tree as a RED node • Whenever the insertion results in two RED nodes in succession, rotate nodes to create a BLACK parent while maintaining balance • When scanning down a path to find the insertion location, split any 4-node.

  36. Insertion at the bottom of the tree • Single (left) rotation • Double (left) rotation

  37. Splitting of a 4-Node (subtree that has a black parent and two RED children) Four Situations: • The splitting of a 4-node begins with a color flip that reverse the color of each of the nodes • When the parent node P is BLACK, the color flip is sufficient to split the 4-node • When the parent node P is RED, the color filp is followed by rotations with possible color change

  38. Left child of a Black parent P • Do color flip

  39. Right child of a Black parent P Splitting a 4-node prior to inserting node 55

  40. Left-left ordering of G, P, and X • Oriented left-left from G (grandparent of the BLACK node X) • Color flip • Using A Single Right Rotation • Color change

  41. Left-right ordering of G, P, and X • Oriented Left-Right From G After the Color Flip • Color flip • Using A Double Rotation (single left-rotation, single right-rotation) • Color change

  42. G G D P X D X P X G C B D A B A B P C C A Left-right ordering of G, P, and X • Oriented Left-Right From G After the Color Flip • Color flip • Using A Double Rotation (single left-rotation, single right-rotation) • Color change Red-black tree after single Left-rotation about X, ignoring colors Red-black tree after a single right-rotation about X and recoloring

  43. Building A Red-Black Tree2, 15, 12, 4, 8, 10, 25, 35, 55, 11, 9, 5, 7

  44. Building A Red-Black Tree (Cont…) 2, 15, 12, 4, 8, 10, 25, 35, 55, 11, 9, 5, 7

  45. Erasing a Node in a Red-Black tree • More difficult to keep the property of a red-black tree • If the replacement node is RED, the BLACK height of the tree is not changes • If the replacement node is BLACK, make adjustments to the tree from the bottom up to maintain the balance

  46. rbnode Representation of Red-Black Tree 35

  47. Summary Slide 1 §- Hash Table - simulates the fastest searching technique, knowing the index of the required value in a vector and array and apply the index to access the value, by applying a hash function that converts the data to an integer - After obtaining an index by dividing the value from the hash function by the table size and taking the remainder, access the table. Normally, the number of elements in the table is much smaller than the number of distinct data values, so collisions occur. - To handle collisions, we must place a value that collides with an existing table element into the table in such a way that we can efficiently access it later. 47

  48. Summary Slide 2 §- Hash Table (Cont…) - average running time for a search of a hash table is O(1) - the worst case is O(n) 48

  49. Summary Slide 3 §- Collision Resolution - Two types: 1) linear open probe addressing - the table is a vector or array of static size - After using the hash function to compute a table index, look up the entry in the table. - If the values match, perform an update if necessary. - If the table entry is empty, insert the value in the table. 49

  50. Summary Slide 4 §- Collision Resolution (Cont…) - Two types: 1) linear open probe addressing - Otherwise, probe forward circularly, looking for a match or an empty table slot. - If the probe returns to the original starting point, the table is full. - you can search table items that hashed to different table locations. - Deleting an item difficult. 50

More Related