1 / 54

# Chapter 12 - PowerPoint PPT Presentation

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

I am the owner, or an agent authorized to act on behalf of the owner, of the copyrighted work described.

Chapter 12

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.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.

- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -

## Chapter 12

Outline

• Hash Function

• Hash table

• 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

### Associative container

• Ordered associative containers

• Binary search tree

• Unordered associative containers

• Hash table

• Open probing

• Chaining with separate lists

### 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

### 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

22

### Design Hash functions

• the hash function is Easy to compute

• Minimize the collision

• Uniform distribution of keys over hash table

### 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

### 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)

### Hash function: Key is a string

• Suppose string is c0c1c2…cn-1

• Method 1:

• Method 2:

### Hash function: Key is not numbers or strings

• Design a custom hash function object type for the key

• Example

### 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

• 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

### Hash Table Using Open Probe Addressing

• insert an item

• Clustering:

• Find an item

• Remove an item

### 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

### Hash table performance

• Chaining:

• Unsuccessful search

• Successful search

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

### 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

### Comparing search algorithm

• Sequential search

• Binary search

• Binary search tree

• hashing

### Two Binary Search Tree Example

• 5, 15, 20, 3, 9, 7, 12, 17, 6, 75, 100, 18, 25, 35, 40

### 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.

Search 7, 30?

### Insertion

Top-down approach to slitting a 4-node:

split the 4-node first, then do insertion

C

### 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

### 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 )

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

### 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.

### 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.

• 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)

### 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.

### Insertion at the bottom of the tree

• Single (left) rotation

• Double (left) rotation

### 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

### Left child of a Black parent P

• Do color flip

### Right child of a Black parent P

Splitting a 4-node prior to inserting node 55

### 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

### 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

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

ignoring colors

Red-black tree after a single right-rotation about X and recoloring

### 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

35

### 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

### 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

### Summary Slide 3

§- Collision Resolution

- Two types:

- 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

### Summary Slide 4

§- Collision Resolution (Cont…)

- Two types:

- 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

### Summary Slide 5

§- Collision Resolution (Cont…)

2) chaining with separate lists.

- the hash table is a vector of list objects

- Each list is a sequence of colliding items.

- After applying the hash function to compute the table index, search the list for the data value.

- If it is found, update its value; otherwise, insert the value at the back of the list.

- you search only items that collided at the same table location

51

### Summary Slide 6

§- Collision Resolution (Cont…)

- there is no limitation on the number of values in the table, and deleting an item from the table involves only erasing it from its corresponding list

52

### Summary Slide 7

§- 2-3-4 tree

- a node has either 1 value and 2 children, 2 values and 3 children, or 3 values and 4 children

- construction of 2-3-4 trees is complex, so we build an equivalent binary tree known as a red-black tree

53

### Summary Slide 8

§- red-black trees

- Deleting a node from a red-black tree is rather difficult.

- the class rbtree, builds a red-black tree

54