k2
Collision Resolution by Chaining
m–1
U
(universe of keys)
K (actual keys)
k1 k2 k3 k5 k4 k6 k7 k8
k1 k4 k5 k6 k7 k3 k8
Collision Resolution by Chaining 0 U ( universe of keys) k 1 k 4 k - - PowerPoint PPT Presentation
Collision Resolution by Chaining 0 U ( universe of keys) k 1 k 4 k 1 k 4 K k 2 k 5 k 2 k 6 (actual k 6 k 5 keys) k 7 k 8 k 3 k 7 k 3 k 8 m 1 Expected Cost of an Search Theorem: An unsuccessful search takes expected time (1+). Proof
k2
m–1
U
(universe of keys)
K (actual keys)
k1 k2 k3 k5 k4 k6 k7 k8
k1 k4 k5 k6 k7 k3 k8
3
If we have enough contiguous memory to store all the keys (m >
n) ⇒ store the keys in the table itself
No need to use linked lists anymore Basic idea:
Insertion: if a slot is full, try another one, until you find an empty one
Search: follow the same sequence of probes
Deletion: more difficult, we will see later
Search time depends on the length of the probe sequence!
Let m = 17 and h(k) = k % 17.
Useful way to think about probing
Let h(k) be the hash function used When a collision occurs, try a different table cell.
Try in succession h0(x), h1(x), h2(x), ... hi(x) = (h(x) + f(i)) mod m, with f(0) = 0
i = 0, 1, 2, …, m-1
Function f is the collision resolution strategy.
With linear probing, f is a linear function of i,
CENG 213 Data Structures
As long as table is big enough, a free cell can
Worse, even if the table is relatively empty,
This effect is known as primary clustering. Any key that hashes into the cluster will require
Let m = 17 and h(k) = k % 17.
7
Worst-case get/put time is O(n), where n is the number of pairs in the table. This happens when all pairs are in the same cluster.
alpha = loading density = number of pairs/slots = n/m.
Sn = expected number of buckets examined in a successful search when n is
large
Un = expected number of buckets examined in a unsuccessful search when n
is large
Time to put governed by Un.
Sn ~ ½(1 + 1/(1 – alpha)) Un ~ ½(1 + 1/(1 – alpha)2) Note that 0 <= alpha <= 1
n
Performance requirements are given, determine maximum permissible loading
density.
We want a successful search to make no more than 10 compares (expected).
We want an unsuccessful search to make no more than 13 compares (expected).
So alpha <= min{18/19, 4/5} = 4/5.
Quadratic Probing eliminates primary clustering problem of linear
probing.
Collision function is quadratic. The popular choice is f(i) = i2. If the hash function evaluates to h and a search in cell h is
inconclusive, we try cells h + 12, h+22, … h + i2.
i.e. It examines cells 1,4,9 and so on away from the original probe. Remember that subsequent probe points are a quadratic
number of positions from the original probe point.
Let m = 17 and h(k) = k % 17. Put in pairs whose keys are 6, 12, 34, 29, 28, 11, 23, 7,
0, 33, 30, 45
Homework:
Find average length of probe if linear probe is used Perform quadratic probing with f(i) = i2
List the positions of all the keys
Find the average length of probe if quadratic probe is used
Dynamic resizing of table.
Linear lists are useful for serially ordered data. (e0, e1, e2, …, en-1) Days of week. Months in a year. Students in this class. Trees are useful for hierarchically ordered data. Employees of a corporation.
President, vice presidents, managers, and so on.
Java’s classes.
Object is at the top of the hierarchy. Subclasses of Object are next, and so on.
The element at the top of the hierarchy is the root. Elements next in the hierarchy are the children of
Elements next in the hierarchy are the and children
Elements that have no children are leaves.
A tree t is a finite nonempty set of elements. One of these elements is called the root. The remaining elements, if any, are
Some texts start level numbers at 0 rather than at 1. Root is at level 0. Its children are at level 1. The grand children of the root are at level 2. And so on. We shall number levels with the root at level 1.
Node Degree = Number Of Children
3 2 1 1 1
Finite (possibly empty) collection of elements. A nonempty binary tree has a root element. The remaining elements (if any) are partitioned into two
binary trees.
These are called the left and right subtrees of the binary tree.
No node in a binary tree may have a
A binary tree may be empty; a tree
The subtrees of a binary tree are ordered;
(a + b) * (c + d) + e – f/g*h + 3.25 Expressions comprise three kinds of entities.
Operators (+, -, /, *). Operands (a, b, c, d, e, f, g, h, 3.25, (a + b), (c + d), etc.). Delimiters ((, )).
Binary Tree Properties & Representation
Minimum number of nodes in a binary tree whose height is h. At least one node at each of first h levels.
All possible nodes at first h levels are present.
Let n be the number of nodes in a binary tree
h <= n <= 2h – 1 log2(n+1) <= h <= n
A full binary tree of a given height h has 2h – 1 nodes.
Number the nodes 1 through 2h – 1. Number by levels from top to bottom. Within a level number from left to right.