Unit-2-Linear-List-Skip-List
Unit-2-Linear-List-Skip-List
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.
• 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)
UNIT II
UNIT II
--natural language dictionary • key: word in language X; value: word in language Y – etc
operations on dictionaries
– 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)
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.
To Represent the dictionary with linear list , each node contain the 3 fields : those are
key ,value and 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
UNIT II
struct node *head;
UNIT II
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.
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.
UNIT II
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’
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
UNIT II
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.
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.
o Search Operation: The search operation is used to search a particular node in a skip list
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:
UNIT II
UNIT II
UNIT II
Example 2: Consider this example where we want to search for key 17.
UNIT II
UNIT II
Ans:
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 –
UNIT II
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:
UNIT II
10
UNIT II
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.
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
UNIT II
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.