0% found this document useful (0 votes)
36 views112 pages

MergeResult 2024 04 06 04 46 13

Uploaded by

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

MergeResult 2024 04 06 04 46 13

Uploaded by

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

Module 1- Introduction

Data Structure

 Data is basically a fact or an entity and is used for calculation or manipulation.

 Data structure is a representation of logical relationship existing between individual


elements of data.

 Data Structure is a way of collecting and organizing data in such a way that we can
perform operations on these data in an effective way.

Ex: GPS, Escalators, Queue System, Feeds, Online ticket Booking .

Classification of data structures

Primitive data Structures

 Primitive data structures are the basic data structures that directly operate upon the machine
instructions.

 They are the fundamental data types which are supported by a programming language.

 Some basic data types are integer, real, character, and Boolean.
Non-primitive data Structures

 Non-primitive data structures are those data structures which are created using primitive data
structures.

 Examples of such data structures include linked lists, stacks, trees, and graphs.

 non-primitive data structures are further classified into

 Linear Data Structure

 If the elements of a data structure are stored in a linear or sequential order, then it is a
linear data structure.

 Linear data structures can be represented in memory in two different ways.

o One way is to have to a linear relationship between elements by means of


sequential memory locations.

o The other way is to have a linear relationship between elements by means


of links.

 Examples include arrays, linked lists, stacks, and queues.

 Non-linear Data Structure

 If the elements of a data structure are not stored in a sequential order, then it is a non-
linear data structure.

 This structure is mainly used to represent data containing a hierarchical relationship between
elements.

 Examples include trees and graphs.

Data structure Operations

 Creating - Creating a new record with some data.

 Traversing- It is used to access each data item exactly once so that it can be processed. This
accessing and processing sometimes called as visiting the record.

 Searching- It is used to find out the location of the data item if it exists in the given
collection of data items.
 Inserting- It is used to add a new data item in the given collection of data items.

 Deleting- It is used to delete an existing data item from the given collection of data items.

 Sorting- It is used to arrange the data items in some order i.e. in ascending or descending
order in case of numerical data and in dictionary order in case of alphanumeric data.

 Merging- It is used to combine the data items of two sorted files into single file in the sorted
form.

Review of Pointers

 A pointer is a variable that contains the memory location of another variable.

 These applications include:

o Pointers are used to pass information back and forth between functions.

o Pointers enable the programmers to return multiple data items from a function
via function arguments.

o Pointers provide an alternate way to access the individual elements of an


array.

o Pointers are used to pass arrays and strings as function arguments.

o Pointers are used to create complex data structures, such as trees, linked lists, linked
stacks, linked queues, and graphs.

o Pointers are used for the dynamic memory allocation of a variable.

Declaring Pointer variables

data_type *ptr_name;

Null Pointers

 A null pointer is a special pointer value that does not point to any valid memory address.

 To declare a null pointer, use the predefined constant NULL,

int *ptr = NULL;

 You can also initialize a pointer to null using the constant 0

*ptr; ptr = 0;

 A function that returns pointer values can return a null pointer when it is unable to perform
its task.
Generic Pointers

 A generic pointer is a pointer variable that has void as its data type.

 The void pointer, or the generic pointer, is a special type of pointer that can point to

Pointer to Pointers

 In C, we can also use pointers that point to pointers.

 The pointers in turn point to data or even to other pointers.

 To declare pointers to pointers, just add an asterisk * for each level of reference.

Dynamic Memory Allocation

The process of allocating memory to the variables during execution of the program or at run time
is known as dynamic memory allocation.

malloc ()

 malloc is declared in <stdlib.h>

 The above header file is included in any program that calls malloc.

 The malloc function reserves a block of memory of specified size and returns a pointer of
type void.

 syntax of malloc () is:

ptr =(cast-type*) malloc(byte-size);

 where ptr is a pointer of type cast-type. malloc () returns a pointer (of cast type) to an
area of memory with size byte-size.

 Example:

Arr =(int*) malloc(10*sizeof(int));

 dynamically allocates memory equivalent to 10 times the area of int bytes.

 The address of the first byte of memory allocated is assigned to the pointer arr of type int.
calloc ()

 It is used to request multiple blocks of storage each of the same size and then sets all
bytes to zero.

 calloc() is primarily used to allocate memory for arrays.

 Syntax:

ptr=(cast-type*) calloc (n, elem-size);

 The above statement allocates contiguous space for n blocks each of size elem-size bytes.

 Example:

 calloc () returns a pointer to the first byte of the allocated region.

free()

 Releasing the used space

 Dynamically allocated memory must be manually released using the free()

 Releasing unused memory is crucial for efficient memory management.

 The free() function takes a pointer as an argument and returns the memory block
pointed to by that pointer to the free list within the heap.
realloc ()

 Realloc () is a function that can be used to change the size of memory that was
previously allocated using calloc() or malloc().

 The function takes two arguments: a pointer to the memory to be resized and the new
size of the memory.

 Realloc () returns a pointer to the resized memory block, or NULL if the request fails.

 If realloc () was able to make the old block of memory bigger, it returns the same
pointer.

 Otherwise, realloc () allocates a new block of memory and copies the old data to it.
Difference between malloc() and calloc()

Malloc Calloc

The name malloc stands for memory allocation. The name calloc stands for contiguous allocation.

malloc () doesn’t initializes the allocated calloc () initializes the allocated memory to

memory. It contains garbage values zero

Since no initialization, time efficiency is More expensive because of zero fillings. But

greater than calloc () convenient than malloc

Syntax Syntax variable_name=(datatype*) calloc (n, size);

variable_name=(datatype*) malloc (sizeof( datatype));

Required no. of bytes to be allocated is Takes 2 arguments: nno. of blocks to be

specified as arguments. i.e., size in bytes allocated, sizeno. of bytes in each block

Arrays

 An array is a data structure consisting of a collection of (mainly of similar data types) elements (values or variables),
each identified by at least one array index or key.

Linear arrays

 Linear arrays are called one dimensional arrays which is referenced by one subscript.

 An array must be declared before being used.

 Declaring an array means specifying the following:

 Data_type—the kind of values it can store, for example, int, char, float, double.

 Name—to identify the array.

 Size—the maximum number of values that the array can hold.


 syntax:

Data_type Name [Size];

 Example:

int marks [10];

 The above statement declares an array mark that contains 10 elements.

 In C, the array index starts from zero. This means that the array marks will contain 10
elements in all.

 The first element will be stored in marks [0], second element in marks[1], so on and so
forth. Therefore, the last element, that is the 10th element, will be stored in marks[9].

Calculating the address of array Elements

 The address of other data elements can simply be calculated using the base address. The
formula to perform this calculation is, Address of data element

A[k] = BA(A) + w (k – lower_bound)

 Here, A is the array, k is the index of the element of which we have to calculate the address,
BA is the base address of the array A, and w is the size of one element in memory, for
example, size of int is 4.

 Ex: Given an array int marks [] = {99,67,78,56,88,90,34,85}, calculate the address of marks [4] if the
base address = 1000.

marks [4] = 1000 + 4(4 – 0) = 1000 + 4(4) = 1016

Calculating the length of array Elements

 The length of an array is given by the number of elements stored in it. The general
formula to calculate the length of an array is:

Length = upper_bound – lower_bound + 1

 Where upper_bound is the index of the last element and lower_bound is the index of the first
element in the array.

 Ex: in the above diagram,


length of the array = 7-0+1=8

Initialization of one-dimensional array

 Arrays can be initialized at declaration time as:

int age[5]={2,4,34,3,4};

 It is not necessary to define the size of arrays during initialization. Below


instruction justifies it

int age[]={2,4,34,3,4};

 An array can be initialized by inputting values from the keyboard. In this method, a
while/do–while or a for loop is executed to input the value for each element of the array.

Dynamically Allocated Array

If the array size is decided during run time, then it is called dynamically allocated array.

1-D Array:

Two-D Array:

 C uses array of array representation to represent a multi-dimensional array. Normally, a


pointer contains the address of a variable. A 2-D array represented as a 1-D array of pointers
where each pointer contains address of 1-D array.

 Ex: int x[3][5]; The figure shows array to array representation, where each of 3 pointers
points to 1-D array consisting of 5 locations
Structures

 Structure is a user defined data type that can hold data items of different data types.

 The major difference between a structure and an array is that an array can store only
information of same data type.

 Syntax:

struct {

member1;

member2;

…..
member n;

}; structure name

 Example

struct {

char name[10];
int age;

float salary;

} person;

Assign values to fields:

 to assign values to the fields, use. (dot) as the structure member operator. This operator is
used to select a particular member of the structure

 Ex:

Person.age = 10;

Person.salary = 35000;
Structure Declaration

Tagged Structure Syntax:

 Structure definition with a tag name

Struct structure_name

member1;

member2;

member n;

};

 Example:

struct person

char name[10];
int age;

float salary;

};

Type Defined Structure:

 Syntax:

typedef struct

data_type member 1;

data_type member 2;

………………………

………………………
data_type member n;
} TypeName;

 typedef is the keyword used at the beginning of the definition and by using typedef user
defined data type can be obtained.

 Type_name is not a variable; it is user defined data_type

 Example:

typedef struct

char name[10];
int age;

float salary

} person;

person person1, person2;

 person person1, person2; This statement declares the variable person1 and person2 are of
type person.

Unions

 Union is a derived data type, like structure, i.e. collection of elements of different data
types which are grouped together.

 Each element in a union is called member.

 Union allocates one common storage space for all its members, or memory space is
shared between its members.

 Syntax:

 Method-1:

union tag_name

Type1 member1;
Type2 member2;
….

};

 Method-2:

typedef union

Type1 member1;
Type2 member2;

….

};

 Thus, unions are used to save memory. They are useful for applications that involve multiple
members, where values need not be assigned to all the members at any one time.

Basis of
Structure Union
comparison

The separate memory location is allotted to each All members of the 'union'
member of the 'structure'.
Basic share the same memory
location.

struct struct_name union u_name

{ {

Declaration type element1; type type element1;


element2; type element2;

…. ….

}Size
variable1, variable2,
of Structure= sum...;
of size of all the }Size
variable1, variable2,
of Union=size of ...;
the
Size
Keyword 'struct'
data members. 'union'
largest members.

Stores distinct values for all the members. Stores same value for all the
Store Value
members.

Way of Provide single way to view each memory Provide multiple way to view

Viewing location. same memory location.


Self- Referential Structure

 Self-referential structures are those structures that contain a reference to the data of its
same type.

 For example, consider the structure node given below. Here, the structure node will contain
two types of data: a character data and a pointer link. The value of link is either the address
in the memory of an instance of a list or the null pointer.

 Consider these statements which create 3 structures and assign values to their respective
fields:

link item1, item2, item3;


item1.data=’a’;
item2.data=’b’;
item3.data=’c’;
item1.link=item2.link=item3.link=NULL;
Polynomials

 A polynomial is a sum of terms, where each term has a form

axe

Where x=variable, a=coefficient and e=exponent.

 For ex:

A(x)=3x20+2x5+4 and B(x)=x4+10x3+3x2+1.

 The largest (or leading) exponent of a polynomial is called its degree.

 A polynomial may be represented using arrays or linked lists.

 If any term is not present, then we assign 0’s in the corresponding coefficient.

 A structure may be defined such that it contains two parts:

 one is the coefficient and

 second is the corresponding degree.

 Structure:

#define max_degree 100

typedef struct

float coef[max_degree];

int degree;

} polynomial;
Array of Structure:

 Instead of using one array for each polynomial, we use one array to store all
polynomials, which saves space

Polynomial Addition:

Suppose a is one polynomial and b is another polynomial then c=a+b, where c is the addition of
polynomial a and b. in order to add 2 polynomials, there are different cases to be verified:

Case1: Power of Polynomial a is Equal to Power of Polynomial b

Ex:

a = 25x6+10x5+7x2+9 b = 15x6+5x4+4x3

lead exponent(a)=6 lead exponent(b)=6


lead exponent(a)=lead exponent(b)

sum is,

25x6+10x5+7x2+9

15x6+5x4+4x3

c=40 x6+………

Case 2: Power of Polynomial a is Greater than Power of Polynomial b

Remaining polynomial is:

a =10x5+7x2+9 b = 5x4+4x3

lead exponent(a)=5 lead exponent(b)=4

lead exponent(a)>lead exponent(b). so, copy lead exponent(a) directly to c


sum is,

10x5+7x2+9

5x4+4x3

c=10 x5+………

Case 3: Power of Polynomial a is Less than Power of Polynomial b

Remaining polynomial is:

a =7x2+9 b = 5x4+4x3

Lead exponent(a)=2 lead exponent(b)=4

lead exponent(a)<lead exponent(b). so copy lead exponent(b) directly to c

sum is,

7x2+9

5x4+4x3

4+………
c=5x

so final polynomial is: 40x6+10x5+5x4+4x3+7x2+9

Sparse Matrix

 A sparse matrix is a matrix in which most of the elements are zero. By contrast, if most of the
elements are nonzero, then the matrix is considered dense.

 A matrix is typically stored as a two-dimensional array.

 Sparse Matrix Representation:

 A sparse matrix can be represented by using TWO representations:

Triplet Representation

#define max 101

typedef struct

int col, row, Val;

} term;
term a[max];

 In this representation, we consider only non-zero values along with their row and column
index values.

 Each non zero value is a triplet of the form <R, C, Value> where R represents the row in
which the value appears, C represents the column in which the value appears and Value
represents the nonzero value itself.

 In this representation, the 0th row stores total rows, total columns and total non-zero values
in the matrix.

 For example:
To transpose a matrix

 we just interchange the rows and columns. This means that each element a[i][j] in the
original matrix becomes b[j][i] in the transpose matrix

 Algorithm to find transpose of sparse matrix:

Representation of Multi-Dimensional Arrays

 A multi-dimensional array can be termed as an array of arrays that stores homogeneous data
in tabular form. Data in multidimensional arrays are stored in row-major order. A three-
dimensional (3D) array is an array of arrays of arrays.

 Syntax:

data_type array_name[size1] [size2] [sizeN];

 Ex: Two-dimensional array: int two_d [10][20];

Three dimensional array: int three_d[10][20][30];

Strings

 A C string is a null-terminated character array.


 This means that a null character ('\0') is stored after the last character to signify the end of
the string.

 For example, the string "HELLO" is stored in memory as HELLO'\0'.

STACK

 Stack is a linear data structure which follows a particular order in which the operations are performed.

 A stack is an ordered list in which all


insertions and deletions are made at one
end, called the top.

 Hence, a stack is called a LIFO (Last-In-


First- Out) data structure, as the element that
was inserted last is the first one to be taken
out.

Array Representation of Stacks

 Every stack has a variable called TOP associated with it, which is used to store the address of
the topmost element of the stack.

 There is another variable called MAX, which is used to store the maximum number of
elements that the stack can hold.

 If TOP = NULL, then it indicates that the stack is empty and if TOP = MAX–1, then the
stack is full.
Stack Operations

 Push: Element is inserted into the top of stack using push operation.

 Pop: Element is deleted from the top of stack using pop operation.

 Overflow: checks if the stack is full or not.

 Underflow: checks if the stack is empty or not.

Stack Create:

Push and Stack Overflow:

Pop and Stack Underflow:

Display (Traverse):

Stacks using Dynamic Arrays:

 The array is used to implement stack, but the bound (MAX_STACK_ SIZE) should be
known during compile time.

 The size of bound is impossible to alter during compilation hence this can be overcome by
using dynamically allocated array for the elements and then increasing the size of array as
needed.

Stack Operations using dynamic array

 Stack CreateS ():


 Boolean IsEmpty (Stack): = top < 0;

 Boolean IsFull (Stack): = top >= capacity-1;

 push ():

 pop ():

 stackFull ():

Evaluation and conversion of Expressions

Infix Expression:

 In this expression, the binary operator is placed in-between the operand.

Prefix notations (Polish notation):

 In this expression, the operator appears before its operand.

Postfix (Reverse Polish notation):

 In this expression, the operator appears after its operand.


Infix to Postfix Conversion

Problem.1: Convert the following infix expression into postfix expression:

A – (B / C + (D % E * F) / G) * H

Solution: given in Table 1. (A – (B / C + (D % E * F) / G) * H )

Infix Character Scanned Stack Postfix Expression

( (

A ( A

- (- A

( (-( A

B (-( AB

/ (-(/ AB

C (-(/ ABC

+ (-(+ ABC/

( (-(+( ABC/

D (-(+( ABC/D

% (-(+(% ABC/D

E (-(+(% ABC/DE

* (-(+(* ABC/DE%

F (-(+(* ABC/DE%F

) (-(+ ABC/DE%F*

/ (-(+/ ABC/DE%F*

G (-(+/ ABC/DE%F*G

) (- ABC/DE%F*G/+

* (-* ABC/DE%F*G/+

H (-* ABC/DE%F*G/+H

) ABC/DE%F*G/+H*-
Problem 2: Convert the following infix expression into postfix expression:

(((A+(B-C) *D) ^E+F)

Solution:

Infix Character Scanned Stack Postfix Expression

( (

( ((

( (((

A ((( A

+ (((+ A

( (((+( A

B (((+( AB

- (((+(- AB

C (((+(- ABC

) (((+ ABC-

* (((+* ABC-

D (((+* ABC-D

) (( ABC-D*+

^ ((^ ABC-D*+

E ((^ ABC-D*+E

) ( ABC-D*+E^

+ (+ ABC-D*+E^

F (+ ABC-D*+E^F

) ABC-D*+E^F+
Evaluation of Postfix Expression

Problem 1: Evaluate the following postfix expression FOR A=1, B=2 and C=3:

ABC+*CBA-+*

Solution: 123+*321-+*

Table 1: Solution for Problem 1.

Postfix Expression Symbol Scanned Op2 Op1 Res = Op1 Op Op2 Stack

123+*321-+* 1 1

23+*321-+* 2 1,2

3+*321-+* 3 1,2,3

+*321-+* + 3 2 2+3=5 1,5

*321-+* * 5 1 1*5=5 5

321-+* 3 5,3

21-+* 2 5,3,2

1-+* 1 5,3,2,1

-+* - 1 2 2-1=1 5,3,1

+* + 1 3 3+1=4 5,4

* * 4 5 5*4=20 20

25
Problem 2: Evaluate the following postfix expression FOR A=1, B=2 and
C=3:

AB+C-BA+C^-

Solution: 12+3-21+3^-

Postfix Expression Symbol Scanned Op2 Op1 Res = Op1 Op Op2 Stack

12+3-21+3^- 1 1

2+3-21+3^- 2 1,2

+3-21+3^- + 2 1 1+2=3 3

3-21+3^- 3 3,3

-21+3^- - 3 3 3-3=0 0

21+3^- 2 0,2

1+3^- 1 0,2,1

+3^- + 1 2 2+1=3 0,3

3^- 3 0,3,3

^- ^ 3 3 3^3=27 0,27

- - 27 0 0-27=-27 -27

26
Problem 3: Convert the following infix expression to postfix expression and evaluate the
postfix expression FOR A=6, B=3, C=1, D=2, E=4:

A/B-C+D*E-A*C

Solution: ((((A/B)-C) +(D*E))-(A*C)).

We get AB/C-DE*+AC*- as postfix expression.

we get: 63/1-24*+61*-.

Postfix Expression Symbol Scanned Op2 Op1 Res = Op1 Op Op2 Stack

63/1-24*+61*- 6 6

3/1-24*+61*- 3 6,3

/1-24*+61*- / 3 6 6/3=2 2

1-24*+61*- 1 2,1

-24*+61*- - 1 2 2-1=1 1

24*+61*- 2 1,2

4*+61*- 4 1,2,4

*+61*- * 4 2 2*4=8 1,8

+61*- + 8 1 1+8=9 9

61*- 6 9,6

1*- 1 9,6,1

*- * 1 6 6*1=6 9,6

- - 6 9 9-6=3 3

27
Module 2-Queues & Linked Lists

QUEUES
 A queue is an ordered list in which insertions (additions, pushes) and deletions (removals
and pops) take place at different ends”.

 The end at which new elements are added is called the rear, and that from which old
elements are deleted is called the front.

 Queues are also known as First-In-First-Out (FIFO) lists.


Array Representation of Queues
 Queues can be easily represented using linear arrays.

 In array representation of a queue Queues may be represented by one-way lists or linear


arrays and it will be maintained by a linear array QUEUE and two pointer variables:

 FRONT-containing the location of the front element of the queue.

 REAR-containing the location of the rear element of the queue.

 The condition FRONT = NULL will indicate that the queue is empty.

Queue Operations

 enqueue () − add (store) an item to the queue from front end.

 dequeue () − remove (access) an item from the queue from rear end.
 isfull () / overflow− Checks if the queue is full.

 isempty () /underflow− Checks if the queue is empty.

Queue Create

 The maximum number of elements that can be entered into queue can be defined as:

#define MAX 5

 Since queue is an array, we can declare it as:

int q[MAX];

 The variables ‘front’ and ‘rear’ are associated with q and holds the index of 1st element

 and index of rear element.

 Initially, front is initialized to -1 and rear to -1.

int front = -1, rear = -1;

Queue Insert and Overflow


The algorithm to insert an element into the queue

Example:
Elements 10, 20, 30, 40 and 50 is entered into queue as in
Queue Delete and Underflow
 The algorithm to delete an element from the queue

Example:
 delete 10, 20, 30, 40 and 50 from the queue.

 When front>rear it results empty queue.

 Hence we need to make the initial condition as front=0 and rear = -1 when front>rear.
Queue Display
 If queue is having some items, then it should be displayed one after the other. If there is no
item, then it should display error message.

 Algorithm for display queue operation

Step 1 − Check if the queue is empty.


Step 2 − If the queue is empty, produce empty queue error and exit.
Step 3 − If the queue is not empty, display from front to rear.

Circular Queues
 Circular Queue is also called ring Buffer.

 A circular queue is a linear data structure in which the operations are performed based on
FIFO (First In First Out) principle and the last position is connected back to the first
position to make a circle.

 Circular Queue is also called ring Buffer.

 In a normal Queue Data Structure, we can insert elements until queue becomes full. For
example, consider the queue in the Fig. 2.8(a).

 Now consider the Fig.2.8 (b), the situation after deleting three elements from the queue.
This situation also says that Queue is full and we cannot insert the new element because
'rear' is still at last position.

 This is the major problem in a normal queue data structure. To overcome this problem, we
use a circular queue data structure.
 When the array is viewed as a circle, each array position has a next and a previous position.
The position next to position MAX-1 is 0, and the position that precedes 0 is MAX-1, the
next element is put into position 0.

 If front =-1 and rear =-1 we cannot distinguish between queue empty and queue full. To
avoid this confusion, we set front=0 and rear =0 in circular queue.

Different operations of circular Queue.


Create Circular Queue
 We have to define the maximum size of circular
queue.
#define MAX 3
 Initially both the front and rear points to 0 in a
circular queue.

int front=0, rear=0;

 Declare a queue array of size MAX as:

int q[MAX];
Insert and Overflow in Circular Queue
 In a circular queue, the new element is always inserted at rear position.

Delete and Underflow in Circular Queue


 In a circular queue, the element is always deleted at front position.
Display in Circular Queue

Multiple Stacks

 Method in which more than one stack is present in the same array of sufficient size.

 Used to overcome overflow condition in normal stack.

 In Fig. 2.13(a) an array STACK[n] is used to represent two stacks, Stack A and Stack B.
The value of n is such that the combined size of both the stacks will never exceed n.

 While operating on these stacks, it is important to note one thing—Stack A will grow from
left to right, whereas Stack B will grow from right to left at the same time.

 Extending this concept to multiple stacks, a stack can also be used to represent n number of
stacks in the same array. That is, if we have a STACK[n], then each stack I will be
allocated an equal amount of space bounded by indices b[i] and e[i]. This is shown in Fig.
2.13(b)
Multiple Queues

 Method in which more than one queue or multiple queue is in the same array of sufficient
size.

 Figure 2.14(a), an array QUEUE[n] is used to represent two queues, QUEUE A and
QUEUE B.

 The value of n is such that the combined size of both the queues will never exceed n.

 QUEUE A will grow from left to right, whereas QUEUE B will grow from right to left at
the same time.

 Extending the concept to multiple queues, a queue can also be used to represent n number
of queues in the same array.

 That is, if we have a QUEUE[n], then each QUEUE I will be allocated an equal amount
of space bounded by indices b[i] and e[i].
Linked List
A linked list or one-way list, is a linear collection of data elements, called nodes, where the linear
order is given by means of pointers. That is, each node is divided into two parts:
 The first part contains the information of the element, and
 The second part, called the link field or next pointer field, contains the address of the
next node in the list.

Representation of linked lists in Memory


Let LIST be a linked list. Then LIST will be maintained in memory as follows.
1. LIST requires two linear arrays such as INFO and LINK-such that INFO[K] and LINK[K]
contains the information part and the next pointer field of a node of LIST.
2. LIST also requires a variable name such as START which contains the location of the
beginning of the list, and a next pointer sentinel denoted by NULL-which indicates the end
of the list.
3. The subscripts of the arrays INFO and LINK will be positive, so choose NULL = 0.

Representing Chain In C
The following capabilities are needed to make linked representation

 A mechanism for defining a node’s structure, that is, the field it contains. So self- referential
structures can be used

typedef struct node

char data [10]; struct node *link;

} NODE;

NODE *start= NULL;

 A way to create new nodes, so malloc functions can do this operation.

Start= (struct node*) malloc (sizeof (struct node));

Scanf(“%s”, start→ data);

start→ link = NULL

 A way to remove nodes that no longer needed. The free function handles this operation.
Linked List Operations
Create

Insert to Front
It works same as create function, by inserting new node to front. Here only one node can be inserted
at a time.

Insert to End
Delete from Front

Delete from End


Traverse (Display) the linked list

Linked Stacks
 The linked representation of a stack is shown

 The push operation is used to insert an element into the stack. The new element is added at
the topmost position of the stack.
 the algorithm to push an element into a linked stack.

 The pop operation is used to delete the topmost element from a stack.

 the algorithm to delete an element from a stack.

Linked Queue
 The linked representation of queue

 The START pointer of the linked list is used as FRONT.


 The REAR pointer will store the address of the last element in the queue.
 If FRONT=REAR=NULL, then it indicates that the queue is empty.
 The insert operation is used to insert an element into the queue. The new element is added
as the last element of the queue.

 the algorithm to insert an element into a linked queue.

 The delete operation is used to delete the element that is first inserted into the queue.

 The algorithm to delete an element from a queue.


Applications of Linked lists: Polynomials
 A polynomial is a combination of coefficients and exponents. We represent a polynomial,
each term as a node containing coefficients and exponent field, as well as a pointer to next
term.

Polynomial representation
 Consider a polynomial 6x3 + 9x2 + 7x + 1. Every individual term in a polynomial consists of
two parts, a coefficient and a power. Every term of a polynomial can be represented as a
node of the linked list.

Polynomial Addition
 Consider an example polynomial To add polynomial, we examine their terms starting at the
nodes pointed by a and b. There are three cases:

 Case 1: If the exponent of a and b are equal


 Case 2: If the exponent of a is less than exponent of b

Fig. 3.11.

 Case 3: If the exponent of a is greater than exponent of b

 Result of the addition:

Circular list representation of Polynomial


 The zero polynomial is represented as in Fig. 3.12 (a)

 The a(x) = 3x14+2x8+1 can be represented as in Fig 3.12 (b).

 here link field of last node points to the 1 st node in the list. So we call this a circular linked
list.
LINKED LISTS

Additional List Operations

Reverse Operations For chains:

FIRST

FIRST

Concatenate two Linked Lists

FIRST1 temp

FIRST2

Algorithm for concatenation

Search the linked list

Searching can be done for two cases:

 List is unsorted

 List is sorted

Inserting a Node After a Given Node in a Linked List

Removing a Node After a Given Node in a Linked List


Operations for Circular Linked List

Create a Node

Insert into the front of the list

Insert into the end

Delete from front

Delete from end

Finding length of a circular linked list

Sparse matrices
 In linked representation, we use linked list data structure to represent a sparse matrix.

 In this linked list, we use two different nodes namely header node and element node.

 Header node consists of three fields and element node consists of five fields.

In this representation, H0, H1..., H5 indicates the header nodes which are used to represent
indexes.

Remaining nodes are used to represent non-zero elements in the matrix, except the very first
node which is used to represent abstract information of the sparse matrix (i.e., It is a
matrix of 5 X 6 with 6 non-zero elements).

In this representation, in each row and column, the last node right field points to its respective
header node

Doubly Linked List

 Doubly linked list is a linear collection of data elements, called nodes, where each node N
is divided into three parts:

a. An information field INFO which contains the data of N.

b. A pointer field LLINK which contains the pointer to previous node.

c. A pointer field RLINK which contains the pointer to next node.

 This list also contains pointer field ‘first’ which points to first node and ‘last’ which
points to last node.

Representation

 Using first and RLINK we can traverse in forward direction. Using last and LLINK we
can traverse in backward direction.
 Structure declaration of doubly linked list is shown in Fig.3.3.1(b)

Representation of Doubly Linked List in Memory

Operation
s of Doubly Linked List

Create

Insert to end

Insert to front
Delete from Front

Delete from End

Traverse (Display)

Doubly Circular Linked List

 In this type the ‘Next’ of the last node points to the first node in a doubly-linked list. The ‘Prev’ of
the first node points to the last node.

 A doubly circular linked list looks as follows:

Inserting a New Node in a Circular Doubly Linked List:


1. The new node is inserted at the beginning.

2. The new node is inserted at the end.


Deleting a Node from a Circular Doubly Linked List:

1. Deleting the First Node from a Circular Doubly Linked List:

2. Deleting the Last Node from a Circular Doubly Linked List:


TREES:

 A tree is a very popular non-linear data structure used in a wide range of applications.

 In tree data structure, every individual element is called as Node.

 Node in a tree data structure stores the actual data of that particular element and link to next
element in hierarchical structure.

Tree Definition

 Definition: A tree is a finite set of one or more nodes such that: (i) there is a specially designated
node called the root; (ii) the remaining nodes are partitioned into disjoint sets T1, ..., Tn where
each of these sets is a tree. T1, ..., Tn are called the sub-trees of the root.

Basic Terminology

Root node:

 The root node R is the topmost node in the tree. If R = NULL, then it means the tree is empty.

 In a tree data structure, the first node is called as Root Node.

Edge:

 In a tree data structure, the connecting link between any two nodes is called as EDGE.

 In a tree with 'N' number of nodes there will be a maximum of 'N-1' number of edges.
Parent

 In a tree data structure, the node which is a predecessor of any node is called as PARENT NODE.

 In simple words, the node which has a branch from it to any other node is called a parent node.

Child:

 In a tree data structure, the node which is descendant of any node is called as CHILD Node.

 In a tree, all the nodes except root are child nodes.


Siblings:

 children of same parent are called siblings.

Sub-trees:

 In a tree data structure, each child from a node forms a subtree recursively.

Internal Nodes:

 In a tree data structure, nodes other than leaf nodes are called as Internal Nodes.

 Internal nodes are also called as 'Non- Terminal' nodes.


Leaf node:

 A node that has no children is called the leaf node or the terminal node.

Path:

 In a tree data structure, the sequence of Nodes and Edges from one node to another node is
called as PATH

 Length of a Path is total number of nodes in that path.


Degree

 In a tree data structure, the total number of children of a node is called as DEGREE of that Node.

 The highest degree of a node among all the nodes in a tree is called as 'Degree of Tree'

Level:

 Every node in the tree is assigned a level number in such a way that the root node is at level 1,
children of the root node are at level number 2.

 All child nodes have a level number given by parent’s level number + 1.

Height of the tree:

 is the maximum distance between the root node of the tree and the leaf node of the tree.
Depth of the tree:

 In a tree, the total number of edges from root node to a leaf node in the longest path is said to be
Depth of the tree.

Ancestor node:

 An ancestor of a node is any predecessor node on the path from root to that node.

Descendant node:

 A descendant node is any successor node on any path from the node to a leaf node.
In-degree:

 In-degree of a node is the number of edges arriving at that node.

Out-degree:

 Out-degree of a node is the number of edges leaving that node.

Representation of Tree:

Example:

List Representation:

 The tree can be represented as a List.

 The information in the root node comes first.

 The root node is followed by a list of the subtrees of that node

(A (B (E (K, L), F), C (G), D (H (M), I, J)))

Left Child-Right Sibling Representation

To convert the tree into this representation:

 First note that every node has at most one leftmost child

 At most one closest right sibling.


The left child field of each node points to its leftmost child (if any), and the right sibling field points
to its closest right sibling (if any).

Representation as a Degree-Two Tree

 To obtain the degree-two tree representation of a tree, simply rotate the right-sibling
pointers in a left child-right sibling tree clockwise by 45 degrees.

Binary Trees

 Definition: A binary tree is a finite set of nodes which is either empty or consists of a root and two
disjoint binary trees called the left subtree and the right subtree.
 The first one has an empty right subtree while the second has an empty left subtree.
 If T does not contain a root R, then two trees T1 and T2 are called left and right
subtrees of R.

 If T1 is non empty, then root is called the left successor of R

 If T2 is non empty, then root is called right successor of R.

Difference between General tree and Binary tree

General tree Binary tree

General tree is a tree in which each node can have many Whereas in binary tree, each node can have at most two
children or nodes. nodes.

The subtree of a general tree does not hold the ordered While the subtree of binary tree holds the ordered property.
property.

In data structure, a general tree cannot be empty. While it can be empty.

In general tree, a node can have at most n While in binary tree, a node can have at most
(number of child nodes) nodes. 2(number of child nodes) nodes.

In general tree, there is no limitation on the degree of a While in binary tree, there is limitation on the degree of
node. a node

In general tree, there is either zero subtree or many While in binary tree, there are mainly two subtrees:
subtrees. Left-subtree and Right-subtree.

Abstract Data Type Binary Tree


 Functions: for all bt, bt1, bt2 ∈ BinTree, item ∈ element

 BinTree Create (): creates an empty binary tree

 Boolean IsEmpty(bt): if (bt == empty binary tree) return TRUE else return FALSE

 BinTree MakeBT (bt1, item, bt2): return a binary tree whose left subtree is bt1, whose right subtree is bt2, and
whose root node contains the data item.

 BinTree Lchild(bt): if (IsEmpty(bt)) return error else return the left subtree of bt.

 Element Data(bt): if (IsEmpty(bt)) return error else return the data in the root node of bt.
 BinTree Rchild(bt): if (IsEmpty(bt)) return error else return the right subtree of bt

Different kind Binary Trees:

Skewed Tree:

 A skewed tree is a tree, skewed to the left or skews to the right. or it is a tree consisting of only left
sub-tree or only right sub-tree

 A tree with only left sub-trees is called Left Skewed Binary Tree.

 A tree with only right sub-trees is called Right Skewed Binary Tree.

Complete:

 In this type all its levels, except possibly the last level, have the maximum number node 2 i, i ≥ 0
and if all the nodes at the last level appears as far left as possible.

Full Binary Tree:

 A full binary tree of depth ‘k’ is a binary tree of depth k having 2k – 1 nodes, k ≥ 1
Extended Binary tree:

 A binary tree T is said to be a 2-tree or an extended binary tree if each node N has either 0 or 2
children.

 The nodes with 2 children are called internal nodes and the nodes with 0 children are called external
nodes.

 Sometimes the nodes are distinguished in diagrams by using circles for internal nodes and squares for
external nodes

Strictly Binary Tree:

 If every non-leaf node in a binary tree has non empty left and right subtrees, the tree is called a
strictly binary tree.

Expression Tree

 An expression containing operands and binary operators can be represented by a binary tree.
Properties of Binary Tree

Lemma 1& 2:

The maximum number of nodes on level i of a binary tree is 2 i-1, i ≥1.

The maximum number of nodes in a binary tree of depth k is 2 k -1, k ≥ 1

Proof:

 Step 1: Induction Base: The root is the only node on level i = 1. Hence, the maximum
number of nodes on level i =1 is 2i-1 = 20 = 1.

 Step 2: Induction Hypothesis: Let i be an arbitrary positive integer greater than 1.


Assume that the maximum number of nodes on level i -1 is 2i-2

 Step 3: Induction Step: Since each node in a binary tree has a maximum degree of 2, the
maximum number of nodes on level i is two times the maximum number of nodes on level
i-1, i.e. or 2*2i-2= 2i-1

(2) The maximum number of nodes in a binary tree of depth k is 𝒊=𝟏 = ∑𝒌𝒊=𝟏 𝟐𝒊−𝟏 =𝟐𝒌−𝟏
∑𝒌
Lemma– 3:

For any non-empty binary tree, T, if n0 is the number of terminal nodes and n2 the number of
nodes of degree 2, then n0 = n2 + 1.

Proof:

• Consider the binary tree with each node in the tree should have a maximum of 2 children. A node
may not have any child or it can have single child or it can have 2 children. But a node in a binary
tree can not have more that 2 children.

Let: No of nodes of degree 0 = n0


No of nodes of degree 1 = n1
No of nodes of degree 2 =
n2

So total no. of nodes in the tree is = n0 + n1 + n2.......................................................................................1

Observe from the tree that the total no of nodes is equal to the total no. of branches (B) plus 1
n = B+1......................................................................................................................2

if there is a node with degree 1, no. of branches = 1

so, for n1, no. of branches = 1n1.......................................................................................................................3

if there is a node with degree 2, no. of branches = 2

so for n2, no. of branches = 2n2...........................................................................................................................4

add 3 and 4, we get

B = 1n1 + 2n2...................................................................................................................................................................5

Substitute 5 in 2 we get
n = 1n1 + 2n2 + 1.....................................................................................................6

from 1 and 6 we get

n0 + n1 + n2 = 1n1 + 2n2 + 1

=> n0 + n1 + n2 - 1n1 - 2n2– 1 = 0=> n0 - n2 – 1 =0=> n0 = n2 + 1

Hence it is proved that for any nonempty binary tree, T, if no is the number of terminal nodes and n2
the number of nodes of degree 2, then n0 = n2 + 1.

Binary Tree Representation

Array representation

If T is a binary tree, then it can be maintained in memory using sequential representation as follows:

• Root R of tree T is stored in TREE [1]

• If a node n occupies TREE[k], then its left child is stored in TREE[2*k] and right child is
stored in TREE[2*k+1]

• NULL is used to represent empty sub-tree

 If TREE [1] is NULL, then it is an empty tree.


Linked representation:

Each node will have three fields LCHILD, DATA and RCHILD as in Fig.3.12.2.

Ex:

Binary Tree Traversal

1. Preorder traversal

The algorithm works by:

o Visiting the root node,

o Traversing the left sub-tree, and

o Traversing the right sub-tree.

2. Postorder traversal

3. Inorder traversal
Iterative Inorder traversal (or) Inorder Tree Traversal without Recursion.

Here we can use a stack to perform inorder traversal of a Binary Tree. Below is the algorithm for
traversing a binary tree using stack.

1. Create an empty stack (say S).

2. Initialize the current node as root.

3. Push the current node to S and set current = current->left until current is NULL

4. If current is NULL and the stack is not empty then:

o Pop the top item from the stack.

o Print the popped item and set current = popped_item->right

o Go to step 3.

5. If current is NULL and the stack is empty then we are done.


Level Order Traversal

 Level Order Traversal technique is defined as a method to traverse a Tree such that all nodes
present in the same level are traversed completely before traversing the next level.

 In level order traversal nodes of tree are traversed level-wise from left to right.

 Queue data structure is used to store nodes level wise so that each node’s children are visited.
Threaded Binary Trees

A threaded binary tree is a type of binary tree data structure where the empty left and right child
pointers in a binary tree are replaced with threads that link nodes directly to their in-order predecessor or
successor, thereby providing a way to traverse the tree without using recursion or a stack.

To construct the threads, use the following rules(Assume that ptr represents a node):

 If ptr→leftChild is null, then replace the null link with a pointer to the inorder predecessor of
ptr.

 If ptr →rightChild is null, replace the null link with a pointer to the inorder successor of ptr.
When trees are represented in memory, it should be able to distinguish between threads and pointers. This
can be done by adding two additional fields to node structure, ie., leftThread and rightThread

 If ptr→leftThread = TRUE, then ptr→leftChild contains a thread, otherwise it contains a pointer


to the left child.

 If ptr→rightThread = TRUE, then ptr→rightChild contains a thread, otherwise it contains pointer


to the right child.
This node structure is defined as follows
struct node
{
short int leftThread;
struct node * leftChild;
char data;
thread node *rightChild;
short int rightThread;
};
typedef struct node ThreadNode;

Inorder traversal of a threaded binary tree


By using of threads, we can perform an inorder traversal without making use of a stack
(simplifying the task)

 If ptr->right_thread = TRUE, the inorder successor of ptr is ptr->right_child by definition of the


threads

 Otherwise, we obtain the inorder successor of ptr by following a path of left-child links
from the right-child of ptr until we reach a node with left_thread = TRUE
MODULE 04: Trees and Graphs

BINARY SEARCH TREES:

Binary search trees (BST), sometimes called ordered or sorted binary trees, are a particular type
of containers: data structures that store "items"
A binary search tree is a binary tree; either it is empty or each node in the tree contains an
identifier and:
all identifiers in the left subtree of T are less (numerically or alphabetically) than the identifier
in the root node T;
all identifiers in the right subtree of T are greater than the identifier in the root node T;
the left and right subtrees of T are also binary search trees.

Create Function
Example:
Create a binary search tree using the following data elements: 45, 39, 56, 12, 34, 78, 32, 10.
Insert Function
The insert function is used to add a new node with a given value at the correct position in the
binary search tree.

Search Function:
The search function is used to find whether a given value is present in the tree or not.

Recursive search of a binary search tree:


Delete Function:
The delete function deletes a node from the binary search tree
Case 1: Deleting a Node that has No Children
Look at the binary search tree given in Fig. 4.14. If we have to delete node 78, we can simply
remove this node without any issue. This is the simplest case of deletion.

Case 2: Deleting a Node with One Child


To handle this case, the node’s child is set as the child of the node’s parent.

Case 3: Deleting a Node with Two Children


To handle this case, replace the node’s value with its in-order predecessor (largest value in
the left sub-tree) or in-order successor (smallest value in the right sub-tree).
SELECTION TREE:
A tournament tree is a form of complete binary tree in which each node denotes a player.
The last level has n-1 nodes (external nodes) used to represent all the players, and the rest of the nodes
(internal nodes) represent either the winner or loser among them.
It is also referred to as a Selection tree
Winner Tree:
In a tournament tree, when the internal nodes represent the winner of the match, the tree obtained is
referred to as the winner tree.
Each internal node stores either the smallest or greatest of its children, depending on the winning criteria.
When the winner is the smaller value then the winner tree is referred to as the minimum winner tree,
When the winner is the larger value, then the winner tree is referred to as the maximum winner tree
Example: Eight players participate in a tournament where a number represents each player, from 1 to 8.
The pairings of these players are given below:
Group A: 1, 3, 5, 7 (Matches: 1 - 7 and 3 - 5)
Group B: 2, 4, 6, 8 (Matches: 2 - 6, and 4 - 8)
Winning Criteria - The player having the largest value wins the match. Represent the winner tree
(maximum winner tree) for this tournament tree.

Looser Trees
In a tournament tree, when the internal nodes are used to represent the loser of the match between two,
then the tree obtained is referred to as the loser tree.
When the loser is the smaller value then the loser tree is referred to as the minimum loser tree, and when
the loser is the larger value, then the loser tree is referred to as the maximum loser tree.

Forest
Definition: A forest is a set of n disjoint trees.
When we remove the root of a tree we obtain a forest.
For example, removing the root of any binary tree produces a forest of two trees.
Three-tree forest

4.1.1 : Transforming a forest into a binary tree

Definition: If T1, . . ., Tn is a forest of trees, then the binary tree corresponding to this forest,
denoted by B (T1, . . . , Tn),

(1) is empty, if n = 0

(2) has root equal to root (T1); has left subtree equal to B(T11,T12. . . T1m), where T11, . . .
,T1m are the subtrees of root (T1); and has right subtree B(T2, . . . ,Tn)
Binary tree representation of forest

4.1.2 Forest Traversal

Preorder Traversal:

The preorder traversal of T is equivalent to visiting the nodes of Fin tree preorder. We define
this as:

1. If F is empty, then return.


2. Visit the root of the first tree of F.
3. Traverse the subtrees of the first tree in tree preorder.
4. Traverse the remaining trees of F in preorder.

Inorder Traversal:

Inorder traversal of T is equivalent to visiting the nodes of F in tree inorder, which is defined
as:

1. If F is empty, then return.


2. Traverse the subtrees of the first tree in tree inorder.
3. Visit the root of the first tree.
4. Traverse the remaining trees in tree inorder.

Postorder Traversal:

There is no natural analog for the postorder traversal of the corresponding binary tree of a
forest. Nevertheless, we can define the postorder traversal of a forest, F, as:

1. If F is empty, then return.


2. Traverse the subtrees of the first tree of F in tree postorder.
3. Traverse the remaining trees of F in tree postorder.
4. Visit the root of the first tree of F.
4.2 Representation of Disjoint Sets

4.2.1 Introduction

The use of trees in the representation of sets. assume that the elements of the sets are the
numbers 0, 1, 2,. . .n-1. In practice, these numbers might be indices into a symbol table that
stores the actual names of the elements.

For example, if we have 10 elements numbered 0 through 9, we may partition them into three
disjoint sets, 51 = {0, 6, 7, 8), S2 = {1, 4, 9}, and S3 = {2, 3, 5}.

Figure shows one possible representation for these sets.

The minimal operations that we wish to perform on these sets are:

Disjoint set union and Find(i).

4.2.2 Union and Find operations:

to obtain the union of S1 and S2Since we have linked the nodes from children to parent, we
simply make one of the trees a subtree of the other.
To implement the set union operation, we simply set the parent field of one of the roots to the
other root. We can accomplish this easily if, with each set name, we keep a pointer to the root
of the tree representing that set.

rather than using the set name S1 we refer to this set as 0. The transition to set names is easy.
We assume that a table, name [ ], holds the set names. If i is an element in a tree with root 7,
and j has a pointer to entry k in the set name table, then the set name is just name[k].

Definition: Weighting rule for union(i, j). If the number of nodes in tree i is less than the
number in tree j then make j the parent of i; otherwise make i the parent of j.
Fig. Union function using weighting rule

Definition [collapsing rule] : If j is a node on the path from i to its root and parent[i] !=
root(i), then set parent [j] to root(i).

Fig: Collapsing rule


4.3 Counting Binary trees

4.3.1 : Distinct Binary tree

if n = 0 or n = 1, there is only one binary tree. If n = 2, then there are two distinct trees and if
n = 3.

4.3.2 Stack permutations:

Suppose we have the preorder sequence: ABCDEFGHI and the inorder sequence:
BCAEDGHFI of the binary tree. To construct the binary tree from these sequences, we
look at the first letter in the preorder sequence, A. This letter must be the root of the tree
by definition of the preorder traversal (VLR.}. We also know by definition of the inorder
traversal {LVR} that all nodes preceding A in the inorder sequence (B Q are in the left
subtree, while the remaining nodes {ED GHFI) are in the right subtree. Figure 5.49(a) is
our first approximation to the correct tree. Moving right in the preorder sequence, we find
B as the next root. Since no node precedes B in the inorder sequence, B has an empty left
subtree, which means that C is in its right subtree. Figure 5.49(b) is the next
approximation. Continuing in this way, we arrive at the binary tree of Figure 5.49(c). By
formalizing this argument (see the exercises for this section), we can verify that every
binary tree has a unique pair of preorder inorder sequences.
 If the nodes of the tree are numbered such that its preorder permutation is 1, 2, • • •
,n, then from our earlier discussion it follows that distinct binary trees define
distinct inorder permutations.
 Thus, the number of distinct binary trees is equal to the number of distinct inorder
permutations obtainable from binary trees having the preorder permutation, 1,2, • •

n. Using the concept of an inorder permutation, we can show that the number of dis
tinct permutations obtainable by passing the numbers 1 to n through a stack and
deleting in all possible ways is equal to the number of distinct binary trees with n
nodes (see the exercises). If we start with the numbers 1, 2, 3, then the possible
permutations obtain able by a stack are: (1, 2, 3) (1, 3, 2) (2, 1, 3) (2, 3, 1) (3, 2, 1)
Obtaining (3, 1, 2) is impossible.
GRAPHS
Definitions

 A graph is an abstract data structure that is used to implement the mathematical concept
of graphs. It is basically a collection of vertices (also called nodes) and edges that
connect these vertices.

 A graph, G, consists of two sets V and E. V is a finite non-empty set of vertices. E is a


set of pairs of vertices, these pairs are called edges.

 V(G) and E(G) will represent the sets of vertices and edges of graph G. We will also
write G = (V, E) to represent a graph.

 In an undirected graph each edge is not represented by a directed pair. Thus, the pairs
(v1, v2) and (v2, v1) represent the same edge.

 In a directed graph each edge is represented by a directed pair (v1, v2). v1 is the tail and
v2 the head of the edge. Therefore, and represent two different edges.

V (G1) = {1,2,3,4}; E(G1) = {(1,2), (1,3), (1,4), (2,3), (2,4), (3,4)}

V (G2) = {1,2,3,4,5,6,7}; E(G2) = {(1,2), (1,3), (2,4), (2,5), (3,6), (3,7)}

V (G3) = {1,2,3}; E(G3) = {<1,2>, <2,1>, <2.3>}


Terminologies

Complete graph:

 A graph G is said to be complete if all its nodes are fully connected.

 A complete graph has n(n–1)/2 edges, where n is the number of nodes in G.

Adjacent and incident:

 If (v1, v2) is an edge in E(G), then we shall say the vertices v1 and v2 are adjacent and
that the edge (v1, v2) is incident on vertices v1 and v2.

 The vertices adjacent to vertex 2 in G2 are 4, 5 and 1. The edges incident on vertex 3 in
G2 are (1,3), (3,6) and (3,7).

A subgraph:

 A subgraph of G is a graph G' such that V(G')⊆ V(G) and E(G') ⊆ E(G)
Path:

 a path from vertex vp to vertex vq in graph G is a sequence of vertices vp, vi1, vi2, ...,
vin, vq such that (vp, vi1), (vi1, vi2), ..., (vin, vq) are edges in E(G).

Length:

 length of a path is the number of edges on it.

Simple Path:

 A simple path is a path in which all vertices except possibly the first and last are
distinct.

 A path such as (1,2) (2,4) (4,3) we write as 1,2,4,3. Paths 1,2,4,3 and 1,2,4,2 are both of
length 3 in G1. The first is a simple path while the second is not.

Cycle:

 A cycle is a simple path in which the first and last vertices are the same.

 1,2,3,1 is a cycle in G1. 1,2,1 is a cycle in G3.

Connected graph:

 An undirected graph G is said to be connected if for every pair of distinct vertices u and v in
V(G) there is a path from u to v in G.

Connected acyclic graph:

 A tree is a connected acyclic (i.e., has no cycles) graph.

Strongly connected:

 A directed graph G is said to be strongly connected if for every pair of distinct vertices
vi, vj in V(G) there is a directed path from vi to vj and also from vj to vi. The graph G3
is not strongly connected as there is no path from v3 to v2.

Degree:

 The degree of a vertex is the number of edges incident to that vertex.


 In case G is a directed graph, we define the in-degree of a vertex v to be the number of
edges for which v is the head. The out-degree is defined to be the number of edges for
which v is the tail. Vertex 2 of G3 has in-degree 1, out-degree 2 and degree 3.

ADT Graph

Graph representations

Adjacency Matrix:

 Let G = (V, E) be a graph with n vertices, n>=1. The adjacency matrix of G is a 2- dimensional
nXn array, say A, with the property that A (i, j) = 1 if the edge (vi, vj) (for a directed graph) is in
E(G). A (i, j) = 0 if there is no such edge in G.
Adjacency Lists:

 In this representation the n rows of the adjacency matrix are represented as n linked lists.

 The nodes in list i represent the vertices that are adjacent from vertex i.

 Each node has at least two fields: VERTEX and LINK.

 Each list has a head node.

Adjacency Multilists:

 For each edge there will be exactly one node, but this node will be in two list (i.e., the
adjacency list for each of the two nodes to which it is incident).

 A new field is necessary to determine if the edge is determined and mark it as examined.

 new node structure is


Weighted Edges:

 The edges of a graph are assigned weights.

 These weights may represent the distance from one vertex to another or the cost of
going from one vertex to an adjacent vertex.

 A graph with weighted edges is called a network.

Elementary Graph Operations

Depth First Search

1. Visit the starting vertex v. (visiting consist of printing node’s vertex)

2. Select an unvisited vertex w from v’s adjacency and carry a depth first search on w.

3. A stack is maintained to preserve the current position in v’s adjacency list.

4. When we reach a vertex u that has no unvisited vertices on adjacency list, remove a vertex from the stack
and continue processing its adjacency list. Previously visited vertices are discarded and unvisited vertices
are placed on stack

5. The search terminates when the stack is empty.


Breadth First Search

1. Search starts at vertex v marks it as visited.

2. It then visits each of the vertices on v’s adjacency list.

3. As we visit each vertex it is placed on a queue.

4. When all the vertices in the adjacency list is visited, we remove a vertex from the queue and
proceed by examining each of the vertices in its adjacency list.

5. Visited vertices are ignored and unvisited vertices are placed on the queue

6. The search terminates when the queue is empty.


Module 5-Hashing

Hashing

 Hashing refers to the process of generating a fixed-size output from an input of variable size using the
mathematical formulas known as hash functions.

 Hashing enables us to perform the dictionary operations such as search, insert and deleting.

Hash Function

 Is a function which is used to put the data in hash table.

 The integer is returned by the hash function is called hash key.

 A good hash function should satisfy 2 criteria

 A hash function should hash address such that keys are distributed as evenly as possible
among the various cells of the hash table.

 Computation of key should be simple.

Hash Table

 Hash table is a data structure used for storing and retrieving data very quickly.

 Insertion, Deletion or Retrieval operation takes place with help of hash value.

 Hence every entry in the hash table is associated with some key.

 Using the hash key the required piece of data can be searched in the hash table by few or more key
comparisons.
Types of Hash Functions

Division Method:

 It is the simplest method of hashing an integer x.

 This method divides x by M and then uses the remainder obtained.

 In this case, the hash function can be given as

h(x) = x mod M

 Generally, it is best to choose M to be a prime number because making M a prime number increases
the likelihood that the keys are mapped with a uniformity in the output range of values.

Mid-Square Method:

 Here, the key K is squared. A number ‘l’ in the middle of K2 is selected by removing the digits from
both ends.

 hash function H is defined by:


H(k)=l

 Example: Let key=2345, Its square is K2 =574525 H (2345) =45=>by discarding 57 and 25

Folding Method:

 Divide the key value into a number of parts. That is, divide k into parts k1, k2, ..., kn, where each part
has the same number of digits except the last part which may have lesser digits than the other parts.

 Add the individual parts. That is, obtain the sum of k1 + k2 + ... + kn. The hash value is produced by
ignoring the last carry, if any.

Collision Resolution Techniques

 When two or more keys map to the same memory location, a collision occurs.

 A method used to solve the problem of collision is called collision resolution technique.

Collision Resolution by Linear Probing (open addressing)

 Suppose new record R with key K is to be added to the memory table T, but that memory with H(k) =
h is already filled, one way of avoiding collision is to assign R to 1st available location following
T[h].

 When inserting a new pair whose key is k, we search the hash table in the order

ht[h(k) +i] %b [0<=i <=b-1]

h is the hash function and

b is the number of buckets

 Example: Consider a hash table of size 10. Using linear probing, insert the keys 72, 27, 36, 24, 63,
81, 92, and 101 into the table.

Solution: Let H(k) = k mod m, m= 10 Initially hash table will be


0 1 2 3 4 5 6 7 8 9

-1 -1 -1 -1 -1 -1 -1 -1 -1 -1

H(72) = 72 mod 10 = 2

0 1 2 3 4 5 6 7 8 9

-1 -1 72 -1 -1 -1 -1 -1 -1 -1

H(27) = 27 mod 10 = 7

0 1 2 3 4 5 6 7 8 9

-1 -1 72 -1 -1 -1 -1 27 -1 -1

H(36) = 36 mod 10 = 6

0 1 2 3 4 5 6 7 8 9

-1 -1 72 -1 -1 -1 36 27 -1 -1

H(24) = 24 mod 10 = 4

0 1 2 3 4 5 6 7 8 9

-1 -1 72 -1 24 -1 36 27 -1 -1

H(63) = 63 mod 10 =3

0 1 2 3 4 5 6 7 8 9

-1 -1 72 63 24 -1 36 27 -1 -1

H(81) = 81 mod 10 =1

0 1 2 3 4 5 6 7 8 9

-1 81 72 63 24 -1 36 27 -1 -1

H(92) = 92 mod 10 =2

Collision occurred since 2 is already filled. So go to next position – 3, which is also already filled, go to next
position – 4 which is also already filled. So go to 5 – which is not filled – so insert the key 92 in position 5.

0 1 2 3 4 5 6 7 8 9

-1 81 72 63 24 92 36 27 -1 -1
H(101) = 101 mod 10 = 1

Collision occurred since 1 is already filled. Do linear probing and the next position free is 8, so insert key
101 in position 8.

0 1 2 3 4 5 6 7 8 9

-1 81 72 63 24 92 36 27 101 -1

Home Actual Search


Key
address Address length

81 1 1 1

72 2 2 1

63 3 3 1

24 4 4 1

92 2 5 4

36 6 6 1

27 7 7 1

101 1 8 8

Average search length = (1+1+1+1+4+1+1+8)/ 8 = 2.25

Quadratic Probing

 Quadratic probing uses a quadratic function of i as the increment

 Suppose a record R with key k has the hash address H(k)=h then instead of searching
the locations with h, h+1, h+2………. we linearly search locations with h, h+1, h+4,
h+9, ......... h+i2

 quadratic probing uses a skip consisting of successive perfect squares.


Double Hashing

 In double hashing, we use two hash functions rather than a single function.

 Double hashing uses the idea of applying a second hash function to the key when a
collision occurs.

 There are a couple of requirements for the second function:

 it must never evaluate to 0

 must make sure that all cells can be probed

 A popular second hash function is:

Hash2(key) = M - (key % M)

 where M is a prime number that is smaller than the size of the table. But any independent
hash function may also be used.

 Example: 37,90,45,22,17,49,55

H1(key)=key%10
H2(key)=7-(key%7)

After collision (H1(key)+1*H2(key))


%10
90

17

22

45

55

37

49

Rehashing

 When the hash table becomes nearly full, the number of collisions increases. In such cases,
a better option is to create a new hash table with size double of the original hash table.

 All the entries in the original hash table will then have to be moved to the new hash table.

 This is done by taking each entry, computing its new hash value, and then inserting it in the new
hash table.

 Example: Consider the hash table of size 5 given below. The hash function used is h(x) = x % 5.
Rehash the entries into to a new hash table.
Chaining

 Chaining technique avoids collision using an array of linked lists.

 If more than one key has same hash value, then all the keys will be inserted at the end of the list
(insert rear) one by one and thus collision is avoided.

 Example: Construct a hash table of size and store the following words: like, a, tree, you, first, place,
to

 Let H(str)=P0+P1+P2+……+Pn-1; where Pi is position of letter in English alphabet series.

 Then calculate the hash address = Sum % 5

H(like) = 12 + 9 + 11 + 5 = 37 % 5 = 2

H(a) = 1 %5 =1

H(tree) = 20 + 18 + 5 + 5 = 48 % 5 = 3

1 H(you) = 25 + 15 + 21 = 61 % 5 = 1

H(first) = 6 + 9 + 18 + 19 + 20 = 72 % 5 = 2

H(place) = 16 + 12 + 1 + 3 + 5 = 37 % 5 =2

H(to) = 20 + 15 = 35 % 5 =
Types of Hashing

Static Hashing:

 Is a hashing technique in which the table(bucket) size remains the same (Fixed during compilation
time) is called static hashing.

 Various techniques of static hashing are linear probing and chaining

 As the size is fixed, this type of hashing consists handling overflow of elements (Collision) efficiently.

Drawbacks of static hashing

 Table size is fixed and hence cannot accommodate data growth.

 Collisions increases as data size grows.

Dynamic Hashing:

 Dynamically increases the size of the hash table as collision occurs. There are two types:

Dynamic hashing using directory or (Extendible hashing):

 uses a directory that grows or shrinks depending on the data distribution.

 No overflow buckets.

 Directory size d= 2t where t is the number of bits used to identify all h(k).

 Uses a directory of pointers to buckets/bins which are collections of records

 The number of buckets are doubled by doubling the directory.


 Directory much smaller than file, so doubling it is much cheaper.

Directory less Dynamic hashing or (Linear hashing):

 Pages are split when overflows occur

 Directory avoided in Linear hashing by using overflow pages. (chaining approach)

 Splitting occurs in turn, in a round robin fashion.one by one from the first bucket to the last bucket.

 When all the pages at one level (the current hash function) have been split, a new level is applied.

 Example: Insert in Order using linear hashing: 1,7,3,8,12,4,11,2,10


 After insertion till 12:

 When 4 inserted overflow occurred. So, we split the bucket and increment pointer.

 So we split bucket 0 and rehashed all keys in it. Placed 3 to new bucket as h1 (3 mod 6 = 3 ) and (12
mod 6 = 0 ). Then 11 and 2 are inserted. And now overflow. s is pointing to bucket 1, hence split
bucket 1 by re- hashing it.

Priority Queues

 Priority queues are data structures that store elements along with their associated priorities.

 The elements in a priority queue are typically retrieved in order of their priority

Representation of a Priority Queue

One-Way List Representation of a Priority Queue:

 One way to maintain a priority queue in memory is by means of a one-way list, as follows:

 Each node in the list will contain three items of information: an information field INFO, a priority
number PRN and a link number LINK.
 A node X precedes a node Y in the list

When X has higher priority than Y

When both have the same priority but X was added to the list before Y.

Array Representation of a Priority Queue:

 Another way to maintain a priority queue in memory is to use a separate queue for each level of
priority (or for each priority number).

 Each such queue must have its own pair of pointers, FRONT and REA R.

 FRONT[K] and REAR[K] contain, respectively, the front and rear elements of row K of
QUEUE, the row that maintains the queue of elements with priority number K.

Single-Ended Priority Queue:

 In a single-ended priority queue, elements are inserted with their priorities, and retrieval or deletion
is performed on the element with the highest (or lowest) priority.

 This type of priority queue is often implemented using a heap data structure, such as a binary heap or a
Fibonacci heap.
Operation:

 SP1: Return an element with minimum priority.

 SP2: Insert an element with an arbitrary priority.

 SP3: Delete an element with minimum priority.

Double-Ended Priority Queue

 Also known as a dequeue (pronounced "deck"), a double-ended priority queue allows elements to be
inserted and removed from both ends, considering both minimum and maximum priorities.

 Double-ended priority queues can be implemented using various data structures, such as doubly-linked
lists, skip lists, or binary heaps

Operations:

 DP1: Return an element with minimum priority.

 DP2: Return an element with maximum priority.

 DP3: Insert an element with an arbitrary priority.

 DP4: Delete an element with minimum priority.

 DP5: Delete an element with maximum priority.

Leftist Trees

 A leftist tree, also known as a leftist heap, is a type of binary heap data structure used
for implementing priority queues.

 Like other heap data structures, it is a complete binary tree, meaning that all levels are fully filled
except possibly the last level, which is filled from left to right.

 Let X be a node in an extended binary tree. Let left-child (x) and right-child (x), respectively, denote
the left and right children of the internal node x.

 Define shortest (x) to be the length of a shortest path from x to an external node. It is easy to see that
shortest (x) satisfies the following recurrence:
Height-Biased Leftist Trees.
Operations:

 The main operation is merge().

 deleteMin() or extractMin() can be done by removing root and calling merge() for left and right
subtrees.

 insert() can be done be create a leftist tree with single key (key to be inserted) and calling merge() for
given tree and tree with single node.

Detailed Steps for Merge:

1. Compare the roots of two heaps.

2. Push the smaller key into an empty stack, and move to the right child of smaller key.

3. Recursively compare two keys and go on pushing the smaller key onto the stack and move to its right
child.

4. Repeat until a null node is reached.

5. Take the last node processed and make it the right child of the node at top of the stack, and convert it
to leftist heap if the properties of leftist heap are violated.

6. Recursively go on popping the elements from the stack and making them the right child of new stack
top.

Example:
Weight-Biased Leftist Trees.

BCS304 CEC Page 110


Optimal Binary Search Tree:

 An Optimal Binary Search Tree (OBST), also known as a Weighted Binary Search Tree, is a
binary search tree that minimizes the expected search cost.

 In a binary search tree, the search cost is the number of comparisons required to search for a
given key.

 In an OBST, each node is assigned a weight that represents the probability of the key being
searched for.

 The sum of all the weights in the tree is 1.0.

 The expected search cost of a node is the sum of the product of its depth and weight, and the
expected search cost of its children.

 Input: keys[] = {10, 12}, freq[] = {34, 50}

 There can be following two possible BSTs

10 12

\ /

12 10

I II

 Frequency of searches of 10 and 12 are 34 and 50 respectively.

 The cost of tree I is 34*1 + 50*2 = 134

 The cost of tree II is 50*1 + 34*2 = 118

 Input: keys[] = {10, 12, 20}, freq[] = {34, 8, 50}

 There can be following possible BSTs (Write on own)

 Among all possible BSTs, cost of the fifth BST is minimum. Cost of the fifth BST is 1*50 + 2*34
+ 3*8 = 142

BCS304 CEC Page 111


BCS304 CEC Page 112

You might also like