0% found this document useful (0 votes)
13 views

Unit-2-Linear-List-Skip-List

Uploaded by

hjjhjkijnjkk
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views

Unit-2-Linear-List-Skip-List

Uploaded by

hjjhjkijnjkk
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 18

lOMoARc PSD|18 878400

www.android.universityupdates.in | www.universityupdates.in | https://telegram.me/jntuh

UNIT II

UNIT - II
Dictionaries: linear list representation, skip list representation, operations -
insertion, deletionand searching.
Hash Table Representation: hash functions, collision resolution-separate
chaining, open addressing linear probing, quadratic probing, double hashing,
rehashing, extendible hashing.

DICTIONARIES:

Dictionary is a collection of pairs of key and value where every value is associated
with thecorresponding key.

Basic operations that can be performed on dictionary are:

1. Insertion of value in the dictionary

2. Deletion of particular value from dictionary

3. Searching of a specific value with the help of key

A dictionary is a general-purpose data structure for storing a group of objects.

• A dictionary has a set of keys and each key has a single associated value.
• When presented with a key the dictionary will return the associated value.
• A dictionary is also called a hash, a map, a hashmap in different programming languages.
• The keys in a dictionary must be simple types (such as integers or strings) while the
valuescan be of any type.
• Different languages enforce different type restrictions on keys and values in a dictionary.
• Dictionaries are often implemented as hash tables.
• Keys in a dictionary must be unique an attempt to create a duplicate key will
typicallyoverwrite the existing value for that key.
• Dictionary is an abstract data structure that supports the following

operations: –search(K key) (returns the value associated with the given

key)

insert(K key, V value) – delete(K key)

• Each element stored in a dictionary is identified by a key of type K.

• Dictionary represents a mapping from keys to values.

Dictionaries have numerous applications.

– contact book • key: name of person; value: – telephone number

--table of program variable identiers • key: identier; value: address in memory


www.android.previousquestionpapers.com | www.previousquestionpapers.com | https://telegram.me/jntuh
lOMoARc PSD|18 878400

www.android.universityupdates.in | www.universityupdates.in | https://telegram.me/jntuh

UNIT II

www.android.previousquestionpapers.com | www.previousquestionpapers.com | https://telegram.me/jntuh


lOMoARc PSD|18 878400

www.android.universityupdates.in | www.universityupdates.in | https://telegram.me/jntuh

UNIT II

– property-value collection • key: property name; value: associated value –

--natural language dictionary • key: word in language X; value: word in language Y – etc

operations on dictionaries

Dictionaries typically support several operations:

– retrieve a value (depending on language, attempting to retrieve a missing key may give
adefault value or throw an exception)

– insert or update a value (typically, if the key does not exist in the dictionary, the key-value
pairis inserted; if the key already exists, its corresponding value is overwritten with the new
one)

– remove a key-value pair

– test for existence of a key

Note that items in a dictionary are unordered, so loops over dictionaries will return
items in anarbitrary order.

Linear List Representation The dictionary can be represented as a linear list. The
linear list is acollection of pair and value. There are two method of representing linear
list.

Structure of linear list for dictionary:

To Represent the dictionary with linear list , each node contain the 3 fields : those are
key ,value and pointer to the next node .

The below is the node structure :

Key value Pointer to the next


node

Example :

Struct node
{
Int key;
Int
value;
Struct node *next;
};
www.android.previousquestionpapers.com | www.previousquestionpapers.com | https://telegram.me/jntuh
lOMoARc PSD|18 878400

www.android.universityupdates.in | www.universityupdates.in | https://telegram.me/jntuh

UNIT II
struct node *head;

www.android.previousquestionpapers.com | www.previousquestionpapers.com | https://telegram.me/jntuh


lOMoARc PSD|18 878400

www.android.universityupdates.in | www.universityupdates.in | https://telegram.me/jntuh

UNIT II

Insertion of new node in Dictionary

Consider that initially dictionary is empty then head = NULL


We will create a new node with some key and value contained in it.

Now as head is NULL, this new node becomes head. Hence the dictionary contains only
one record. this node will be ‘curr’ and ‘prev’ as well. The ‘cuur’ node will always point to
current visiting node and ‘prev’ will always point to the node previous to ‘curr’ node. As
now there is only one node in the list mark as ‘curr’ node as ‘prev’ node
Insert a record, key=4 and value=20,

Compare the key value of ‘curr’ and ‘New’ node. If New->key > Curr->key then attach
New node to ‘curr’ node.

Add a new node then

If we insert then we have to search for it proper position by comparing key value. (curr-
>key < New->key) is false. Hence else part will get executed.

www.android.previousquestionpapers.com | www.previousquestionpapers.com | https://telegram.me/jntuh


lOMoARc PSD|18 878400

www.android.universityupdates.in | www.universityupdates.in | https://telegram.me/jntuh

UNIT II

The delete operation:

Case 1: Initially assign ‘head’ node as ‘curr’ node.Then ask for a key value of the node
which is to be deleted. Then starting from head node key value of each jode is cked
and compared with the desired node’s key value. We will get node which is to be deleted
in variable ‘curr’. The node given by variable ‘prev’ keeps track of previous node of ‘cuu’
node. For eg, delete node with keyvalue 4 then

Case 2: If the node to be deleted is head node i.e.. if(curr==head) Then, simply
make ‘head’node as next node and delete ‘curr’

Hence the listbecomes

SKIP LIST REPRESENTATION

A skip list is a data structure that is used for storing a sorted list of items with a help of
hierarchy of linked lists that connect increasingly sparse subsequences of the items. A
skip list allows the process of item look up in efficient manner. The skip list data
structure skips over many of the items of the full list in one step, that’s why it is known
as skip list.

Skip list is a variant list for the linked list. Skip lists are made up of a series of nodes
connected one after the other. Each node contains a key and value pair as well as one or
more references, or

www.android.previousquestionpapers.com | www.previousquestionpapers.com | https://telegram.me/jntuh


lOMoARc PSD|18 878400

www.android.universityupdates.in | www.universityupdates.in | https://telegram.me/jntuh

UNIT II

www.android.previousquestionpapers.com | www.previousquestionpapers.com | https://telegram.me/jntuh


lOMoARc PSD|18 878400

www.android.universityupdates.in | www.universityupdates.in | https://telegram.me/jntuh

UNIT II

pointers, to nodes further along in the list. The number of references each node contains
is determined randomly. This gives skip lists their probabilistic nature, and the number
of references a node contains is called its node level. There are two special nodes in the
skip list one is head node which is the starting node of the list and tail node is the last
node of the list.

The skip list is an efficient implementation of dictionary using sorted chain. This is
because in skip list each node consists of forward references of more than one node at a
time.

Skip list structure

It is built in two layers: The lowest layer and Top layer.

The lowest layer of the skip list is a common sorted linked list, and the top layers of the
skip list are like an "express line" where the elements are skipped.

You can see in the example that 47 does not exist in the express line, so you search for a
node of less than 47, which is 40. Now, you go to the normal line with the help of 40,
and search the 47, as shown in the diagram.

Skip List Basic Operations

There are the following types of operations in the skip list.

o Insertion operation: It is used to add a new node to a particular location in a


specificsituation.

o Deletion operation: It is used to delete a node in a specific situation.

o Search Operation: The search operation is used to search a particular node in a skip list

www.android.previousquestionpapers.com | www.previousquestionpapers.com | https://telegram.me/jntuh


lOMoARc PSD|18 878400

www.android.universityupdates.in | www.universityupdates.in | https://telegram.me/jntuh

UNIT II

Example 1: Create a skip list, we want to insert these following keys in the empty skip list.
1. 6 with level 1.
2. 29 with level 1.
3. 22 with level 4.
4. 9 with level 3.
5. 17 with level 1.
6. 4 with level 2.
Ans:

Step 1: Insert 6 with level 1

Step 2: Insert 29 with level 1

www.android.previousquestionpapers.com | www.previousquestionpapers.com | https://telegram.me/jntuh


lOMoARc PSD|18 878400

www.android.universityupdates.in | www.universityupdates.in | https://telegram.me/jntuh

UNIT II

www.android.previousquestionpapers.com | www.previousquestionpapers.com | https://telegram.me/jntuh


lOMoARc PSD|18 878400

www.android.universityupdates.in | www.universityupdates.in | https://telegram.me/jntuh

UNIT II

Step 3: Insert 22 with level 4


Step 4: Insert 9 with level 3

Step 5: Insert 17 with level 1

www.android.previousquestionpapers.com | www.previousquestionpapers.com | https://telegram.me/jntuh


lOMoARc PSD|18 878400

www.android.universityupdates.in | www.universityupdates.in | https://telegram.me/jntuh

UNIT II

Step 6: Insert 4 with level 2

Example 2: Consider this example where we want to search for key 17.

www.android.previousquestionpapers.com | www.previousquestionpapers.com | https://telegram.me/jntuh


lOMoARc PSD|18 878400

www.android.universityupdates.in | www.universityupdates.in | https://telegram.me/jntuh

UNIT II

www.android.previousquestionpapers.com | www.previousquestionpapers.com | https://telegram.me/jntuh


lOMoARc PSD|18 878400

www.android.universityupdates.in | www.universityupdates.in | https://telegram.me/jntuh

UNIT II

Ans:

INSERTION IN SKIP LIST

We will start from highest level in the list and compare key of next node of the current
node withthe key to be inserted. Basic idea is If –
1. Key of next node is less than key to be inserted then we keep on moving forward on
thesame level
2. Key of next node is greater than the key to be inserted then we store the pointer to
currentnode and move one level down and continue our search.
At the level 0, we will definitely find a position to insert given
key.Consider this example where we want to insert key 17 –

www.android.previousquestionpapers.com | www.previousquestionpapers.com | https://telegram.me/jntuh


lOMoARc PSD|18 878400

www.android.universityupdates.in | www.universityupdates.in | https://telegram.me/jntuh

UNIT II

SEARCH IN SKIP LIST

Searching an element is very similar to approach for searching a spot for inserting an
elementin Skip list. The basic idea is if –
1. Key of next node is less than search key then we keep on moving forward on the
samelevel.
2. Key of next node is greater than the key to be inserted then we store the pointer
tocurrent node and move one level down and continue our search.

At the lowest level (0), if the element next to the rightmost element has key equal to
the searchkey, then we have found key otherwise failure.

EXAMPLE:

Consider this example where we want to search for key 17-

www.android.previousquestionpapers.com | www.previousquestionpapers.com | https://telegram.me/jntuh


lOMoARc PSD|18 878400

www.android.universityupdates.in | www.universityupdates.in | https://telegram.me/jntuh

UNIT II

10

www.android.previousquestionpapers.com | www.previousquestionpapers.com | https://telegram.me/jntuh


lOMoARc PSD|18 878400

www.android.universityupdates.in | www.universityupdates.in | https://telegram.me/jntuh

UNIT II

Deleting an element from the Skip list

Deletion of an element k is preceded by locating element in the Skip list using above
mentioned search algorithm. Once the element is located, rearrangement of pointers is
done to remove element form list just like we do in singly linked list. We start from
lowest level and do rearrangement until element next is not k.
After deletion of element there could be levels with no elements, so we will remove these
levels as well by decrementing the level of Skip list.

Consider this example where we want to delete element 6 –

Here at level 3, there is no element (arrow in red) after deleting element 6. So we will
decrement level of skip list by 1.

11
www.android.previousquestionpapers.com | www.previousquestionpapers.com | https://telegram.me/jntuh
lOMoARc PSD|18 878400

www.android.universityupdates.in | www.universityupdates.in | https://telegram.me/jntuh

UNIT II

Advantages of the Skip list

1. If you want to insert a new node in the skip list, then it will insert the node very
fastbecause there are no rotations in the skip list.
2. The skip list is simple to implement as compared to the hash table and the binary
searchtree.
3. It is very simple to find a node in the list because it stores the nodes in sorted form.
4. The skip list algorithm can be modified very easily in a more specific structure, such as
indexable skip lists, trees, or priority queues.
5. The skip list is a robust and reliable list.

Disadvantages of the Skip list

1. It requires more memory than the balanced tree.


2. Reverse searching is not allowed.
3. The skip list searches the node much slower than the linked list.

Applications of the Skip list

1. It is used in distributed applications, and it represents the pointers and system in


thedistributed applications.
2. It is used to implement a dynamic elastic concurrent queue with low lock contention.
3. The indexing of the skip list is used in running median problems.
4. The skip list is used for the delta-encoding posting in the Lucene search.

www.android.previousquestionpapers.com | www.previousquestionpapers.com | https://telegram.me/jntuh

You might also like