0% found this document useful (0 votes)
6 views11 pages

Unit 3(ADS)

The document discusses Abstract Data Types (ADTs), including List, Stack, and Queue ADTs, emphasizing their implementation-independent nature and the operations that can be performed on them. It also covers Hash Tables, detailing their structure, the importance of hash functions, and collision resolution techniques such as separate chaining and open addressing. Additionally, it highlights the advantages and disadvantages of direct addressing, separate chaining, and linear probing in managing hash tables.

Uploaded by

navata
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views11 pages

Unit 3(ADS)

The document discusses Abstract Data Types (ADTs), including List, Stack, and Queue ADTs, emphasizing their implementation-independent nature and the operations that can be performed on them. It also covers Hash Tables, detailing their structure, the importance of hash functions, and collision resolution techniques such as separate chaining and open addressing. Additionally, it highlights the advantages and disadvantages of direct addressing, separate chaining, and linear probing in managing hash tables.

Uploaded by

navata
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 11

UNIT – 3(ADS) search with  for new topics

Abstract Data Types

Abstract Data type (ADT) is a type (or class) for objects whose behaviour is defined by a set of value and
a set of operations.
The definition of ADT only mentions what operations are to be performed but not how these operations
will be implemented. It does not specify how data will be organized in memory and what algorithms will
be used for implementing the operations. It is called “abstract” because it gives an implementation-
independent view. The process of providing only the essentials and hiding the details is known as
abstraction.

The user of data type does not need to know how that data type is implemented, for example, we have
been using Primitive values like int, float, char data types only with the knowledge that these data type
can operate and be performed on without any idea of how they are implemented. So a user only needs
to know what a data type can do, but not how it will be implemented. Think of ADT as a black box which
hides the inner structure and design of the data type. Now we’ll define three ADTs
namely List ADT, Stack ADT, Queue ADT.

List ADT
 The data is generally stored in key sequence in a list which has a head structure consisting
of count, pointers and address of compare function needed to compare the data in the list.

Page 1 of 11
 The data node contains the pointer to a data structure and a self-referential pointer which points
to the next node in the list.

//List ADT Type Definitions

typedef struct node

void *DataPtr;

struct node *link;

} Node;

typedef struct

int count;

Node *pos;

Node *head;

Node *rear;

Page 2 of 11
int (*compare) (void *argument1, void *argument2)

} LIST;

 The List ADT Functions is given below:

2. A list contains elements of the same type arranged in sequential order and following operations can
be performed on the list.
 get() – Return an element from the list at any given position.

Page 3 of 11
 insert() – Insert an element at any position of the list.
 remove() – Remove the first occurrence of any element from a non-empty list.
 removeAt() – Remove the element at a specified location from a non-empty list.
 replace() – Replace an element at any position by another element.
 size() – Return the number of elements in the list.
 isEmpty() – Return true if the list is empty, otherwise return false.
 isFull() – Return true if the list is full, otherwise return false.

Stack ADT
 In Stack ADT Implementation instead of data being stored in each node, the pointer to data is
stored.
 The program allocates memory for the data and address is passed to the stack ADT.

 The head node and the data nodes are encapsulated in the ADT. The calling function can only
see the pointer to the stack.
 The stack head structure also contains a pointer to top and count of number of entries currently
in stack.

//Stack ADT Type Definitions

typedef struct node

void *DataPtr;

Page 4 of 11
struct node *link;

} StackNode;

typedef struct

int count;

StackNode *top;

} STACK;

3. A Stack contains elements of the same type arranged in sequential order. All operations take place
at a single end that is top of the stack and following operations can be performed:
 push() – Insert an element at one end of the stack called top.
 pop() – Remove and return the element at the top of the stack, if it is not empty.
 peek() – Return the element at the top of the stack without removing it, if the stack is not
empty.
 size() – Return the number of elements in the stack.
 isEmpty() – Return true if the stack is empty, otherwise return false.
 isFull() – Return true if the stack is full, otherwise return false.

Queue ADT
 The queue abstract data type (ADT) follows the basic design of the stack abstract data type.

Page 5 of 11
 Each node contains a void pointer to the data and the link pointer to the next element in the
queue. The program’s responsibility is to allocate memory for storing the data.

//Queue ADT Type Definitions

typedef struct node

void *DataPtr;

struct node *next;

} QueueNode;

typedef struct

QueueNode *front;

QueueNode *rear;

int count;

} QUEUE;

4. A Queue contains elements of the same type arranged in sequential order. Operations take place at
both ends, insertion is done at the end and deletion is done at the front. Following operations can
be performed:
 enqueue() – Insert an element at the end of the queue.
 dequeue() – Remove and return the first element of the queue, if the queue is not empty.
 peek() – Return the element of the queue without removing it, if the queue is not empty.
 size() – Return the number of elements in the queue.
 isEmpty() – Return true if the queue is empty, otherwise return false.
 isFull() – Return true if the queue is full, otherwise return false.
From these definitions, we can clearly see that the definitions do not specify how these ADTs will be
represented and how the operations will be carried out. There can be different ways to implement an
ADT, for example, the List ADT can be implemented using arrays, or singly linked list or doubly linked list.
Similarly, stack ADT and Queue ADT can be implemented using arrays or linked lists.

Page 6 of 11
Hash Table Data Structure

A Hash table is a data structure that is used to store the data in key
key-value pairs.

 Each key in the hash table is mapped to a value that is present in the hash table.
 The key present in the hash table are used to index the values, as we take this key and pass it to
a hash function which in turn performs some arithmetic operation on it. The resulting
resulti value we
get after the operation is the index of the hash table where we store the key
key-value
value pairs in the
table.
 Internally a hash table contains buckets, and the location of which bucket to use for a particular
key is determined by the key's hash funct
function.

Consider the visual representation below:

Components of a Hash Table

A hash table comprises two components in total, these are:

Hash function

 A hash function is used to determine the index of the key


key-value pair.
 It is always recommended that we should choose a good hash function for creating a good hash
table.

Page 7 of 11
 Besides a good hash function, it should be a one
one-way
way function, i.e. we should be able to get the
hash value from the key and not vice versa.
 Also, it should avoid producing the same hash fo
for different keys.

Arrays:

 The array(buckets) are used to hold the key


key-value pairs.
 The size of the array should be set accordingly to the number of key
key-value
value pairs we will have.

Direct Addressing

It is a technique in which we make use of direct address tab


tables
les to map the values with their keys. It uses
the keys as indexes of the buckets and then store the values at those bucket locations. Though direct
addressing does facilitate fast searching, fast inserting and fast deletion operations, but at a cost.

Consider
ider the pictorial representation below:

Advantages of Direct Address Table:

 Insertion in O(1) time: Inserting an element in a direct address table is the same as inserting an
element in an array, hence we can do that in O(1) time as we already know the index(via key).
 Deletion in O(1) time: Deleting an element from a direct address table is the same as deleting
from an array, hence the O(1) time.
 Searching in O(1) time: Searching an element takes linear time
time(O(1)) as we can easily access an
element in an array in linear time if we already know the index of that element.

Page 8 of 11
Disadvantages of Direct Address Table:

 It is not recommended using the direct address table if the key values are very large.
 It cannot handle the case where two keys are equal and contain different data.

A good hash function:

A good hash function can mean many things at the same time, but we can conclude that a hash function
is considered as a good hash function, if:

 It minimizes the amount of collisions as much as possible.


 It should not generate the bucket locations that are larger than the hash table, in that case we
will be wasting too much space.
 The bucket locations generated should neither be too far apart and too much closer.

Separate chaining
In this technique, a linked list is created from the slot in which collision has occurred, after which the
new key is inserted into the linked list. This linked list of slots looks like a chain, so it is called separate
chaining. It is used more when we do not know how many keys to insert or delete.
Time complexity

1. Its worst-case complexity for searching is o(n).


2. Its worst-case complexity for deletion is o(n).

Advantages of separate chaining

1. It is easy to implement.
2. The hash table never fills full, so we can add more elements to the chain.
3. It is less sensitive to the function of the hashing.

Disadvantages of separate chaining

1. In this, cache performance of chaining is not good.


2. The memory wastage is too much in this method.
3. It requires more space for element links.

Open Addressing
Open addressing is collision-resolution method that is used to control the collision in the hashing table.
There is no key stored outside of the hash table. Therefore, the size of the hash table is always greater
than or equal to the number of keys. It is also called closed hashing.
The following techniques are used in open addressing:

Page 9 of 11
1. Linear probing
2. Double hashing

Linear probing

In this, when the collision occurs, we perform a linear probe for the next slot, and this probing is
performed until an empty slot is found. In linear probing, the worst time to search for an element is
O(table size). The linear probing gives the best performance of the cache but its problem is clustering.
The main advantage of this technique is that it can be easily calculated.
Disadvantages of linear probing

1. The main problem is clustering.


2. It takes too much time to find an empty slot.

Example
Suppose we have a list of size 20 (m = 20). We want to put some elements in linear probing fashion. The
elements are {96, 48, 63, 29, 87, 77, 48, 65, 69, 94, 61}

Hash Table

Page 10 of 11
Double hashing

In this, you use another hash function, and probe for (i * hash 2(x)) in the i th iteration. It takes longer to
determine two hash functions. The double probing gives the very poor the cache performance, but
there has no clustering problem in it.

Page 11 of 11

You might also like