0 ratings0% found this document useful (0 votes) 100 views24 pagesData Structures
data structures chapter two
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content,
claim it here.
Available Formats
Download as PDF or read online on Scribd
Dictionaries and Hash Table
Representation
. SyYttasus
Dictionaries: Linear List Representation, Skip List Representation, Operations - Insertion,
Deletion and Searching,
Hash Table Representation: Hash Functions, Collision Resolution - Separate Chaining, Open
Addressing - Linear Probing, Quadratic Probing, Double Hashing, Rehashing, Extendible
Hashing.
(EEARNING OBJECTIVES |
Introduction to Dictionaries
Skip List and its Representation
Various Operations Performed on Skip List
Concept of Ideal Hashing
Various Hash Functions
Various Collision Resolution Techniques
SOAS ES SK OS
Concept of Extensible Hashing
[A dictionary consists of « group of keys and their associated values. Its of the form (K, V) where,
'K! denotes the key and ‘V’ denotes the value of the key ‘K’. These keys are unique. Various
operations performed on dictionaries are find, Insert and erase. A skip list is a probabl
data structure for storing an ordered list of items. It was developed by William Pugh in 1990.
It is based on a collection of linked lists. :
Hashing is 0 technique that makes use of « hash function for mapping pairs to the corresponding
(entries) positions in a hash table. Ideally a pair ‘P' with a key ‘K’ is stored at a position f(K)
by the hash function “". Each hash table position can store one pair, Hash function is @ function
that is-used to map the dictionary pairs to the corresponding entries. in the hash table. The
various Hashing functions include Division method, Mid square method, Folding Method and
Digit Analysis.42
2.1 DicTiONARIES
Q1. Explain briefly about dictionaries with an
example.
Answer :
Dictionaries
A dictionary consists of a group of keys and
their associated values. It is of the form (KV) where,
*K denotes the key and ‘17 denotes the value of the key
“K”. These keys are unique.
‘Example 1
+The “company list” for the data structure “job”
is a dictionary that consists ofa group of “employe
Who are working in that company. When a new employee
is appointed, a record for that employee is added to the
dictionary. Similarly, when an employee leaves the
company, his/her record gets deleted from the diction-
ary. During the employment period, a manager may
query the dictionary to get the record associated with a
particular employee and can make changes in the record.
‘The employee-id (eid) is considered as the key’and the
other details of an eniployee is the value of that key.
A dictionary with duplicates is a dictionary that
allows the same Key to be'used by two or more (key,
value) pairs. The operations that can be performed on
dictionary with duplicates include find, insert and erase.
Example 2
A telephone directory. The ambiguities in ‘find”
and ‘erase’ operations ina dictionary with duplicates can
be eliminated by,
(Finding a pair that contains the specified key or
Gi)__By finding all the pai
specified key. °
For the ‘erase? operation, a user must decide
which pair to delete from among all the pairs and can
either delete that pair or can delete all the pairs that are
associated with that specified key. 4
The ‘find’ operation finds and retrieves the pairs
randomly when the key is provided. Some applications
makes use of sequential access to retrieve pages one-b:
one in increasing order of their
Q2. What are the operations that can be per=
formed on a dictionary? How a dictionary
is specified in a ADT?
Answer :
Operations
@ Adding pair to a dictionary,
that are associated with a
(Deleting the pair associated with a specified key
from the dietionary.-
ii) Finding the pair that is associated with a given key.
iv) Finding the number of pairs ie. size of adictionary.
(v) Determining whether the dictionary is empty or ful
DATA STRUCTURES [JNTU-HYDER, Aan)
Dictionary in ADT
Dictionary in ADT is specified as,
Abstract Data Type Dictionary
{
Instances: a
‘group of pairs with unique keys for each pair,
Operations .
insent(p) : Insert the pair ‘p"to the dictionary;
erase(k) : Delete the pair wth key ‘K’ from the dictionary,
find(k) _: Finds and returns the pair with key k;
ize()- : Finds the size of a dictionary;
empty(): Returns true if the dictionary is empty ely
returns false;
} if
The key and its associated value in'a pair ‘p* i,
denoted as ‘p-first’ and‘
Q3.__ How to perform reassign operation ona
dictionary?
Answer : (Model Paper-tl, @4(a) | AprivMay-23(R18), a4)
The reassign operation on a dictionary is per-
formed when the existing value need to be modified
which is associated with a key. Alternately, a new value
can be mapped to a kéy which is already present in a
dictionary.
Syntax: Modify(c, ¢)
Here, element ‘e’ is replaced/modified with its
locator ‘c*.
For remaining answer refer Unit-ll, Page No. 42,
a it Representation
Q4. Discuss the two classes that are used for
the linear list representation method.
Answer : :
The two classes that are used for the linear list
representation includes,”
1. Sorted Array List Class .
The sorted array list class makes use of the ar
ray representation of a linear list. This class follows the
binary search technique. So, the find ‘operation requires
the time of O(log n) for a. tionary consisting of n-paits.
(@) Insertion: A pair is inserted into a dictionary only
after ensuring that none of the already existing
Pairs in a dictionary contains the same key 25
inserted pair. Hence, the total time taken to inseft
a pair in a dictionary is the sum of,
‘Time for searching (find) + Time for inserting
= Ollog n) + O(n)
Deletion: A pair is deleted from the dictionary
after searching it in the dictionary. Hence, the
{otal time required for deletion is given as,
Time for searching + Time for deleting
=> O(log n) + O(n)
(byjonaries and Hash 3
UNIT: Table Representation
Femento) 1 21 B11 13) (6 01
[eT Tso To [0
Bement) = 10
Eke} = 39
Ekement[2} = $0
Ekrmeni{3} = 99
Elemen(a] = 99
Blemeni[5] = empty
Ekmeni(
e
em = ery
2 Sorted Chain Class .
‘The sorted chain class uses a linked I i
l list representation method. In this class, the nodes are ta
The cost of finding the pairs
nereasing order of their keys is O(1) per pair.
Example for Sorted Chain/Linked List Representation "
Kor vate Key Vat Key Vale Key Vabe Key Vahe
= 3 [0 [C4 [0 Ts Te Trl
2.1.2 Skip List Representation
Q5. What is skip list? How it is different from a linear linked list?
OR
What is a skip list? 7
Answer : . ApriliMay-24R18), Q1(c)
Skip List
A skip list is a probabilistic data structure for ‘storing an ordered list of items. It was developed by William
Pugh in 1990. Itis based on a collection of linked lists. These linked lists connect increasingly sparse subsequences
of items as shown in the figure.
Sm)
4 ‘ull
3 5 i)
Figure: Skip List Example
‘The above list is called skip list because it skips certain items in the list. The last list in the skip list structure
is a regular ordered linked list without any clement being skipped. The primary drawback associated with linked
list structure is that, they need sequential scanning in order to locate a searched element.
Ina linear linked list, the search process initiates from the start of the list and terminates only when the item
being searched is found or when the complete lis is covered. The search process of linear linked list can be speed-
up by ordering the items in the list. However, sequential scanning is still required. The skip list on the other hand is
a special type of ordered linked list that allows non-sequential scanning of elements. In this list, the second element
links to the succeeding node that is two positions ahead, the fourth element links to the succeeding node that is four
positions ahead, and so on. Whereas, in the linear linked list, each element links only to its adjacent node.
Q6. Explain about the representation of skip list structure and skipList class.
Answer : ° .
Skip List Structure * .
_ Ina skip list structure, the header node requires specified number of pointer fields at each level while the
tail node does not require any pointer field as each node consists, of an element field and the sufficient pointer
fields (i.e., level number = 1).
Element | Pointer
Node} feid | “fields
Figure: Skip List Structure
SPECTRUM ALL-IN-ONE JOURNAL FOR ENGINEERING STUDENTSWhere,
[Number of pointer fields = Level number +1
Hence, astruct SkipNode is used that satisfies all
the requirements of the nodes which is given as,
template
struct SkipNode
‘
typedefPair < const K, E > pairType;
pairType
SkipNode ** n;
SkipNode(const pairType& P, int s)
eP)
{n= new SkipNode * (S];}
5
Where; The struct “SkipNode”
¢=Element, ; n=Next element
p= Pair 3 S= Size.
The pointer fields are denoted by the. airay n
and the constructor allocates the memory space for
the “n’ array of structures and keeps the pair into ‘e’.
When the constructor is called, the size is given as,
Size=Lev+1 for each ‘lev’ pair.
Gy
SkipList Class
class SkipList
t
private:
Data_members;
public:
constrictor;
find(const K& key);
insert(const pair & pair);
erase(const K& key);
B z
Data Members
The data members of the SkipList class are as
follows.
@ _~ SkipNode * headerNode
It specifies the pointer to the header node.
(i) SkipNode * tailNode
It specifies the pointer to the tail node.
(ili) SkipNode < K, E> ** Last
Itrefers to the last node.
Ifthe subscript ‘is mentioned with last as Last{/]
then it denotes the last node of level ‘i*,
Gv) int levels
‘It denotes the maximum number of non-émpty
chains currently existing in the skip list,
@) int dsize
It represents the total number of < key, value >
pairs in the dictionary, ;
3 book
)
DATA STRUCTURES [JNTU-HYDERABAD,
Int maxlevel
It specifies the maximum number of levels thay
are allowed in a particular chain.
(vil) float cutort
It is used in deciding the number of the levels,
(vill) K tallKey
Itrefers to a large key.
class SkipList
{
private:
‘SkipNode < K, E> * headerNode;
SkipNode < K, E> * tailNode;
SkipNode ** Last;
int levels, dsize, maxlevel;
float cutOf;
K tailkey;
ho. fi
Constructor
The constructor of the SkipList class takes three
parameters as,
@
iy
Larger Key :
This is of type ‘K’. It is greater than any key in
the dictionary. It is used in the tailNode.
Maxpairs 5
It is of type ‘int’,and denotes the maximum
number of pairs, a dictionary can have. Hence,
the maxlevel is,
maxlevel=[logyp maxpairs}-1
Prob
Itis of type ‘float’ and denotes the probability that
pair is present in both the level i and the level
4-1 chain.
The constructor initializes the SkipList class data
members as,
tailkey
maxlevel = (int) cell(logt ((float) maxpairs/
logf{I/prob)) ~ 1);
headerNode = new skipNode (pair tailpair, maxlevel + 1);
tailNode = new skipNode (pair
tailpair, Q);
Last = new skipNode *(maxlevel + 1);
Where, [Link] = tailkey;
This constructor sets aside the space for the
“tail’ and the ‘header’ nodes. The array ‘last’ refers t0
the last node in each chain during the search operation.
The skiplist contains ‘maxlevel + 1° Pointers from the
“header’ to the ‘tail’ node. The constructors complexity
is O(maxlevel),
CRIMINAL act. Anyone found guilty is LIABLE to face LEGAL proceedings.UNIT-2: Dictionaries and Hash Taby
lo Representati
G7. Explain in detail about skip ate ha =
in data
on structure
For answer refer Unit, Page No, 43
(Model Papers, 4b) | Maren-21(R18), 08)
8. Describe how the key
Nn of a di
awe a dictionary.
ApritMay-23(R 10), 44a)
‘The number of comparisons can be dccrease tol 1
@ Maintaining a pointer to the middle pair.
Gi) By comparing the pair to be sea
Gi). For a smatter key p;
) for an n-pair dictionary by,
irched with the middle pair.
Search only the left half portion of the sorted chain.
Search only the right half portion of the sorted ehain.
parisons can be reduced,
(iv) Fora larger key pai
Hence, the number of com
Example
Consider the sorted chain consisting of 9-pai
tail node and associated keys wit shown as Recent
ith cach node,
figure. This sorted chain has a header node, a
shown as the numbers in the figure.
As there are nine pairs, a search involves 9 key comparisons, Thi
to S by keeping a pointer to the middle node. pepe: Tile ees
of comparisons can be reduced
. =
PPS ay S-f1-PEP Sh
Header Node
‘Middle Node Tail Node
In order to search for a key, first it must be compared with the middle pair key. Ifits valuc is less than the
riddle pair key value then the search operation is performed only on the left side portion of the middle pair. Else,
ifvalue is greater than the middle pair key value then search is performed on the right hand side of the sorted chain
ic. if a pair with a key 37 is to be searched, then 37 is compared with the middle key value 50 as 37 < 50, only
the left side is examined. Similarly, for a pair with a key value 75 can be obtained by examining only the right side
Portion of the sorted chain thereby reducing the number of comparisons.
+ ____ These comparisons can even be reduced by further maintaining two separate pointers at the two sides of the
middle pair.
For ‘n’ nodes, the level 0 includes all the node pairs, the level 1 includes every second pair and the level 2
includes every fourth pair and so on i.e, the level j includes every 2 #* pair.
and Searchi
2:13" Skip List Of 1 and|Searching|
Q9. Explain the insertion and searching operations of skip list with an example.
Answer ;
ion operation of a skip list is an extension to its search operation. It makes use of randomization tech-
nique Sereda a eta! of references to the new item that mist be added to the skip list. First, the skip
search(x) operation is performed for adding the new item (x, ¢) into skip list. This operation results in a position
*pos' ofthe last-level item whose largest key is less than or equal to item x. The new item, e) is then inserted into
the skip list immediately after the position pos. A function rand{ ) is invoked ora coin i tossed after inserting the
new item which returns a random number between 0 and 1. If this number retumed is below 0.5 then, at
to be “heads and backtrack to the higher level for inserting the item. If the number is above 0,5, consider it to
tails and stop the processes. The insertion process is repeated until tail is obtained.
“The tower for the new item (x, €) is constructed by linking all its references together. The’ algorithm for
insertion operation of a skip list is given below.
‘SPECTRUM ALL-IN-ONE JOURNAL FOR ENGINEERING STUDENTSipinsert(s. ©)
pon item at lowest leve}
‘Algorithm ae
Input: item(, €) jon that points 10 *
Output: position
pos — skipSearch(x)
pos] « insertAferABOVE(POS:
& position(pos!)
while rand( )< 172 do .
while above(pos)= null do yscanni
pos before(pos) .
pos < above(pos)
pos] — insertAfter/
inpositi
null, (x, €))
ing backward
‘Above(pos, posl. (6) Inserting item
return pos? 3
Example: Insertion of new item into the skip list
* a
Ly = 7
I —
Lb 1 15 45
I 1 I
Lb |= 7 1s 21 45
I == I I ili
L [2 7 15 |] 21 28 MM 45 tro
Tesi ot Co 1 ;
wheHe H7 Hel sH2He Hers H 45 i
“To insen the key 32 into the list, a coin is tossed to get the insertion position. The tossed coin re
consecutive heads followed by a tail. So, the skip list will be as follows,
Le te
1 . fe]
Ly |= 7 um
oT I __ am
bl= 7 15 22 45 te
I I I
»[= 7 1s 20 32 45 !
plea eal a) at I =
2S}
T Cc r
uo EHH He Helse Lee
‘The positions holding the newly inserted item
aes item are shaded while th ns prec
ith thick borders. The expected running time for the above insertion algorithms Sloane deserts
en
Q10. Explain the deletion o}
oe peration of skip list with an example.
Deletion Operation of a Skip List
c wccessed by a a
Sees cea ; by applying an above( ) method for moving up in th
i time for deleting an item from the skip let xExample
bk =
ui 7 _L
I to
lL, |— 7 iis] _L,
I I fa
-— Hp :
Ly |= 2 I I
tk Hf a1 Hos a elfen
I i I L
Lp |=} 2 7
LEACH 10 Has Har fos LH He Hs LH Hs LH
Some improvements associated with skp list
(There is no need for storing pointers to items at
ee Pointers to items at
ii) There is no need s
oS down oi to make use of above( ) and before) method because insertion and deletionsean be made
in top-down and scan ina forward way thereby saving the memory tized By "uP and pre’ pont
insertion, the deletion of an item is made asthe search moves down the skip list. The changed
top-down structure employed by deletion operation elimi e ee
structure are,
ferent levels of skip list because pointers to keys will be
ies te
—_ -
La [= 7 pe
i I ;
Lb 7 “S
t i I I
a id a5}
T I i i
ule247 34 45] [+=
Lo L I I i
Lo |= 2 a |_{ 34 LJ 40 | 45 tee
To delete 15 from the skip list, firstly search for 15. The last pointer found on the chain is at level 2 with node 7.
Thus, it is necessary to change the connection between the nodes from level 0 to level 3. At level 0, con-
neci node 10 to 21. At levels 1 and 2 connect node 7 to 21 and at level 3, connect the node 7 with the node 32. By
changing these connections, the node 15 will be removed.
Q11. Explain the algorithms of skip list operations.
OR
Write an algorithm of skip list operations insertion and deletion.
(epee OF TopIESY AIROF Yor IBERIORA ROP POR DeTetION)
Answer :
The algorithms for variou:
@ Algorithm for Searching
The algorithm for searching operation in s
Algorithm SkipSeareh(L, key)
Begin
p=L{header] 2
for(i = level; i <=0;i-~)
while(p = key —> forw[i] forwf
P=p— forw[0)
if (p + key = key)
Retum p> value
else
return
End
2, Algorithm for Insertion
The algorithm for insertion operation in skip list
is as follows,
Algorithm SkipInsertion(L, key, item)
Begin
P= Lfheader);
for(int i = level; i> -~)
while(p = key -» forw[i] < key)
P=p- forv{i]
ili] =p
P=p forv{o)
if (p > key = key)
‘Update p[value] = item
else
IM = random _level()
ifQ1> level)
for (i= level +1
ip[i] = Lfheader]
level = IvI;
P=new snode (Ivl, key, value)
forfint i
P— forw[i] = ip{i] > forw{i]
ipli] > forwfi
End
3. Algorithm for Deletion
The algorithm for deletion operation in skip list
is as follows,
Algorithm SkipDeletion(L, key)
Begin
p=Lfheader]
forfint i= level; i> = 0; i--)
while(p = key > forw{i] < key)
P=p— forw(i]
ipli]=p
P=p- forw([0]
if (Pp — key = key)
for(int i = 0; i 1&&L|
== NULL)
level --
<=lvh iH)
[header] + forw[level]
End
DATA STRUCTURES [INTU-HYDERAGay
Q12. Explain the operations of the ski
representation.
(Model Papers, a) Marcha, ayy,
OR
P ling
Describe the operations of skip list
an example.
Answer : Pee=t90R 18, 9,
For answer refer Unit-II, Page Nos. 45, 46, 29, QI9,
ABLE REPRESEy
"ATION es
Q13. Explain ideal hashing with an example,
Answer :
Hashing
with
A dictionary can even be represented by using ,
method called “hashing”.
Hashing is a technique that makes use ofa hash
function for mapping pairs to the corresponding (entries)
positions in a hash table. Ideally a pair *P” with a key
°K’ is stored at a position (K) by the hash funetion
Each hash table position can store one pair.
Inorder to search a pair with a key *K’, first)
is calculated to determine the existence of a pair at a
position (K) in the hash table. If a desired pair is found
then it can be deleted, if required, else if, it does not ex-
ists in a table then it can be inserted at a position f(A)
Example: Consider a dictionary consisting of ‘employ-
ce’ records. If employce id (eid) is used as-a key, then
for atmost 100 employees, the eid’s ranges from 301000
and 302000. The hash function (K) = K — 301000 maps
the eid’s to the table positions 0 to 1000. Table [1001]
can be used to store dictionary pair pointers. The array
table[#] is NULL for 0
int div_method(int a, int b)
{retum a%%b+1; }
void main)
\_val=div_method(a,b)//Calling hash function
Printf("The value of 26 is stored at index
‘%d" hash_val);
getchQ;
3
Output
i ‘count_dig(long int key)
imte=1;
while(key !=0) {
key /= 10;
» ed retum e;
i Set_Nth_dig(long int n, int og
long int dig=0,frac=0, len;
= count digi),
ot Gindpow(to, (len-
Gig frac % 10; ety loc);
DATA STRUCTURES [JNTU-HYDERA Bp,
num= num
num= num + dig: |
jretum num;
i
int hash_midsq(long int key,int $7)
t
int hashval= 0;
int keysq_len=0; *
int mid_loc=0;
long int keysq = key*hey;
keysq_len = count_digikeysa);
mid_loc= keysq_len /2;
hashval= get_dig_rangelkeysq,(mid_locs
Dezy,
return hashval;
j
int main()
clrser();
printf(“nThe middle digits of a key 5420 based
digit address = “A” hash_midsq(5420,1)),
printf(*\n\nThe middle digits of a key 5420 based g
2 digits address = ud" hash_midsq(3420.2) 9”
Printf(“1n The middle digits of a key 5420 based ¢
3 digits address = %d"hash_midsq(5420,3),,
getch(); return 0; :
ont+ Dictionarios and Hash Ta
xpiain tho algorithm for imptomenting
quadratic probing on a hash table,
(REPTOAY TOTS QUIIRTTE TIO,
answer Aprninbay 24R1R), 08
cottston Resolatlon
Collisions are said to occur when a new key is
uahest tam autess hy using the hashing methods that
tar provate one-t0-00 Mapping, There ie a nce for
anating these collision. Por this purpose, there are many
Waals that are independent of the hashing algorithm
uNt
used
aypes of Collision Resolution
The various types of collision resolution
techniques ate as follows,
1, Open addressing (or) Closed hashing
Separate chaining (or) Linked list (oe) Open hashing,
Open Addressing (or) Closed Hashing
Open addressing ean be defined as one of the
methods used for resolving the collisions, This process
ofeallision resolution takes place in an area called as the
prime area that consists of the home addresses, Whenever
collision takes place, the prime area addresses will be
scanned until open (or) vacant element is found. When
‘an unoccupied element is found the new data will be
placed in that position. Open addressing consists of four
methods,
@ Linear Probing
In linear probing method of hashing, fis a linear
function of i, typically fli) = i. Linear probing is a
simple open addressing strategy for collision handling,
In this method, the cells are sequentially searched to
find an empty cell.
Example: Assume that,
keys: 16,23, 43, 18, 34, 59, 30,22
Size of Hash Table (HT) = 10
Then, modular arithmetic function will be,
Hx) = x% (size of HT) = x% 10
Initially, the Hash Table is empty.
1
or 2 3 4 5 6 78D
Inserting 16: For inserting she first key *16" into thi
table, compute h(x).
W(x) = x% 10 = 16% 10=
So, the key 6 is inserted into 7"
16
ar, 4.5 67 8 9
So, the key 23 will be inserted into 3” pos
23 16
OT a8 4 5, en Seret OF
53
4196 10
Collision will occur if 43 is placed at 3" position
pecause 21 is already present there in that position, So the
next cell will be searched after J". which is 4" position.
Thus the hey 43 is inserted in 4 position,
nf 16
. 1.2) 4 Ss 6 7 8D
Inserting 18: A(A) = 18%6 10= 8
J at 8* position.
Thus, the key 18 will be plac
as 16 18
o 1 2 ’ 4s 67 8
0% 104
Inserting 34: hk
Ifthe key 44 is placed at 4* position, the collision
will occur. $0, the next available cell is searched after 4
which is 5, Thus 34 will be inserted into S* position.
nf of [6 18
.o 123s # 5 6 7 8 9
Inserting $9: h(k) = 59% 109
Thus, the key 59 will be inserted
a | 9 [34 [16
OeUInaraierro nae eon Onn?
erting 30: /(A)= 30% 10 = 0
‘Thus, the key 30 will be placed at 0 position.
30 a | 3 [34] 6 18 59,
0 7 8 9
Insert 5
at 2" position.
30 2 fe [as] io 18 [59
(OM inate 2 rs Sura ers GIL 7b OE)
‘The implementation of linear probing is as
follows,
element* LinearProbing_Search(int key)
{
int curr_bkt, hm_bkt=hm();
for(curr_bkt =hm_bkt; ht{curr_bkt] &&
hifeurr_bkt] ey;)
(
cuirr_bkt = (curr_bkt + 1) %p;
itourr_bkt == hm_bkt)
retum NULL;
)
if{ht{curr_bkt] > k = = key)
return hifeurr_bkt};
return NULI
}
‘SPECTRUM ALL-IN-ONE JOURNAL FOR ENGINEERING STUDENTS.D
= But quadratic probing ‘guarantees of inserting =
“That is atleast half empty if the
(i) Quadratic Probing,
Quadratic probing overcomes the clusteritt
problem of linear probing. This is a collision resolution
‘on function is quadratic. The
method where the col
quadric probing function is express
method, when the collision occurs,
in-a cell which is one cell away from 1
ed as fli) = ?. In this
the key is inserted
he collided cell
Algorithm
The algorithm that is used fo
quadratic probing on a hash table is given below.
‘StepI: Start
Step2: Initially read the key value (i.c., Key) or element
to be inserted.
Step3: Assume j = 0.
‘Step: Compute hash key, Le., Mkey) = key % HT_size.
‘StepS: Then compute index of the hash table where an
element to be inserted i.e., Index = (hkey +j *
D% HT_size.
Step6: If clement is not present at that index then,
insert the value at that index and go to step-9.
Step7: Else, if element is already present at that index
then, increment the value of §' (i.e.,j=5+ 0).
Step8: Ifj
=
| a] _®
oe
a
Hash Table with Double Hashing/Rehashing After
Inserting {89, 18, 49, 58, 69}
pe
WARNING: Xerox/Photocopying of this book is a CRIMINAL act. Anyone found guilty is LIABLE to face LEGAL proceedings-Tre roeriing 4
te ecolved by evaluating the #49. This
_stion TE oe iserted in el ee)
ne inserting $8. I is resolved by hash es
oS go $8 is inserted in position 3. Next whe
asertd in position Oat distance of hash (60)
cotG= Laway: There are some hod cases if insert y
= js itoa series of collisions. Fr example if ion
seating 60m psitonOthenitreits ina ee
fie Hijisions at cells 3, 6, 9, since hash,(60) = Taek,
ed until an empty spot is found, =
‘The hash table size in the above example i
coe. Buttisdesinbletohave the able sneeeee
ee pot then it is possible to run out of altemative
vations prematurely. For example, if user try to inset
Spove table it collides with $8. Since hash,(23)
x
3 into a
= § and table size is 10 (even) then user have
Satyone alternative location which is already occupied.
if double hashing is implemented correctly, then
ge expected number of probes in double hashing is,
Soest same as fora random collision resolution strategy.
jrewever quadratic probing is simple and fast because it
oss not require a second hash function.
Random Probing
‘The random probing method determines the next
_qrsilable slot using a random number generator. It uses
hs formula, hash code = (hf(x) + ran#) % size of HT
Here, ran# is a random number. All insertion and
searches uses the same sequence of random numbers.
Example: Consider that the keys 36, 25, $5 are to be
imered in the hash table. Also assume that ran‘ 1 = 3,
. Therefore, the probe sequence
wo
For 55: 55 +3 =58, 55+
‘Advantages of Open Addressing
(@ There is no need for a separate data structure for
storing the items because they are directly stored
in the hash table.
(i Open addressing proves to be more efficient,
interms of storage.
Disadvantages of Open Addressing
() It is mandatory to have distinct
objects to be hashed.
(i) Table size must be chosen care!
does not work well.
keys for the
fully otherwise, it
state flag (full,
(ii) Each cell must contain a three~
‘empty or deleted).
2. Separate Chaining (or) Linked List (or) Open
Hashing
In this type of hashing, the hash table stort a
set of pointers (rather than data elements) -ach pointer
Points to a separate linked list. The data elements TS
“SPECTRUM ‘ALL-IN-ONE J
55
ich element
‘stored in the nodes of these linked I
“hash funetion (hf (k)) in applied, which gives the hash
code (index) of a particular bucket (cell) in the hash
table. The element is stored in the linked list, whic
assogiated with that bucket (cell).
Algorithm
Algorithm Separate Chaining
Input -
The key(K) of element to be searched. A flag
called INSERT to perform insertion.
Output
Return the location of element which is to be
searched or return NULL
‘Step1: Find the hash address of key using hash function
LashFunetion(key)
Step2: Assign pointer P to any node in List, P= H,{1]
Step3: Assign flag with false for controlling the search
Flag =F
‘Step4: While(P —- NULL) and (Flag = F) do
Step5: If{ P+ DATA = key) then
Step6: Flag =T
Step7: Return(P)
Step8: Exit
Step9: Go to else
‘Step10: Move pointers to next node,
Step: End If
Step12: End while
Stepl3: Ififlag = F) then
‘Step14: Printf “The key is not found”
Step15: If{[INSERT_Value) then
‘Step16: Insert_End_SL[1]
Step17: End If
‘Step18: End If
Stepl
Example
Keys to be inserted (3417, 3132, 7122, 5199,
5344, 6796, 1898}
Hash function hf{x) = x mod 10.
| Computing the hash values for each of the given
input using the hash function.
nf{3417) 7 (7th bucket)
1nf(3132) = 3132 mod 10 = 2 2nd bucket)
7122 mod 102 (2nd bucket)
(collision occurred)
5199 mod 10 =9 (9th bucket)
f(5344) 5344 mod 10 =4 (4th bucket)
6796 mod 10 = 6 (6th bucket)
1898 mod 10 = 8 (8th bucket)
= PLINK
Stop.
OURNAL FOR ENGINEERING STUDENTS%
Advantages of Separate Chaining
©) ies sz=ple and efficient collision resolution.
@)
Gay
(0 The keys of the elements to be hashed may or
may toh be unique,
Disadvantages of Separate Chaining
G) A separate data structure for chains must be
Answer: Medel Papers, O20) | Sep.2i¢ns8), 2)
Given bash table size ~ 13 .
List of elements are 65, 34, 79, 114, 26, 85, 55,
09,22; 9%,
Insert 65°
Jn Quadratic probing hash function,
HO) = (+f) mod table size
= (65 +) mod 13
= 65 mod 13 =0
i. 3-456 7 8.9 uit
t
WARMING: Kerox{Phatocopying of this book fz a CRIMINA ‘Anyone found guilty is LIABLE to fm
DATA STRUCTURES [JNTU-HYp;
Insert 34
HG)= (G4 + 0°) mod 13 = 34 mod 13 = g
As & position is empty insert the element 3g
Insert 79
HG) = (79 + 0°) mod 13
79 mod 13=1
As I" position is empty, insert the element 79,
(elsT TT TTT Pty
Oo 12345678 9G
T
Insert 114
H(x)= (114 + 0) mod 13
= 114 mod 13 = 10
As 10° position is empty, insert the element
14,
ssli7 T I I 34] TsT J ]
o1
23456789 0 np
tr
Aso
value ie, 1
H(@)= (26+
27 mod 13 =1
As 1* position is al
7 value ie, 2.
A(z) = (26 + 22) mod 13
= (26 +4) mod 13
=30 mod 13= 4
As 4* position is empty,
65] 79 26 I
o 12s
Position is not empty, increment the‘
so not empty, increment the
insert the element 26.
Bal Tay 1)
$567 8 on
Insert 85
HG) = (85 + 02) mod 13
Position is empty, insert the element 85.
26] T [stsa] [ay Td
10 2
Ome 2ia
Insert $9
HG) = (89 + 0°) mod 13 = 89 mog
As 11® position is empty a
sin
65 | 79 .
a ar 34 ia ps9
Insert 22 sae 8 9 0 WW
H(x)= (22 + 0°) mod ‘
13 = 22 mod 13-9
AS 9° position is empty, insert the clement 23
6S 79
~ : ~ x 85 | 34 | 22 [114] 89
2 5s 6 7 8 9 1 Wh 12
Insert 98 ‘
HQ) = 8 + 0°) mod 13°
=98 mod 13=7
As 7* position is not empty, increment the ‘/' value i.e, |
H(&)= 98 + 1) mod 13 a
=99 mod 13=8
As 8* position is not empty, increment the ‘/’ value i.c,, 2.
H()= (98 +22) mod 13
= 102 mod 13 = 11
As 11® position is also not empty, increment the */° value i.
(x)= (98 + 37) mod 13
= [Link] 13 =3
As 3" position is not empty, increment the ‘j’ valuz i.e,
(x) = (98 + 4) mod 13
= 114 mod 13=10
As 10* position is also not empty, increment the ‘/ value i.e., 5.
(x)= (98 + 5?) mod 13
= 123 mod 13=6 * , '
‘As 6" position is empty, insert the element 98.
‘The resultant hash table is, .
65 | 79 | 55 | 26 os [85 | 34 | 22 [14] 89
5 2
ya SGC
o 1 2 § ;
22. Write C programs to implement different collision resolution techniques. *
Answer = r plemented by using the following programs,
‘ ate: i
“The collision resolution techniques can Pe
1. Open Addressing
Program :
include
#inelude
‘int HT. 7 <
Susie; __cye journal
SPECTRUM LtDATA STRUCTURES [JNTU-HYDERAp,
$$ Aly
So haa :
ss HT (int key) a ar
int x; '
: . rintf(*inThe keys of th ae BIVEN belay,
Sa aga “ rie for (x-0:x2HIT size;xt++){
) : print(\nThe key at index % is %dx typ,
int rehash_inear(int key) _//Liniear Probing Jbreak ;
r cas
intx; ) printf(“Enter size of hash table: ");
x= (key+I)%HT_size; . scant (“¥%6d",&HT_size)s
return x for (x=0:x :
#include
#define HT_size 8
struct node
{
-int info;
struct node*next;
hb
struct node *head[HT_siz.
void insert()
{
struct node*nnode;
int key;
(NULL},*chain;
Printf(“Enter value of a ash table: ");
scanf(“%d",&key),
Jrkey%HT_size;
nnode=(struct node *)malloc(sizeof(struet node));
y=key;
hnode->next
NULL;
While(chain->next!= NULL) ,
{chain=chain->next; }
Void search)
{
int [Link];
Printf(“\nEnter value to search:
Seanfl"%d" &key);
ind=key%HT_size;
Miheadlind] = NULL)
Printf(“Element not found!\n");
else{
for(chain=headtind};chai
¢
‘NUL Lichain=chain->next)
if{chain>info= key){
Printf("Element found!
break;
,
}
if{chain-SNULL)
); -
Printf(“Element not found!\n");
H
void display(){
int
for=05",chain-info);
a»
printf"); :
}
void main()
t
intkey,jchs
clrser(); 7"
while(1) :
f :
i ni \
sintf(*\n~-Separate Chaining, Method~\n1 Insert
m2 Display genet ExitinEnter your choice: "),
scanf("%d" deh);
next)
= Awe IANDNG! EOD ENGmEcRINS enech(ch)
rchQ);
ak;
© Azexit(0):
‘ult:
ntf(“Enter Correct Choice!”);
tput
See Ret Met
eoess
Bye per)
oor
Sacha
ascites
Pome Ls ere
Seo eet ReR TN fii
fees
Siete
7
ere eee
eerie ese Ceaser
ares eng aires
emai ores
Brome ya me ts iL) elas 12a
eae nae
ete at ere eu ce
Tae a
Scere
AE DOSRarOTL Chu speed manlt0% cyles Famestp 0) Programe
iC
We ase
fs
ea Bae ae
ieee
separate Chaining Method —
Witeaas
ANE dE)
ioral
(irs
eateries Ties
Enter value ta search: 3132
Recaray cbs
.2 Extendible Hashing
Q23. Explain in detail about extendible
hashing. Model Paper, O42)
OR
List and explain the advantages of
extendible hashing.
(Refer Only Topic: Advantages)
Answer: March-22(R18), 4)
Extendible Hashing
Extendible hashing is a dynamic hashing
technique that is capable of’ dividing the buckets 25
database grows and rejoining them as it shrinks. This
requires reorganization of buckets in hash table. Aa
important feature of extendible hashing is that it reduces
the performance overhead of hashing because it performs
organization only on one bucket at a time.
In extendible hashing, the hash. function “
chosen such that it has sufficient qualities of uniformity
and randomness. And it is capable of generating valves
over a lafge range, often known as b-bit binary inteset
“The value of 6 is usually 32. Therefore, it can create 2
i.e., more than 4 billion buckets. However, all buckets 2
not created at once, instead they are created on dem:
as records are inserted into hash table.
tae tye Pes big Neg ee te tee eaov Bucket |
1»... [oS
Ne bene
see Bucket 2
pa :
Bocket Address Bucket S
Table
CAL
Bucket ‘n°
top of bucket address table. This value indicates that ‘d’ bits of hash
value ix) are needed to find the appropriate bucket for item ‘x
Note that, in figure, each bucket has an integer value associated with it, labelled asd, dd,
value specifies the length of common hash prefix that is present in several consecutive table
the same bucket. The number of entries that point to a bucket ‘nis given by 3-4),
Example
entries which point to
Consider an example to insert some search key values into memory using extendible hashing. In this, a hash
function h(x) = x mod 8 is considered for insertion.
Given hash function,
Hx) = x mod 8
Search-key values = 2, 3, 5, 7, 11, 17, 19, 23, 29, 31
Size of bucket = 3
Stept
‘Compute the hash values for each of the given search key value,
‘h(2) = 2 mod 8 = 2
43) =3 mod 8 = 3
A(S) = 5 mod 8 = 5
‘A(7) = 7 mod 8=7
(11) = 11 mod 8= 3
WIT) = 17 mod 8 = 1
(19) = 19 mod 8 =3
4(23) = 23 mod 8 =7
(29) = 29 mod 8 = 5
G1) = 31 mod 8 =7
SPECTRUM ALL-IN-ONE JOURNAL FOR ENGINEERING STUDENTS62
Step2
Compute the
resultant hash valu
HQ)=2=010
inary values for each of the
WQ)= 3-01
WS) =5=101
W)=7= 11 :
W)=3=011
W7)=1= 001
19) 11
nQ3)=7= 11
(29) = 5 = 101
W431) u
Step3
Compute the global depth value and place it on
the top of the bucket address table consisting of 8 slots
numbered (000, 001, 010, 011, 100, 101, 110, 111)
Global depth = 3 (+° The size of the directory = 3]
Step4
Insert the key values in accordance to their
binary values. Here, the number of buckets used are S
because five different hash values are generated,
1,2,3,5,7.
Now, consider the insertion ofthe search key
value 2.
Since, the computed binary value of 2 is 010, the
appropriate bucket (ie., bucket 1) is searched. Since, the
bucket is empty, the data entry 2 can be inserted. After
inserting, a pointer is added from 010 address in the bucket
address table to bucket 1.
In the similar fashion, the remaining data entry
values are inserted.
Steps
Verify whether any of the address slots are empty.
If they are empty, then pointer needs to be inserted from
the address slots to the respective buckets. This can be
done by comparing the first two MSB's of the’ empty
address slot with the first two MSB's of the succeeding
address slot. If both the MSB’s matches then only a
pointer is added to the bucket.
Steps
Compute the local depth for each of the bucket.
This is done by taking into account the number of
pointers attached to every bucket. Ifa bucket is assigned.
two pointers, then the MSB’s of this bucket is compared
with the MSB’s of the next bucket. If the MSBs of both
buckets binary address matches, then the local depth is
assigned the value depending on the number of bits that,
‘are matching. On the other hand, if'a bucket is assigned
only one pointer, then the local depth value of that bucket
is equal to the global depth value.
ir
IT scary
ma
= oO
7
4 EJ Petes
ovo a
3
ou TT} Becker
1m 4
-—__5
101
}29 | Buckets
0 :
Mm j=_==x_"4
| Bek
Bucket addressable Z| toca
Figure: Extendible Hash Structure
Advantages
° The advantages of extendible hashing are as
follows,
1 Variable Number of Buckets: In extendible
hashing, buckets are divided when database
increases. However, when data size decreases,
buckets are rejoined or shrinked.
2. Minimum Number of Buckets: As bucket grows
and shrinks, memory is utilized properly without
getting wasted.
3. Increase in file size does not degrade the perfor-
mance,
4. In terms of computation, data retrieval is less
expensive
5. _ Itean access desired records immediately.
Disadvantages é
The disadvantages of extendible hashing are as.
follows,
1. The records are not accessed sequentially.
2. The data may be erased unexpectedly, thereby
requiring special precautions,RY SHOR
ve T QUESTIONS WITH SOLUTIONS (VSQS) /
dictionary consists ofa grouy
‘a group of keys
of Keys and their associated values. His ofthe Form (K. V) where, “K" denotes
ey and 17 denotes the value of the key "A" These keys af
These keys are unique.
we
Gz. Define skip list,
answer =
skip list is a probabilisti
_Askip list is a probabilistic data structure for storing an ordered yy William
List of items. It was developed by
sh in, 1990. itis based on a collection of linked lists
3. Define hash function.
Answe!
Model Papers, 21(¢)
the hash
Hash function isa fi is
a finetion that is used to map the dictionary pairs tothe corresponding entries in tl
sable.
Q4. _ Define hash table.
Answer =
Hash table isa data structure that is used to store dictionary pairs. It is of fixes
to apart of the dictionary pair called the key. The size of the hash table is re
ize which ranges from 0 to table size — 1
ise se Tipp avnblealed
Q5, List the operations in dictionaries.
Answer = Model Papers, 1(4)
“The operations in dictionaries are,
(Adding pair to a dictionary.
(i) Deleting the pair associated wi
ated with a given key.
ith a specified key from the dictionary.
Gi) Finding the pair that is assoc
(iv) _Determining whether the dictionary is empty of full.
Q6. Write about midsquare method.
Answer:
solving the middle digits
In midsquare meth
from the number. The num!
jtself and address is obtained by
multiplied by
ison the table size.
1od, the key is
ected usually depend:
ber of digits se?
Q7. Givean ‘example for digital analysis method.
Model Papers, 1)
sition 3 to 6 and reversing
2164 by selecting digits in po:
n should be used
Answer:
‘nd the same arrangement patte
Akey 7546123 is trans!
their order. For a given key set
consistently.
Q8. Define collision.
Answer :
fo the address
formed t
position in the key
some
cn two different keys want 10 0¢cUPY the same home bucket. As the
urs wh
‘an easily be handled.
‘situation that occ
Acollision is
ne pait, collision ©
bucket can have more than oFDATA STRUCTURES UNTUHYDERARAD,
PORTANT QUESTIONS
Eachus Srey about dictionaries with an eTaeTS Imvportant Qerton
wie Uninth Page So Qt - ethod.
— : Yor the near list representation m™
SS] Sisauss the Gwe classes that are used Important Question
aswe
anower meter Unt, Page Nn £2. C4
; from a linear linked list?
Lo Whats stip st? How itis different Important Question
amporant Question | ApriUMay-23(R18), 4a)
5 (Samad
CS Expizin the coerations of the skip list representation. easel)
(March 22{R18), O3(2) | Dec.-19(R18), 25)
ines)
Important Question
(important Question | March-22(R18), Q4{s))
Witte the dicorithm for calculating Hash function using division method.
Aaswer Important Question
For mswer refx Unie Il Page No. S1,Q18.
) ‘CG. Whet ere Gifierent methods of collision resolution in hashing? Explain in brief.
paswes = (Aertiay-24RAE}, OS | AprUMay-23(R18), Q1(d) | March-22(R18), Q3(b)
er, G2 De-6(8, Dees, ign |_O
For anewer teie Univll, Paze No. $2, Q20.
0. ieaert the folowing ist of elements into the hash table
Hash table is 12) 65, 24, 79, 114, 26,
using Qu
35, 55, 89, 22 oon! USING Quadratic probing (size of
(important Question | Sep.-21(R18), Q2)
a