- 133 Views
- Uploaded on
- Presentation posted in: General

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
- Four Situations in the Splitting of a 4-Node:
- Building a Red-Black Tree
- Red-Black Tree Representation

- Ordered associative containers
- Binary search tree

- Unordered associative containers
- Hash table
- Open probing
- Chaining with separate lists

- Hash table

- 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

- hf: UN
- 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

- the hash function is Easy to compute
- Minimize the collision
- Uniform distribution of keys over hash table

- 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

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

- choose m as a prime number

- Suppose string is c0c1c2…cn-1
- Method 1:
- Method 2:

- Design a custom hash function object type for the key
- Example

- 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
- Quadratic probe

- insert an item
- Clustering:

- Find an item
- Remove an item

- 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

- 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

- Implementation of hashing by using chaining with separate lists
- Stree class implements ordered sets and maps
- Hash class implement unordered sets and maps

- Applications

- Sequential search
- Binary search
- Binary search tree
- hashing

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

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

Top-down approach to slitting a 4-node:

split the 4-node first, then do insertion

C

Insertion Sequence: 2, 15, 12, 4, 8, 10, 25, 35, 55, 11, 9, 5, 7

Insert 8

Insertion Sequence: 2, 15, 12, 4, 8, 10, 25, 35, 55, 11, 9, 5, 7

(4,12,25 )

Insert 7

Insertion Sequence: 2, 15, 12, 4, 8, 10, 25, 35, 55, 11, 9, 5, 7

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

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

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

- Single (left) rotation
- Double (left) rotation

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

- Do color flip

Splitting a 4-node prior to inserting node 55

- Oriented left-left from G (grandparent of the BLACK node X)
- Color flip
- Using A Single Right Rotation
- Color change

- 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

- 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

- 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

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

§- Hash Table (Cont…)

- average running time for a search of a hash table is O(1)

- the worst case is O(n)

48

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

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

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

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

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

§- red-black trees

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

- the class rbtree, builds a red-black tree

54