Data Structure - CS3301 - Notes
Data Structure - CS3301 - Notes
CSE
Home Mech
e
EEE
ECE
Physics
Basic for Engineering
Electrical and Data Structure
Problem Solving and Science Engineering
Electronics
Python Programming Object Oriented
Programming in C
Programming
Elective-Management
Professional Elective II
Professional Elective IV
www.Poriyaan.in
1 CS3301 - DATASTRUCTURES
PRATHYUSHA
ENGINEERING COLLEGE
DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING
CS3301 - DATASTRUCTURES
(REGULATION R2021 – III SEMESTER)
https://play.google.com/store/apps/details?id=com.poriyaan.poriyaan
www.Poriyaan.in
2 CS3301 - DATASTRUCTURES
UNIT I LISTS 9
Abstract Data Types (ADTs) – List ADT – Array-based implementation – Linked list
implementation – Singly linked lists – Circularly linked lists – Doubly-linked lists –
Applications of lists – Polynomial ADT – Radix Sort – Multilists.
https://play.google.com/store/apps/details?id=com.poriyaan.poriyaan
www.Poriyaan.in
3 CS3301 - DATASTRUCTURES
TOTAL: 45 PERIODS
OUTCOMES:
At the end of the course, the student should be able to:
• Define linear and non-linear data structures.
• Implement linear and non–linear data structure operations.
• Use appropriate linear/non–linear data structure operations for solving a given
problem.
• Apply appropriate graph algorithms for graph applications.
• Analyze the various searching and sorting algorithms.
TEXT BOOKS:
1. Mark Allen Weiss, “Data Structures and Algorithm Analysis in C”, 2nd Edition,
Pearson Education,1997.
2. Kamthane, Introduction to Data Structures in C, 1st Edition, Pearson Education, 2007
REFERENCES:
1. Langsam, Augenstein and Tanenbaum, Data Structures Using C and C++, 2nd Edition,
Pearson Education, 2015.
2. Thomas H. Cormen, Charles E. Leiserson, Ronald L.Rivest, Clifford Stein,
“Introduction to Algorithms”, Second Edition, Mcgraw Hill, 2002.
3. Alfred V. Aho, Jeffrey D. Ullman,John E. Hopcroft ,Data Structures and Algorithms,
1st edition, Pearson, 2002.
4. Kruse, Data Structures and Program Design in C, 2nd Edition, Pearson Education,
2006.
https://play.google.com/store/apps/details?id=com.poriyaan.poriyaan
www.Poriyaan.in
4 CS3301 - DATASTRUCTURES
UNIT I LISTS 9
Abstract Data Types (ADTs) – List ADT – Array-based implementation – Linked list
implementation – Singly linked lists – Circularly linked lists – Doubly-linked lists –
Applications of lists – Polynomial ADT – Radix Sort – Multilists.
https://play.google.com/store/apps/details?id=com.poriyaan.poriyaan
www.Poriyaan.in
5 CS3301 - DATASTRUCTURES
2. Insert(x,3) might make the list into 34, 12, 52, x, 16, 12 (if we insert
after the position given);
3. Delete (3) might turn that list into 34, 12, x, 16, 12.
IMPLEMENTATION OF THE LIST
1. Array implementation
2. Linked list implementation
Simple Array Implementation of Lists
Normally array is static allocation which causes more wastage of memory.
Even if the array is dynamically allocated, an estimate of the maximum size of
the list is required. Usually this requires a high over-estimate, which wastes
considerable space. This could be a serious limitation, especially if there are many
lists of unknown size.
https://play.google.com/store/apps/details?id=com.poriyaan.poriyaan
www.Poriyaan.in
6 CS3301 - DATASTRUCTURES
https://play.google.com/store/apps/details?id=com.poriyaan.poriyaan
www.Poriyaan.in
7 CS3301 - DATASTRUCTURES
https://play.google.com/store/apps/details?id=com.poriyaan.poriyaan
www.Poriyaan.in
8 CS3301 - DATASTRUCTURES
https://play.google.com/store/apps/details?id=com.poriyaan.poriyaan
www.Poriyaan.in
9 CS3301 - DATASTRUCTURES
https://play.google.com/store/apps/details?id=com.poriyaan.poriyaan
www.Poriyaan.in
10 CS3301 - DATASTRUCTURES
https://play.google.com/store/apps/details?id=com.poriyaan.poriyaan
www.Poriyaan.in
11 CS3301 - DATASTRUCTURES
https://play.google.com/store/apps/details?id=com.poriyaan.poriyaan
www.Poriyaan.in
12 CS3301 - DATASTRUCTURES
https://play.google.com/store/apps/details?id=com.poriyaan.poriyaan
www.Poriyaan.in
13 CS3301 - DATASTRUCTURES
Programming Details
• First,It is difficult to insert at the front of the list from the list given.
• Second, deleting from the front of the list is a special case, because it changes
the start of the list;
• A third problem concerns deletion in general. Although the pointer moves
above are simple, the deletion algorithm requires us to keep track of the cell
before the one that we want to delete.
In order to solve all three problems, we will keep a sentinel node, which is called as a
header or dummy node. (a header node contains the address of the first node in
the linked list)
Linked list with a header
The above figure shows a linked list with a header representing the list a1, a2, . . . , a5.
To avoid the problems associated with deletions, we need to write a routine
find_previous, which will return the position of the predecessor of the cell we wish to
delete.
If we use a header, then if we wish to delete the first element in the list, find_previous
will return the position of the header.
https://play.google.com/store/apps/details?id=com.poriyaan.poriyaan
www.Poriyaan.in
14 CS3301 - DATASTRUCTURES
https://play.google.com/store/apps/details?id=com.poriyaan.poriyaan
www.Poriyaan.in
15 CS3301 - DATASTRUCTURES
https://play.google.com/store/apps/details?id=com.poriyaan.poriyaan
www.Poriyaan.in
16 CS3301 - DATASTRUCTURES
https://play.google.com/store/apps/details?id=com.poriyaan.poriyaan
www.Poriyaan.in
17 CS3301 - DATASTRUCTURES
L->next = NULL;
while( p != NULL )
{
tmp = p->next;
free( p );
p = tmp;
} }
Doubly Linked Lists
A linked list is called as doubly when it has two pointers namely forward and
backward pointers. It is convenient to traverse lists both forward and backwards.
An extra field in the data structure, containing a pointer to the previous cell;
The cost of this is an extra link, which adds to the space requirement and also doubles
the cost of insertions and deletions because there are more pointers to fix.
Node
Structure declaration
struct node
{
int Element;
struct node *FLINK;
struct node *BLINK;
}
https://play.google.com/store/apps/details?id=com.poriyaan.poriyaan
www.Poriyaan.in
18 CS3301 - DATASTRUCTURES
Insertion
Insert(15,L,P)
Deletion:
https://play.google.com/store/apps/details?id=com.poriyaan.poriyaan
www.Poriyaan.in
19 CS3301 - DATASTRUCTURES
Structure declaration:
struct node
{
int Element;
struct node *Next; }
Insert at beginning:
https://play.google.com/store/apps/details?id=com.poriyaan.poriyaan
www.Poriyaan.in
20 CS3301 - DATASTRUCTURES
Insert in middle:
void insert_mid(int X, List L, Position P)
https://play.google.com/store/apps/details?id=com.poriyaan.poriyaan
www.Poriyaan.in
21 CS3301 - DATASTRUCTURES
Insert at Last
https://play.google.com/store/apps/details?id=com.poriyaan.poriyaan
www.Poriyaan.in
22 CS3301 - DATASTRUCTURES
Deletion at middle
https://play.google.com/store/apps/details?id=com.poriyaan.poriyaan
www.Poriyaan.in
23 CS3301 - DATASTRUCTURES
Deletion at last:
Structure Declaration:
struct node
{
int Element;
struct node *FLINK;
struct node *BLINK;
}
https://play.google.com/store/apps/details?id=com.poriyaan.poriyaan
www.Poriyaan.in
24 CS3301 - DATASTRUCTURES
Insert at beginning:
Insert at Middle:
https://play.google.com/store/apps/details?id=com.poriyaan.poriyaan
www.Poriyaan.in
25 CS3301 - DATASTRUCTURES
Insert at Last:
https://play.google.com/store/apps/details?id=com.poriyaan.poriyaan
www.Poriyaan.in
26 CS3301 - DATASTRUCTURES
Deletion
Deleting First node
void dele_first(List L)
Deletion at middle:
void dele_mid (int X, List L)
{ Position P, Temp;
P=FindPrevious(X);
https://play.google.com/store/apps/details?id=com.poriyaan.poriyaan
www.Poriyaan.in
27 CS3301 - DATASTRUCTURES
https://play.google.com/store/apps/details?id=com.poriyaan.poriyaan
www.Poriyaan.in
28 CS3301 - DATASTRUCTURES
Example:
P1:4X10+5X5+3
P2:10X6-5X2+2X
struct link
{
int coeff;
int pow;
struct link *next;
};struct link *poly1=NULL,*poly2=NULL,*poly=NULL;
https://play.google.com/store/apps/details?id=com.poriyaan.poriyaan
www.Poriyaan.in
29 CS3301 - DATASTRUCTURES
poly->coeff=poly2->coeff;
poly2=poly2->next;
}
else
{
poly->pow=poly1->pow;
poly->coeff=poly1->coeff+poly2->coeff;
poly1=poly1->next;
poly2=poly2->next;
}
poly->next=(struct link *)malloc(sizeof(struct link));
poly=poly->next;
poly->next=NULL;
}
if(poly1->next != NULL)
{
poly->coeff = poly1->coeff;
poly->pow = poly1->pow;
poly->next=(struct link *)malloc(sizeof(struct link));
poly=poly->next;
poly->next=NULL;
}
else
{
poly->coeff = poly2->coeff;
poly->pow = poly2->pow;
poly->next=(struct link *)malloc(sizeof(struct link));
poly=poly->next;
poly->next=NULL;
}
}
https://play.google.com/store/apps/details?id=com.poriyaan.poriyaan
www.Poriyaan.in
30 CS3301 - DATASTRUCTURES
11 14 -3 10 2 8
14 14 14 14
2 8 10 6
SUBTRACTION OF TWO POLYNOMIAL
void sub ( )
0 1
{
poly *ptr1, *ptr2, *newnode;
ptr1 = list1 ;
ptr2 = list 2;
while (ptr1! = NULL && ptr2! = NULL)
{
newnode = malloc (sizeof (Struct poly));
if (ptr1 power = = ptr2 power)
{
newnode→coeff = (ptr1 coeff) - (ptr2 coeff);
newnode→power = ptr1 power;
newnode→next = NULL;
list3 = create (list 3, newnode);
ptr1 = ptr1→next;
https://play.google.com/store/apps/details?id=com.poriyaan.poriyaan
www.Poriyaan.in
31 CS3301 - DATASTRUCTURES
ptr2 = ptr2→next; }
else
{
if (ptr1→power > ptr2→power)
{
newnode→coeff = ptr1→coeff;
newnode→power = ptr1→power;
newnode→next = NULL;
list 3 = create (list 3, newnode);
ptr1 = ptr1→next; }
else
{
newnode→coeff = - (ptr2→coeff);
newnode→power = ptr2→power;
newnode→next = NULL;
list 3 = create (list 3, newnode);
ptr2 = ptr2 next; } } }
POLYNOMIAL DIFFERENTIATION
void diff ( )
{
poly *ptr1, *newnode;
ptr1 = list 1;
while (ptr1 ! = NULL)
{
newnode = malloc (sizeof (Struct poly));
newnode coeff = ptr1 coeff *ptr1 power;
newnode power = ptr1 power - 1;
newnode next = NULL;
list 3 = create (list 3, newnode);
ptr1 = ptr1→next; } }
https://play.google.com/store/apps/details?id=com.poriyaan.poriyaan
www.Poriyaan.in
32 CS3301 - DATASTRUCTURES
Radix Sort
A second example where linked lists are used is called radix sort. Radix sort is
also known as card sort. Because it was used, until the advent of modern computers, to
sort old-style punch cards.
If we have n integers in the range 1 to m (or 0 to m - 1) 9, we can use this
information to obtain a fast sort known as bucket sort. We keep an array called count,
of size m, which is initialized to zero. Thus, count has m cells (or buckets), which are
initially empty.
When ai is read, increment (by one) counts [ai]. After all the input is read, scan
the count array, printing out a representation of the sorted list. This algorithm takes
O(m + n); If m = (n), then bucket sort takes O(n) times.
The following example shows the action of radix sort on 10 numbers. The input
is 64, 8, 216, 512, 27, 729, 0, 1, 343, and 125. The first step (Pass 1) bucket sorts by
the least significant digit.. The buckets are as shown in below figure, so the list, sorted
by least significant digit, is 0, 1, 512, 343, 64, 125, 216, 27, 8, 729. These are now
sorted by the next least significant digit (the tens digit here)
Pass 2 gives output 0, 1, 8, 512, 216, 125, 27, 729, 343, 64. This list is now
sorted with respect to the two least significant digits. The final pass, shown in Figure
3.26, bucket-sorts by most significant digit.
The final list is 0, 1, 8, 27, 64, 125, 216, 343, 512, and 729.
The running time is O(p(n + b)) where p is the number of passes, n is the
number of elements to sort, and b is the number of buckets. In our case, b = n.
Buckets after first step of radix sort
0 1 512 343 64 125 216 27 8 729
0 1 2 3 4 5 6 7 8 9
Buckets after the second pass of radix sort
8 216 729 343 64
1 512 27
0 125
0 1 2 3 4 5 6 7 8 9
https://play.google.com/store/apps/details?id=com.poriyaan.poriyaan
www.Poriyaan.in
33 CS3301 - DATASTRUCTURES
Multilists
A university with 40,000 students and 2,500 courses needs to be able to
generate two types of reports. The first report lists the class registration for each class,
and the second report lists, by student, the classes that each student is registered for.
If we use a two-dimensional array, such an array would have 100 million entries. The
average student registers for about three courses, so only 120,000 of these entries, or
roughly 0.1 percent, would actually have meaningful data.
To avoid the wastage of memory, a linked list can be used. We can use two link list
one contains the students in the class. Another linked list contains the classes the
student is registered for.
All lists use a header and are circular. To list all of the students in class C3, we
start at C3 and traverse its list . The first cell belongs to student S1.
Multilist implementation for registration problem
https://play.google.com/store/apps/details?id=com.poriyaan.poriyaan
www.Poriyaan.in
34 CS3301 - DATASTRUCTURES
https://play.google.com/store/apps/details?id=com.poriyaan.poriyaan
www.Poriyaan.in
35 CS3301 - DATASTRUCTURES
The model depicted in above figure signifies that pushes are input operations
and pops and tops are output.
Stack model: only the top element is accessible
https://play.google.com/store/apps/details?id=com.poriyaan.poriyaan
www.Poriyaan.in
36 CS3301 - DATASTRUCTURES
Implementation of Stacks
A stack is a list, gives two popular implementations.
1. Array implementation
2. Linked list implementation
https://play.google.com/store/apps/details?id=com.poriyaan.poriyaan
www.Poriyaan.in
37 CS3301 - DATASTRUCTURES
struct node
{
Element_type element;
PtrToNode next;
};
Routine to test whether a stack is empty-linked list implementation
This routine checks whether Stack is empty or not. If it is not empty it will
return a pointer to the stack. Otherwise return NULL
int is_empty( STACK S )
{
return( S->next == NULL );
}
Routine to create an empty stack-linked list implementation
This routine creates a Stack and return a pointer of the stack. Otherwise return
a warning to say Stack is not created.
STACK create_stack( void )
{
STACK S;
S = malloc( sizeof( struct node ) );
if( S == NULL )
fatal_error("Out of space!!!");
return S; }
https://play.google.com/store/apps/details?id=com.poriyaan.poriyaan
www.Poriyaan.in
38 CS3301 - DATASTRUCTURES
https://play.google.com/store/apps/details?id=com.poriyaan.poriyaan
www.Poriyaan.in
39 CS3301 - DATASTRUCTURES
https://play.google.com/store/apps/details?id=com.poriyaan.poriyaan
www.Poriyaan.in
40 CS3301 - DATASTRUCTURES
https://play.google.com/store/apps/details?id=com.poriyaan.poriyaan
www.Poriyaan.in
41 CS3301 - DATASTRUCTURES
if( S == NULL )
fatal_error("Out of space!!!");
S->Array = malloc( sizeof( ElementType ) * MaxElements );
if( S->Array == NULL )
fatalerror("Out of space!!!");
S->Capacity = MaxElements;
MakeEmpty(S);
return( S ); }
Routine for freeing stack--array implementation
This routine frees or removes the Stack Structure itself by deleting the array
elements one by one.
Void dispose_stack( Stack S )
{
if( S != NULL )
{ free( S->Array );
free( S ); } }
Routine to test whether a stack is empty--array implementation
This routine is to check whether stack is empty or not.
int IsEmpty( Stack S )
{
return( S->top_of_stack == EmptyTOS ); }
Routine to create an empty stack--array implementation
This routine helps to make the Stack as empty one.
Void MakeEmpty( STACK S )
{
S->top_of_stack = EMPTY_TOS; }
Routine to push onto a stack--array implementation
This routine will insert the new elemnt onto the top of the stack using stack
pointer.
Void push( ElementType X, Stack S )
{ if( IsFull( S ) )
https://play.google.com/store/apps/details?id=com.poriyaan.poriyaan
www.Poriyaan.in
42 CS3301 - DATASTRUCTURES
Error("Full stack");
else
S->Array[ ++S->TopofStack ] = X; }
Routine to return top of stack--array implementation
This routine is to return the topmost element from the stack.
ElementType Top( Stack S )
{
if( !IsEmpty( S ) )
return S->Array[ S-> TopofStack];
error("Empty stack");
return 0;
}
Routine to pop from a stack--array implementation
This routine is to delete the topmost element from the stack.
Void pop( Stack S )
{
if( IsEmpty( S ) )
error("Empty stack");
else
S->TopofStack--;
}
Routine to give top element and pop a stack--array implementation
This routine is to return as well as remove the topmost element from the stack.
ElementType TopandPop( Stack S )
{
if( IsEmpty( S ) )
error("Empty stack");
else
return S->Array[ S->TopofStack-- ];
}
https://play.google.com/store/apps/details?id=com.poriyaan.poriyaan
www.Poriyaan.in
43 CS3301 - DATASTRUCTURES
Stack Applications
Stack is used for the following applications.
1. Reversing of the string
2. Tower’s of Hanoi’s problem
3. Balancing Symbols
4. Conversion of Infix to postfix expression
5. Conversion of Infix to prefix expression
6. Evaluation of Postfix expression
7. Used in Function calls
Balancing Symbols
Compilers check your programs for syntax errors, but frequently a lack of one
symbol (such as a missing brace or comment starter) will cause the compiler to
spill out a hundred lines of diagnostics without identifying the real error.
A useful tool in this situation is a program that checks whether everything is
balanced. Thus, every right brace, bracket, and parenthesis must correspond to
their left counterparts.
The sequence [()] is legal, but [(]) is wrong. That it is easy to check these things. For
simplicity, we will just check for balancing of parentheses, brackets, and braces and
ignore any other character that appears.
https://play.google.com/store/apps/details?id=com.poriyaan.poriyaan
www.Poriyaan.in
44 CS3301 - DATASTRUCTURES
Expression:
Expression is defined as a collection of operands and operators. The operators
can be arithmetic, logical or Boolean operators.
Rules for expression
✓ No two operand should be continuous
✓ No two operator should be continuous
Types of expression:
Based on the position of the operator, it is classified into three.
1. Infix Expression / Standard notation
2. Prefix Expression/ Polished notation
3. Postfix Expression / Reversed Polished notation
Infix Expression:
In an expression if the operator is placed in between the operands, then it is
called as Infix Expression.
Eg : A+B
Prefix Expression:
In an expression if the operator is placed before the operands, then it is called
as Prefix Expression.
Eg : +AB
Postfix Expression:
In an expression if the operator is placed after the operands, then it is called as
Postfix Expression.
Eg : AB+
https://play.google.com/store/apps/details?id=com.poriyaan.poriyaan
www.Poriyaan.in
45 CS3301 - DATASTRUCTURES
a+b*c+(d*e+f)*g.
A correct answer is a b c * + d e * f + g * +.
Algorithm:
1. We start with an initially empty stack
2. When an operand is read, it is immediately placed onto the output.
3. Operators are not immediately placed onto the output, so they must be saved
somewhere. The correct thing to do is to place operators that have been seen,
but not placed on the output, onto the stack. We will also stack left parentheses
when they are encountered.
4. If we see a right parenthesis, then we pop the stack, writing symbols until we
encounter a (corresponding) left parenthesis, which is popped but not output.
5. If we see any other symbol ('+','*', '(' ), then we pop entries from the stack until
we find an entry of lower priority. One exception is that we never remove a '('
from the stack except when processing a ')'. For the purposes of this operation,
'+' has lowest priority and '(' highest. When the popping is done, we push the
operand onto the stack.
6. Finally, if we read the end of input, we pop the stack until it is empty, writing
symbols onto the output.
To see how this algorithm performs, we will convert the infix expression into
its postfix form.
a + b * c + (d * e + f) * g
First, the symbol a is read, so it is passed through to the output. Then '+' is read and
pushed onto the stack. Next b is read and passed through to the output. Then the stack
will be as follows.
Next a '*' is read. The top entry on the operator stack has lower precedence than '*', so
nothing is output and '*' is put on the stack. Next, c is read and output.
https://play.google.com/store/apps/details?id=com.poriyaan.poriyaan
www.Poriyaan.in
46 CS3301 - DATASTRUCTURES
The next symbol is a '+'. Checking the stack, we find that we will pop a '*' and place it
on the output, pop the other '+', which is not of lower but equal priority, on the stack,
and then push the '+'.
The next symbol read is an '(', which, being of highest precedence, is placed on the
stack. Then d is read and output.
We continue by reading a '*'. Since open parentheses do not get removed except when
a closed parenthesis is being processed, there is no output. Next, e is read and output.
The next symbol read is a '+'. We pop and output '*' and then push '+'. Then we read
and output f.
.
Now we read a ')', so the stack is emptied back to the '('. We output a '+' 0nto the stack.
We read a '*' next; it is pushed onto the stack. Then g is read and output.
https://play.google.com/store/apps/details?id=com.poriyaan.poriyaan
www.Poriyaan.in
47 CS3301 - DATASTRUCTURES
The input is now empty, so we pop and output symbols from the stack until it is
empty.
As before, this conversion requires only O(n) time and works in one pass
through the input. We can add subtraction and division to this repertoire by assigning
subtraction and addition equal priority and multiplication and division equal priority.
A subtle point is that the expression a - b - c will be converted to ab - c- and not
abc - -. Our algorithm does the right thing, because these operators associate from left
to right. This is not necessarily the case in general, since exponentiation associates
right to left: 223 = 28 = 256 not 43 = 64.
Next a '+' is read, so 3 and 2 are popped from the stack and their sum, 5, is pushed.
TopofStack 5
5
6
https://play.google.com/store/apps/details?id=com.poriyaan.poriyaan
www.Poriyaan.in
48 CS3301 - DATASTRUCTURES
Next 8 is pushed.
TopofStack 8
5
5
6
Now a '*' is seen, so 8 and 5 are popped as 8 * 5 = 40 is pushed.
TopofStack 40
5
6
Next a '+' is seen, so 40 and 5 are popped and 40 + 5 = 45 is pushed.
TopofStack 45
6
Now, 3 is pushed.
TopofStack 3
45
6
Next '+' pops 3 and 45 and pushes 45 + 3 = 48.
TopofStack 48
6
Finally, a '*' is seen and 48 and 6 are popped, the result 6 * 48 = 288 is pushed.
TopofStack 288
https://play.google.com/store/apps/details?id=com.poriyaan.poriyaan
www.Poriyaan.in
49 CS3301 - DATASTRUCTURES
Function Calls
• When a call is made to a new function, all the variables local to the calling
routine need to be saved by the system. Otherwise the new function will
overwrite the calling routine's variables.
• The current location in the routine must be saved so that the new function
knows where to go after it is done.
• The reason that this problem is similar to balancing symbols is that a function
call and function return are essentially the same as an open parenthesis and
closed parenthesis, so the same ideas should work.
• When there is a function call, all the important information that needs to be
saved, such as register values (corresponding to variable names) and the return
address is saved "on a piece of paper" in an abstract way and put at the top of a
pile. Then the control is transferred to the new function, which is free to replace
the registers with its values.
• If it makes other function calls, it follows the same procedure. When the
function wants to return, it looks at the "paper" at the top of the pile and
restores all the registers. It then makes the return jump.
• The information saved is called either an activation record or stack frame.
• There is always the possibility that you will run out of stack space by having
too many simultaneously active functions. Running out of stack space is always
a fatal error.
• In normal events, you should not run out of stack space; doing so is usually an
indication of runaway recursion. On the other hand, some perfectly legal and
seemingly innocuous program can cause you to run out of stack space.
A bad use of recursion: printing a linked list
void /* Not using a header */
print_list( LIST L )
{ if( L != NULL )
{
print_element( L->element );
print_list( L->next ); } }
https://play.google.com/store/apps/details?id=com.poriyaan.poriyaan
www.Poriyaan.in
50 CS3301 - DATASTRUCTURES
• The above routine prints out a linked list, is perfectly legal and actually correct.
It properly handles the base case of an empty list, and the recursion is fine. This
program can be proven correct.
• Activation records are typically large because of all the information they
contain, so this program is likely to run out of stack space. This program is an
example of an extremely bad use of recursion known as tail recursion. Tail
recursion refers to a recursive call at the last line.
• Tail recursion can be mechanically eliminated by changing the recursive call
to a goto receded by one assignment per function argument.
• This simulates the recursive call because nothing needs to be saved -- after the
recursive call finishes, there is really no need to know the saved values.
Because of this, we can just go to the top of the function with the values that
would have been used in a recursive call.
The below program is the improved version. Removal of tail recursion is so simple
that some compilers do it automatically.
Printing a list without recursion
Void print_list( LIST L ) /* No header */
{
top:
if( L != NULL )
{
print_element( L->element );
L = L->next;
goto top;
}
}
Recursion can always be completely removed. But doing so can be quite tedious. The
non-recursive programs are generally faster than recursive programs; the speed
advantage rarely justifies the lack of clarity that results from removing the recursion.
https://play.google.com/store/apps/details?id=com.poriyaan.poriyaan
www.Poriyaan.in
51 CS3301 - DATASTRUCTURES
Queue Model
The basic operations on a queue are
1. enqueue, which inserts an element at the end of the list (called the rear)
2. dequeue, which deletes (and returns) the element at the start of the list
(known as the front).
Abstract model of a queue
• By the way, the cells that are blanks have undefined values in them. In
particular, the first two cells have elements that used to be in the queue.
https://play.google.com/store/apps/details?id=com.poriyaan.poriyaan
www.Poriyaan.in
52 CS3301 - DATASTRUCTURES
https://play.google.com/store/apps/details?id=com.poriyaan.poriyaan
www.Poriyaan.in
53 CS3301 - DATASTRUCTURES
https://play.google.com/store/apps/details?id=com.poriyaan.poriyaan
www.Poriyaan.in
54 CS3301 - DATASTRUCTURES
else
{
Q->Size++;
Q->Rear = succ( Q->Rear, Q );
Q->Array[ Q->Rear ] = x;
} }
Applications of Queues
The applications are,
1. When jobs are submitted to a printer, they are arranged in order of arrival. Then
jobs sent to a line printer are placed on a queue.
2. Lines at ticket counters are queues, because service is first-come first-served.
3. Another example concerns computer networks. There are many network setups
of personal computers in which the disk is attached to one machine, known as
the file server.
4. Users on other machines are given access to files on a first-come first-served
basis, so the data structure is a queue.
Circular Queue:
In Circular Queue, the insertion of a new element is performed at the very first
locations of the queue if the last location of the queue is full, in which the first
element comes after the last element.
Advantages:
It overcomes the problem of unutilized space in linear queue, when it is
implemented as arrays.
https://play.google.com/store/apps/details?id=com.poriyaan.poriyaan
www.Poriyaan.in
55 CS3301 - DATASTRUCTURES
To perform the insertion of the element to the queue, the position of the
element is calculated as rear= (rear+1) % queue_size and set Q[rear]=value.
Similarly the element deleted from the queue using front = (front + 1) %
queue_size.
Enqueue:
This routine insert the new element at rear position of the circular queue.
Dequeue:
This routine deletes the element from the front of the circular queue.
void CQ_dequeue( )
{
If(front==-1 && rear==-1)
Print(“Queue is empty”);
Else
{
Temp=CQueue[front];
If(front==rear)
Front=rear=-1;
Else
Front=(front+1)% maxsize;
}}
https://play.google.com/store/apps/details?id=com.poriyaan.poriyaan
www.Poriyaan.in
56 CS3301 - DATASTRUCTURES
Priority Queue:
In an priority queue, an element with high priority is served before an element
with lower priority.
If two elements with the same priority, they are served according to their order
in the queue.
Two types of priority Queue.
https://play.google.com/store/apps/details?id=com.poriyaan.poriyaan
www.Poriyaan.in
57 CS3301 - DATASTRUCTURES
https://play.google.com/store/apps/details?id=com.poriyaan.poriyaan
www.Poriyaan.in
58 CS3301 - DATASTRUCTURES
https://play.google.com/store/apps/details?id=com.poriyaan.poriyaan
www.Poriyaan.in
59 CS3301 - DATASTRUCTURES
https://play.google.com/store/apps/details?id=com.poriyaan.poriyaan
www.Poriyaan.in
60 CS3301 - DATASTRUCTURES
https://play.google.com/store/apps/details?id=com.poriyaan.poriyaan
www.Poriyaan.in
61 CS3301 - DATASTRUCTURES
TREES
Tree is a Non- Linear datastructure in which data are stored in a hierarchal manner. It is also
defined as a collection of nodes. The collection can be empty. Otherwise, a tree consists of a
distinguished node r, called the root, and zero or more (sub) trees T1, T2, . . . , Tk, each of
whose roots are connected by a directed edge to r.
The root of each subtree is said to be a child of r, and r is the parent of each subtree
root. A tree is a collection of n nodes, one of which is the root, and n - 1 edges. That there are
n - 1 edges follows from the fact that each edge connects some node to its parent and every
node except the root has one parent
Generic tree
A tree
Terms in Tree
In the tree above figure, the root is A.
✓ Node F has A as a parent and K, L, and M as children.
✓ Each node may have an arbitrary number of children, possibly zero.
https://play.google.com/store/apps/details?id=com.poriyaan.poriyaan
www.Poriyaan.in
62 CS3301 - DATASTRUCTURES
Eg:
https://play.google.com/store/apps/details?id=com.poriyaan.poriyaan
www.Poriyaan.in
63 CS3301 - DATASTRUCTURES
General Tree
In a tree, node can have any no of children. Then it is called as general Tree.
Eg:
Implementation of Trees
Tree can be implemented by two methods.
1. Array Implementation
2. Linked List implementation
Apart from these two methods, it can also be represented by First Child and
Next sibling Representation.
One way to implement a tree would be to have in each node, besides its data, a pointer
to each child of the node. However, since the number of children per node can vary so greatly
and is not known in advance, it might be infeasible to make the children direct links in the
data structure, because there would be too much wasted space. The solution is simple: Keep
the children of each node in a linked list of tree nodes.
Node declarations for trees
typedef struct tree_node *tree_ptr;
struct tree_node
{
element_type element;
tree_ptr first_child;
tree_ptr next_sibling;
};
https://play.google.com/store/apps/details?id=com.poriyaan.poriyaan
www.Poriyaan.in
64 CS3301 - DATASTRUCTURES
First child/next sibling representation of the tree shown in the below Figure
Arrows that point downward are first_child pointers. Arrows that go left to right are
next_sibling pointers. Null pointers are not drawn, because there are too many. In the above
tree, node E has both a pointer to a sibling (F) and a pointer to a child (I), while some nodes
have neither.
Tree Traversals
Visiting of each and every node in a tree exactly only once is called as Tree
traversals. Here Left subtree and right subtree are traversed recursively.
Types of Tree Traversal:
1. Inorder Traversal
2. Preorder Traversal
3. Postorder Traversal
Inorder traversal:
Rules:
• Traverse Left subtree recursively
• Process the node
• Traverse Right subtree recursively
Eg
Inorder traversal: a + b*c + d*e + f*g.
Preorder traversal:
Rules:
• Process the node
• Traverse Left subtree recursively
• Traverse Right subtree recursively
Preorder traversal: ++a*b c*+*d e f g
https://play.google.com/store/apps/details?id=com.poriyaan.poriyaan
www.Poriyaan.in
65 CS3301 - DATASTRUCTURES
Postorder traversal:
Rules:
• Traverse Left subtree recursively
• Traverse Right subtree recursively
• Process the node
Postorder traversal: a b c*+de*f + g* +
✓ The root of this directory is /usr. (The asterisk next to the name indicates that /usr is
itself a directory.)
✓ /usr has three children, mark, alex, and bill, which are themselves directories. Thus,
/usr contains three directories and no regular files.
✓ The filename /usr/mark/book/ch1.r is obtained by following the leftmost child three
times. Each / after the first indicates an edge; the result is the full pathname.
✓ Two files in different directories can share the same name, because they must have
different paths from the root and thus have different pathnames.
https://play.google.com/store/apps/details?id=com.poriyaan.poriyaan
www.Poriyaan.in
66 CS3301 - DATASTRUCTURES
✓ A directory in the UNIX file system is just a file with a list of all its children, so the
directories are structured almost exactly in accordance with the type declaration.
✓ Each directory in the UNIX file system also has one entry that points to itself and
another entry that point to the parent of the directory. Thus, technically, the UNIX file
system is not a tree, but is treelike.
Routine to list a directory in a hierarchical file system void
list_directory ( Directory_or_file D )
{
list_dir ( D, 0 ); }
Void list_dir ( Directory_or_file D, unsigned int depth )
{
if ( D is a legitimate entry)
{
print_name ( depth, D );
if( D is a directory )
for each child, c, of D
list_dir( c, depth+1 );
} }
The logic of the algorithm is as follow.
✓ The argument to list_dir is some sort of pointer into the tree. As long as the pointer is
valid, the name implied by the pointer is printed out with the appropriate number of
tabs.
✓ If the entry is a directory, then we process all children recursively, one by one. These
children are one level deeper, and thus need to be indenting an extra space.
This traversal strategy is known as a preorder traversal. In a preorder traversal, work at a
node is performed before (pre) its children are processed. If there are n file names to be
output, then the running time is O (n).
The (preorder) directory listing
/usr
mark
book
chr1.c
chr2.c
chr3.c
https://play.google.com/store/apps/details?id=com.poriyaan.poriyaan
www.Poriyaan.in
67 CS3301 - DATASTRUCTURES
course
cop3530
fall88
syl.r
spr89
syl.r
sum89
syl.r
junk.c
alex
junk.c
bill
work
course
cop3212
fall88
grades
prog1.r
prog2.r
fall89
prog1.r
prog2.r
grades
https://play.google.com/store/apps/details?id=com.poriyaan.poriyaan
www.Poriyaan.in
68 CS3301 - DATASTRUCTURES
https://play.google.com/store/apps/details?id=com.poriyaan.poriyaan
www.Poriyaan.in
69 CS3301 - DATASTRUCTURES
fall88 9
prog2.r 2
prog1.r 7
grades 9
fall89 19
cop3212 29
course 30
bill 32
/usr 72
Figure shows that a binary tree consists of a root and two subtrees, Tl and
Tr, both of which could possibly be empty.
Worst-case binary tree
Implementation
A binary tree has at most two children; we can keep direct pointers to them. The
declaration of tree nodes is similar in structure to that for doubly linked lists, in that a node is
a structure consisting of the key information plus two pointers (left and right) to other nodes.
https://play.google.com/store/apps/details?id=com.poriyaan.poriyaan
www.Poriyaan.in
70 CS3301 - DATASTRUCTURES
Expression Trees
When an expression is represented in a binary tree, then it is called as an expression Tree.
The leaves of an expression tree are operands, such as constants or variable names, and the
other nodes contain operators. It is possible for nodes to have more than two children. It is
also possible for a node to have only one child, as is the case with the unary minus operator.
We can evaluate an expression tree, T, by applying the operator at the root to the
values obtained by recursively evaluating the left and right subtrees.
In our example, the left subtree evaluates to a + (b * c) and the right subtree evaluates
to ((d *e) + f ) *g. The entire tree therefore represents (a + (b*c)) + (((d * e) + f)* g).
We can produce an (overly parenthesized) infix expression by recursively
producing a parenthesized left expression, then printing out the operator at the
root, and finally recursively producing a parenthesized right expression. This
general strattegy ( left, node, right ) is known as an inorder traversal; it gives Infix
Expression.
An alternate traversal strategy is to recursively print out the left subtree, the
right subtree, and then the operator. If we apply this strategy to our tree above, the output is a
b c * + d e * f + g * +, which is called as postfix Expression. This traversal strategy is
generally known as a postorder traversal.
A third traversal strategy is to print out the operator first and then recursively print out
the left and right subtrees. The resulting expression, + + a * b c * + * d e f g, is the less useful
prefix notation and the traversal strategy is a preorder traversal
Expression tree for (a + b * c) + ((d * e + f ) * g)
https://play.google.com/store/apps/details?id=com.poriyaan.poriyaan
www.Poriyaan.in
71 CS3301 - DATASTRUCTURES
The first two symbols are operands, so we create one-node trees and push pointers to
them onto a stack.
Next, a '+' is read, so two pointers to trees are popped, a new tree is formed, and a pointer to it
is pushed onto the stack.
https://play.google.com/store/apps/details?id=com.poriyaan.poriyaan
www.Poriyaan.in
72 CS3301 - DATASTRUCTURES
Next, c, d, and e are read, and for each a one-node tree is created and a pointer
to the corresponding tree is pushed onto the stack.
Now a '+' is read, so two trees are merged. Continuing, a '*' is read, so we pop two tree
pointers and form a new tree with a '*' as root.
Finally, the last symbol is read, two trees are merged, and a pointer to the final
tree is left on the stack.
https://play.google.com/store/apps/details?id=com.poriyaan.poriyaan
www.Poriyaan.in
73 CS3301 - DATASTRUCTURES
In the above figure, the tree on the left is a binary search tree, but the tree on the right
is not. The tree on the right has a node with key 7 in the left subtree of a node with key 6.
The average depth of a binary search tree is O(log n).
Binary search tree declarations
typedef struct tree_node *tree_ptr;
struct tree_node
{
element_type element;
tree_ptr left;
tree_ptr right;
};
typedef tree_ptr SEARCH_TREE;
Make Empty:
This operation is mainly for initialization. Some programmers prefer to initialize the
first element as a one-node tree, but our implementation follows the recursive definition of
trees more closely.
Find
This operation generally requires returning a pointer to the node in tree T that has key
x, or NULL if there is no such node. The structure of the tree makes this simple. If T is , then
we can just return . Otherwise, if the key stored at T is x, we can return T. Otherwise, we
make a recursive call on a subtree of T, either left or right, depending on the relationship of x
to the key stored in T.
Routine to make an empty tree
SearchTree makeempty (search tree T)
{
if(T!=NULL)
{
Makeempty (T->left);
Makeempty (T->Right);
https://play.google.com/store/apps/details?id=com.poriyaan.poriyaan
www.Poriyaan.in
74 CS3301 - DATASTRUCTURES
Free( T);
}
return NULL; }
Routine for Find operation
Position find( Elementtype X, SearchTree T )
{
if( T == NULL )
return NULL;
if( x < T->element )
return( find( x, T->left ) );
else
if( x > T->element )
return( find( x, T->right ) );
else
return T;
}
FindMin & FindMax:
These routines return the position of the smallest and largest elements in the
tree, respectively.
To perform a findmin, start at the root and go left as long as there is a left child. The
stopping point is the smallest element.
The findmax routine is the same, except that branching is to the right child.
https://play.google.com/store/apps/details?id=com.poriyaan.poriyaan
www.Poriyaan.in
75 CS3301 - DATASTRUCTURES
https://play.google.com/store/apps/details?id=com.poriyaan.poriyaan
www.Poriyaan.in
76 CS3301 - DATASTRUCTURES
To insert 5, we traverse the tree as though a find were occurring. At the node with key
4, we need to go right, but there is no subtree, so 5 is not in the tree, and this is the correct
spot.
Insertion routine
Since T points to the root of the tree, and the root changes on the first insertion, insert
is written as a function that returns a pointer to the root of the new tree.
https://play.google.com/store/apps/details?id=com.poriyaan.poriyaan
www.Poriyaan.in
77 CS3301 - DATASTRUCTURES
Delete
Once we have found the node to be deleted, we need to consider several possibilities.
If the node is a leaf, it can be deleted immediately.
If the node has one child, the node can be deleted after its parent adjusts a pointer to
bypass the node
if a node with two children. The general strategy is to replace the key of this node
with the smallest key of the right subtree and recursively delete that node. Because the
smallest node in the right subtree cannot have a left child, the second
delete is an easy one.
The node to be deleted is the left child of the root; the key value is 2. It is replaced
with the smallest key in its right subtree (3), and then that node is deleted as before.
Deletion of a node (4) with one child, before and after
https://play.google.com/store/apps/details?id=com.poriyaan.poriyaan
www.Poriyaan.in
78 CS3301 - DATASTRUCTURES
https://play.google.com/store/apps/details?id=com.poriyaan.poriyaan
www.Poriyaan.in
79 CS3301 - DATASTRUCTURES
AVL Trees
The balance condition and allow the tree to be arbitrarily deep, but after every
operation, a restructuring rule is applied that tends to make future operations efficient. These
types of data structures are generally classified as self-adjusting.
An AVL tree is identical to a binary search tree, except that for every node in the
tree, the height of the left and right subtrees can differ by at most 1. (The height of an empty
tree is defined to be -1.)
An AVL (Adelson-Velskii and Landis) tree is a binary search tree with a balance
condition. The simplest idea is to require that the left and right subtrees have the same height.
The balance condition must be easy to maintain, and it ensures that the depth of the tree is
O(log n).
The above figure shows, a bad binary tree. Requiring balance at the root is not enough.
In Figure, the tree on the left is an AVL tree, but the tree on the right is not.
Thus, all the tree operations can be performed in O(log n) time, except possibly insertion.
When we do an insertion, we need to update all the balancing information for the
nodes on the path back to the root, but the reason that insertion is difficult is that inserting a
node could violate the AVL tree property.
Inserting a node into the AVL tree would destroy the balance condition.
Let us call the unbalanced node α. Violation due to insertion might occur in four
cases:
1. An insertion into the left subtree of the left child of α
2. An insertion into the right subtree of the left child of α
3. An insertion into the left subtree of the right child of α
4. An insertion into the right subtree of the right child of α
https://play.google.com/store/apps/details?id=com.poriyaan.poriyaan
www.Poriyaan.in
80 CS3301 - DATASTRUCTURES
The two trees in the above Figure contain the same elements and are both binary
search trees.
First of all, in both trees k1 < k2. Second, all elements in the subtree X are smaller
than k1 in both trees. Third, all elements in subtree Z are larger than k2. Finally, all elements
in subtree Y are in between k1 and k2. The conversion of one of the above trees to the other
is known as a rotation.
In an AVL tree, if an insertion causes some node in an AVL tree to lose the balance
property: Do a rotation at that node.
The basic algorithm is to start at the node inserted and travel up the tree, updating the
balance information at every node on the path.
In the above figure, after the insertion of the in the original AVL tree on the left, node 8
becomes unbalanced. Thus, we do a single rotation between 7 and 8, obtaining the tree on the
right.
https://play.google.com/store/apps/details?id=com.poriyaan.poriyaan
www.Poriyaan.in
81 CS3301 - DATASTRUCTURES
Routine :
Static position Singlerotatewithleft( Position K2)
{
Position k1;
K1=k2->left;
K2->left=k1->right;
K1->right=k2;
K2->height=max(height(k2->left),height(k2->right));
K1->height=max(height(k1->left),k2->height);
Return k1;
}
A dashed line indicates the two nodes that are the subject of the rotation. Next, we insert the
key 4, which causes no problems, but the insertion of 5 creates a violation at node 3, which is
fixed by a single rotation.
Next, we insert 6. This causes a balance problem for the root, since its left subtree is
of height 0, and its right subtree would be height 2. Therefore, we perform a single rotation at
the root between 2 and 4.
The rotation is performed by making 2 a child of 4 and making 4's original left subtree
the new right subtree of 2. Every key in this subtree must lie between 2 and 4, so this
transformation makes sense. The next key we insert is 7, which causes another rotation.
https://play.google.com/store/apps/details?id=com.poriyaan.poriyaan
www.Poriyaan.in
82 CS3301 - DATASTRUCTURES
Routine :
Static position Singlerotatewithright( Position K1)
{
Position k2;
K2=k1->right;
K1->right=k2->left;
K2->left=k1;
K1->height=max(height(k1->left),height(k1->right));
K2->height=max(height(k2->left),k1->height);
Return k2;
}
https://play.google.com/store/apps/details?id=com.poriyaan.poriyaan
www.Poriyaan.in
83 CS3301 - DATASTRUCTURES
Double Rotation
(Right-left) double rotation
In the above diagram, suppose we insert keys 8 through 15 in reverse order. Inserting 15 is
easy, since it does not destroy the balance property, but inserting 14 causes a height
imbalance at node 7.
As the diagram shows, the single rotation has not fixed the height imbalance. The problem is
that the height imbalance was caused by a node inserted into the tree containing the middle
elements (tree Y in Fig. (Right-left) double rotation) at the same time as the other trees had
identical heights. This process is called as double rotation, which is similar to a single
rotation but involves four subtrees instead of three.
https://play.google.com/store/apps/details?id=com.poriyaan.poriyaan
www.Poriyaan.in
84 CS3301 - DATASTRUCTURES
https://play.google.com/store/apps/details?id=com.poriyaan.poriyaan
www.Poriyaan.in
85 CS3301 - DATASTRUCTURES
In our example, the double rotation is a right-left double rotation and involves 7, 15,
and 14. Here, k3 is the node with key 7, k1 is the node with key 15, and
k2 is the node with key 14.
Next we insert 13, which require a double rotation. Here the double rotation is again a
right-left double rotation that will involve 6, 14, and 7 and will restore the tree. In this case,
k3 is the node with key 6, k1 is the node with key 14, and k2 is the node with key 7. Subtree
A is the tree rooted at the node with key 5, subtree B is the empty subtree that was originally
the left child of the node with key 7, subtree C is the tree rooted at the node with key 13, and
finally, subtree D is the tree rooted at the node with key 15.
If 12 is now inserted, there is an imbalance at the root. Since 12 is not between
4 and 7, we know that the single rotation will work. Insertion of 11 will require a single
rotation:
To insert 10, a single rotation needs to be performed, and the same is true for the
subsequent insertion of 9. We insert 8 without a rotation, creating the almost perfectly
balanced tree.
https://play.google.com/store/apps/details?id=com.poriyaan.poriyaan
www.Poriyaan.in
86 CS3301 - DATASTRUCTURES
https://play.google.com/store/apps/details?id=com.poriyaan.poriyaan
www.Poriyaan.in
87 CS3301 - DATASTRUCTURES
https://play.google.com/store/apps/details?id=com.poriyaan.poriyaan
www.Poriyaan.in
88 CS3301 - DATASTRUCTURES
https://play.google.com/store/apps/details?id=com.poriyaan.poriyaan
www.Poriyaan.in
89 CS3301 - DATASTRUCTURES
A complete binary tree of height h has between 2h and 2h+1 - 1 nodes. This implies
that the height of a complete binary tree is log n, which is clearly O(log n).
https://play.google.com/store/apps/details?id=com.poriyaan.poriyaan
www.Poriyaan.in
90 CS3301 - DATASTRUCTURES
The only problem with this implementation is that an estimate of the maximum heap
size is required in advance.
Types of Binary Heap
Min Heap
A binary heap is said to be Min heap such that any node x in the heap, the key value
of X is smaller than all of its descendants children.
Max Heap
A binary heap is said to be Min heap such that any node x in the heap, the key
value of X is larger than all of its descendants children.
It is easy to find the minimum quickly, it makes sense that the smallest element
should be at the root. If we consider that any subtree should also be a heap, then any node
should be smaller than all of its descendants.
Applying this logic, we arrive at the heap order property. In a heap, for every node X,
the key in the parent of X is smaller than (or equal to) the key in X.
Similarly we can declare a (max) heap, which enables us to efficiently find and
remove the maximum element, by changing the heap order property. Thus, a priority queue
can be used to find either a minimum or a maximum.
By the heap order property, the minimum element can always be found at the root.
https://play.google.com/store/apps/details?id=com.poriyaan.poriyaan
www.Poriyaan.in
91 CS3301 - DATASTRUCTURES
https://play.google.com/store/apps/details?id=com.poriyaan.poriyaan
www.Poriyaan.in
92 CS3301 - DATASTRUCTURES
Insert
To insert an element x into the heap, we create a hole in the next available location,
since otherwise the tree will not be complete.
If x can be placed in the hole without violating heap order, then we do so and are
done. Otherwise we slide the element that is in the hole's parent node into the hole, thus
bubbling the hole up toward the root. We continue this process until x can be placed in the
hole.
Figure shows that to insert 14, we create a hole in the next available heap location.
Inserting 14 in the hole would violate the heap order property, so 31 is slide down into the
hole.
This strategy is continued until the correct location for 14 is found. This general
strategy is known as a percolate up; the new element is percolated up the heap until the
correct location is found.
We could have implemented the percolation in the insert routine by performing
repeated swaps until the correct order was established, but a swap requires three assignment
statements. If an element is percolated up d levels, the number of assignments performed by
the swaps would be 3d. Our method uses d + 1 assignments.
https://play.google.com/store/apps/details?id=com.poriyaan.poriyaan
www.Poriyaan.in
93 CS3301 - DATASTRUCTURES
{
int i;
if( is_full( H ) )
error("Priority queue is full");
else
{
i = ++H->size;
while( H->elements[i/2] > x )
{
H->elements[i] = H->elements[i/2];
i /= 2;
} H->elements[i] = x; } }
If the element to be inserted is the new minimum, it will be pushed all the way to the
top. The time to do the insertion could be as much as O (log n), if the element to be inserted
is the new minimum and is percolated all the way to the root. On
Deletemin
Deletemin are handled in a similar manner as insertions. Finding the minimum is
easy; the hard part is removing it.
When the minimum is removed, a hole is created at the root. Since the heap now
becomes one smaller, it follows that the last element x in the heap must move somewhere in
the heap. If x can be placed in the hole, then we are done. This is unlikely, so we slide the
smaller of the hole's children into the hole, thus pushing the hole down one level. We repeat
this step until x can be placed in the hole. This general strategy is known as a percolate
down.
In Figure, after 13 is removed, we must now try to place 31 in the heap. 31 cannot be
placed in the hole, because this would violate heap order. Thus, we place the smaller child
(14) in the hole, sliding the hole down one level. We repeat this again, placing 19 into the
https://play.google.com/store/apps/details?id=com.poriyaan.poriyaan
www.Poriyaan.in
94 CS3301 - DATASTRUCTURES
hole and creating a new hole one level deeper. We then place 26 in the hole and create a new
hole on the bottom level. Finally, we are able to place 31 in the hole.
https://play.google.com/store/apps/details?id=com.poriyaan.poriyaan
www.Poriyaan.in
95 CS3301 - DATASTRUCTURES
}
H->elements[i] = last_element;
return min_element;
}
The worst-case running time for this operation is O(log n). On average, the element
that is placed at the root is percolated almost to the bottom of the heap, so the average
running time is O (log n).
Other Heap Operations
The other heap operations are
1. Decreasekey
2. Increasekey
3. Delete
4. Buildheap
Decreasekey
The decreasekey(x, ∆, H) operation lowers the value of the key at position x by a
positive amount ∆. Since this might violate the heap order, it must be fixed by a percolate up.
USE:
This operation could be useful to system administrators: they can make their programs
run with highest priority.
Increasekey
The increasekey(x, ∆, H) operation increases the value of the key at position x by a
positive amount ∆. This is done with a percolate down.
USE:
Many schedulers automatically drop the priority of a process that is consuming
excessive CPU time.
Delete
The delete(x, H) operation removes the node at position x from the heap. This is done
by first performing decreasekey(x,∆ , H) and then performing deletemin(H). When a process
is terminated by a user, it must be removed from the priority queue.
Buildheap
The buildheap(H) operation takes as input n keys and places them into an empty heap.
This can be done with n successive inserts. Since each insert will take O(1) average and
O(log n) worst-case time, the total running time of this algorithm would be O(n) average but
O(n log n) worst-case.
https://play.google.com/store/apps/details?id=com.poriyaan.poriyaan
www.Poriyaan.in
96 CS3301 - DATASTRUCTURES
B-Trees
AVL tree and Splay tree are binary; there is a popular search tree that is not binary.
This tree is known as a B-tree.
A B-tree of order m is a tree with the following structural properties:
a. The root is either a leaf or has between 2 and m children.
b. All nonleaf nodes (except the root) have between m/2 and m children.
c. All leaves are at the same depth.
All data is stored at the leaves. Contained in each interior node are pointers
p1, p2, . . . , pm to the children, and values k1, k2, . . . , km - 1, representing the smallest key
found in the subtrees p2, p3, . . . , pm respectively. Some of these pointers might be NULL,
and the corresponding ki would then be undefined.
For every node, all the keys in subtree p1 are smaller than the keys in subtree p2, and so on.
The leaves contain all the actual data, which is either the keys themselves or pointers to
records containing the keys.
The number of keys in a leaf is also between m/2 and m.
An example of a B-tree of order 4
A B-tree of order 4 is more popularly known as a 2-3-4 tree, and a B-tree of order 3 is
known as a 2-3 tree
https://play.google.com/store/apps/details?id=com.poriyaan.poriyaan
www.Poriyaan.in
97 CS3301 - DATASTRUCTURES
We have drawn interior nodes (nonleaves) in ellipses, which contain the two pieces of
data for each node. A dash line as a second piece of information in an interior node indicates
that the node has only two children. Leaves are drawn in boxes, which contain the keys. The
keys in the leaves are ordered.
To perform a find, we start at the root and branch in one of (at most) three directions,
depending on the relation of the key we are looking for to the two values stored at the node.
When we get to a leaf node, we have found the correct place to put x. Thus, to insert a
node with key 18, we can just add it to a leaf without causing any violations of the 2-3 tree
properties. The result is shown in the following figure.
If we now try to insert 1 into the tree, we find that the node where it belongs is
already full. Placing our new key into this node would give it a fourth element which is not
allowed. This can be solved by making two nodes of two keys each and adjusting the
information in the parent.
https://play.google.com/store/apps/details?id=com.poriyaan.poriyaan
www.Poriyaan.in
98 CS3301 - DATASTRUCTURES
To insert 19 into the current tree, two nodes of two keys each, we obtain the following tree.
This tree has an internal node with four children, but we only allow three per node.
Again split this node into two nodes with two children. Now this node might be one of three
children itself, and thus splitting it would create a problem for its parent but we can keep on
splitting nodes on the way up to the root until we either get to the root or find a node with
only two children.
If we now insert an element with key 28, we create a leaf with four children, which is
split into two leaves of two children.
https://play.google.com/store/apps/details?id=com.poriyaan.poriyaan
www.Poriyaan.in
99 CS3301 - DATASTRUCTURES
This creates an internal node with four children, which is then split into two children.
Like to insert 70 into the tree above, we could move 58 to the leaf containing 41 and 52,
place 70 with 59 and 61, and adjust the entries in the internal nodes.
Deletion in B-Tree
• If this key was one of only two keys in a node, then its removal leaves only one key.
We can fix this by combining this node with a sibling. If the sibling has three keys,
we can steal one and have both nodes with two keys.
• If the sibling has only two keys, we combine the two nodes into a single node with
three keys. The parent of this node now loses a child, so we might have to percolate
this strategy all the way to the top.
• If the root loses its second child, then the root is also deleted and the tree becomes one
level shallower.
We repeat this until we find a parent with less than m children. If we split the root, we
create a new root with two children.
The depth of a B-tree is at most log m/2 n.
The worst-case running time for each of the insert and delete operations is thus O(m
logm n) = O( (m / log m ) log n), but a find takes only O(log n ).
Definitions
Graph
A graph G = (V, E) consists of a set of vertices, V, and a set of edges, E. Each
edge is a pair (v,w), where v,w € V. Edges are sometimes referred to as arcs.
A B Edge / arcs
A, B, C, D and E are vertices
Vertex C E
D
Types of graph
1. Directed Graph
If the pair is ordered, then the graph is directed. In a graph, if all the edges are
directionally oriented, then the graph is called as directed Graph. Directed graphs are
sometimes referred to as digraphs.
Vertex w is adjacent to v if A B and only if (v, w) has an edge E.
C E
D
https://play.google.com/store/apps/details?id=com.poriyaan.poriyaan
www.Poriyaan.in
100 CS3301 - DATASTRUCTURES
2. Undirected Graph
In a graph, if all the edges are not directionally oriented, then the graph is called as
undirected Graph. In an undirected graph with edge (v,w), and hence (w,v), w is adjacent to
v and v is adjacent to w.
1 2
3
4
5
3. Mixed Graph
In a graph if the edges are either directionally or not directionally oriented, then it is
called as mixed graph.
S U
T V
Path
A path in a graph is a sequence of vertices w1, w2, w3, . . . , wn such that (wi, wi+i) €
E for 1< i < n.
Path length
The length of a path is the number of edges on the path, which is equal to n – 1 where
n is the no of vertices.
Loop
A path from a vertex to itself; if this path contains no edges, then the path length is 0.
If the graph contains an edge (v,v) from a vertex to itself, then the path v, v is sometimes
referred to as a loop.
https://play.google.com/store/apps/details?id=com.poriyaan.poriyaan
www.Poriyaan.in
101 CS3301 - DATASTRUCTURES
Simple Path
A simple path is a path such that all vertices are distinct, except that the first and last
could be the same.
A B
A->C->D->E
C E
D
Cycle
In a graph, if the path starts and ends to the same vertex then it is known as Cycle.
A->C->D->E->A
Cyclic Graph
A directed graph is said to be cyclic graph, if it has cyclic path.
Acyclic Graph
A directed graph is acyclic if it has no cycles. A directed acyclic graph is also referred
as DAG.
Connected Graph
An undirected graph is connected if there is a path from every vertex to every other
vertex.
https://play.google.com/store/apps/details?id=com.poriyaan.poriyaan
www.Poriyaan.in
102 CS3301 - DATASTRUCTURES
Complete graph
A complete graph is a graph in which there is an edge between every pair of vertices.
Weighted Graph
In a directed graph, if some positive non zero integer values are assigned to
each and every edges, then it is known as weighted graph. Also called as Network
An example of a real-life situation that can be modeled by a graph is the airport
system. Each airport is a vertex, and two vertices are connected by an edge if there is a
nonstop flight from the airports that are represented by the vertices. The edge could have a
weight, representing the time, distance, or cost of the flight.
https://play.google.com/store/apps/details?id=com.poriyaan.poriyaan
www.Poriyaan.in
103 CS3301 - DATASTRUCTURES
Representation of Graphs
1. Adjacency matrix / Incidence Matrix
2. Adjacency Linked List/ Incidence Linked List
Adjacency matrix
We will consider directed graphs. (Fig. 1)
Now we can number the vertices, starting at 1. The graph shown in above figure represents 7
vertices and 12 edges.
One simple way to represent a graph is to use a two-dimensional array. This is known
as an adjacency matrix representation.
For each edge (u, v), we set a[u][v]= 1; otherwise the entry in the array is 0. If the edge has a
weight associated with it, then we can set a[u][v] equal to the weight and use either a very
large or a very small weight as a sentinel to indicate nonexistent edges.
Advantage is, it is extremely simple, and the space requirement is (|V|2).
https://play.google.com/store/apps/details?id=com.poriyaan.poriyaan
www.Poriyaan.in
104 CS3301 - DATASTRUCTURES
Adjacency lists
Adjacency lists are the standard way to represent graphs. Undirected graphs can be
similarly represented; each edge (u, v) appears in two lists, so the space usage essentially
doubles. A common requirement in graph algorithms is to find all vertices adjacent to some
given vertex v, and this can be done, in time proportional to the number of such vertices
found, by a simple scan down the appropriate adjacency list.
An adjacency list representation of a graph (See above fig 5.1)
Topological Sort
A topological sort is an ordering of vertices in a directed acyclic graph, such that if
there is a path from vi to vj, then vj appears after vi in the ordering.
It is clear that a topological ordering is not possible if the graph has a cycle, since for
two vertices v and w on the cycle, v precedes w and w precedes v.
Directed acyclic graph
In the above graph v1, v2, v5, v4, v3, v7, v6 and v1, v2, v5, v4, v7, v3, v6 are both
topological orderings.
https://play.google.com/store/apps/details?id=com.poriyaan.poriyaan
www.Poriyaan.in
105 CS3301 - DATASTRUCTURES
https://play.google.com/store/apps/details?id=com.poriyaan.poriyaan
www.Poriyaan.in
106 CS3301 - DATASTRUCTURES
top_num[v] = counter;
for each w adjacent to v
indegree[w]--;
}
}
Explanation
The function find_new_vertex_of_indegree_zero scans the indegree array looking
for a vertex with indegree 0 that has not already been assigned a topological number. It
returns NOT_A_VERTEX if no such vertex exists; this indicates that the graph has a cycle.
Routine to perform Topological Sort
Void topsort( graph G )
{
QUEUE Q;
unsigned int counter;
vertex v, w;
Q = create_queue( NUM_VERTEX );
makeempty( Q );
counter = 0;
for each vertex v
if( indegree[v] = 0 )
enqueue( v, Q );
while( !isempty( Q ) )
{
v = dequeue( Q );
top_num[v] = ++counter; /* assign next number */
for each w adjacent to v
if( --indegree[w] = 0 )
enqueue( w, Q );
}
if( counter != NUMVERTEX )
error("Graph has a cycle");
dispose_queue( Q ); /* free the memory */
https://play.google.com/store/apps/details?id=com.poriyaan.poriyaan
www.Poriyaan.in
107 CS3301 - DATASTRUCTURES
}
Graph Traversal:
Visiting of each and every vertex in the graph only once is called as Graph traversal.
There are two types of Graph traversal.
1. Depth First Traversal/ Search (DFS)
2. Breadth First Traversal/ Search (BFS)
https://play.google.com/store/apps/details?id=com.poriyaan.poriyaan
www.Poriyaan.in
108 CS3301 - DATASTRUCTURES
An undirected graph
https://play.google.com/store/apps/details?id=com.poriyaan.poriyaan
www.Poriyaan.in
109 CS3301 - DATASTRUCTURES
Tree edge
The root of the tree is A, the first vertex visited. Each edge (v, w) in the graph is present
in the tree. If, when we process (v, w), we find that w is unmarked, or if, when we process
(w, v), we find that v is unmarked, we indicate this with a tree edge.
If when we process (v, w), we find that w is already marked, and when processing (w, v), we
find that v is already marked, we draw a dashed line, which we will call a back edge, to
indicate that this "edge" is not really part of the tree.
{
visited[v]= true;
B E
For each w adjacent to v D
If (!visited[w])
visited[w] = true;
C
}
Difference between DFS & BFS
S. No DFS BFS
1 Back tracking is possible from a dead end. Back tracking is not possible.
2 Vertices from which exploration is The vertices to be explored are
incomplete are processed in a LIFO order. organized as a FIFO queue.
3 Search is done in one particular direction at The vertices in the same level are
the time. maintained parallel. (Left to right) (
alphabetical ordering)
https://play.google.com/store/apps/details?id=com.poriyaan.poriyaan
www.Poriyaan.in
110 CS3301 - DATASTRUCTURES
A
A
B A
D A
C D
B C
E
E G H
Order of traversal:
F
A→B→C→D→E
Order of traversal:
A→B→C→D→E→F→G→H
https://play.google.com/store/apps/details?id=com.poriyaan.poriyaan
www.Poriyaan.in
111 CS3301 - DATASTRUCTURES
https://play.google.com/store/apps/details?id=com.poriyaan.poriyaan
www.Poriyaan.in
112 CS3301 - DATASTRUCTURES
The depth-first search tree in the above Figure shows the preorder number first, and then the
lowest-numbered vertex reachable under the rule described above.
The lowest-numbered vertex reachable by A, B, and C is vertex 1 (A), because they can all
take tree edges to D and then one back edge back to A and find low value for all other
vertices.
Depth-first tree that results if depth-first search starts at C
https://play.google.com/store/apps/details?id=com.poriyaan.poriyaan
www.Poriyaan.in
113 CS3301 - DATASTRUCTURES
https://play.google.com/store/apps/details?id=com.poriyaan.poriyaan
www.Poriyaan.in
114 CS3301 - DATASTRUCTURES
findart( w );
if( low[w] >= num[v] )
printf ( "%v is an articulation point\n", v );
low[v] = min( low[v], low[w] ); /* Rule */
}
else
if( parent[v] != w ) /* back edge */
low[v] = min( low[v], num[w] ); /* Rule 2 */
}
}
Euler Circuits
We must find a path in the graph that visits every edge exactly once. If we are to solve
the "extra challenge," then we must find a cycle that visits every edge exactly once. This
graph problem was solved in 1736 by Euler and marked the beginning of graph theory. The
problem is thus commonly referred to as an Euler path or Euler tour or Euler circuit
problem, depending on the specific problem statement.
Consider the three figures as shown below. A popular puzzle is to reconstruct these
figures using a pen, drawing each line exactly once. The pen may not be lifted from the paper
while the drawing is being performed. As an extra challenge, make the pen finish at the same
point at which it started.
Three drawings
1. The first figure can be drawn only if the starting point is the lower left- or right-hand
corner, and it is not possible to finish at the starting point.
2. The second figure is easily drawn with the finishing point the same as the starting
point.
3. The third figure cannot be drawn at all within the parameters of the puzzle.
https://play.google.com/store/apps/details?id=com.poriyaan.poriyaan
www.Poriyaan.in
115 CS3301 - DATASTRUCTURES
We can convert this problem to a graph theory problem by assigning a vertex to each
intersection. Then the edges can be assigned in the natural manner, as in figure.
The first observation that can be made is that an Euler circuit, which must end on its starting
vertex, is possible only if the graph is connected and each vertex has an even degree (number
of edges). This is because, on the Euler circuit, a vertex is entered and then left.
If exactly two vertices have odd degree, an Euler tour, which must visit every edge but need
not return to its starting vertex, is still possible if we start at one of the odd-degree vertices
and finish at the other.
If more than two vertices have odd degree, then an Euler tour is not possible.
That is, any connected graph, all of whose vertices have even degree, must have an Euler
circuit
As an example, consider the graph in
The main problem is that we might visit a portion of the graph and return to the starting point
prematurely. If all the edges coming out of the start vertex have been used up, then part of the
graph is untraversed.
The easiest way to fix this is to find the first vertex on this path that has an untraversed edge,
and perform another depth-first search. This will give another circuit, which can be spliced
into the original. This is continued until all edges have been traversed.
Suppose we start at vertex 5, and traverse the circuit 5, 4, 10, 5. Then we are stuck,
and most of the graph is still untraversed. The situation is shown in the Figure.
https://play.google.com/store/apps/details?id=com.poriyaan.poriyaan
www.Poriyaan.in
116 CS3301 - DATASTRUCTURES
We then continue from vertex 4, which still has unexplored edges. A depth-first search might
come up with the path 4, 1, 3, 7, 4, 11, 10, 7, 9, 3, 4. If we splice this path into the previous
path of 5, 4, 10, 5, then we get a new path of 5, 4, 1, 3, 7 ,4, 11, 10, 7, 9, 3, 4, 10, 5.
The graph that remains after this is shown in the Figure
The next vertex on the path that has untraversed edges is vertex 3. A possible circuit would
then be 3, 2, 8, 9, 6, 3. When spliced in, this gives the path 5, 4, 1, 3, 2, 8, 9, 6, 3, 7, 4, 11, 10,
7, 9, 3, 4, 10, 5.
The graph that remains is in the Figure.
On this path, the next vertex with an untraversed edge is 9, and the algorithm finds the circuit
9, 12, 10, 9. When this is added to the current path, a circuit of 5, 4, 1, 3, 2, 8, 9, 12, 10, 9, 6,
3, 7, 4, 11, 10, 7, 9, 3, 4, 10, 5 is obtained. As all the edges are traversed, the algorithm
terminates with an Euler circuit.
Then the Euler Path for the above graph is 5, 4, 1, 3, 2, 8, 9, 12, 10, 9, 6, 3, 7, 4, 11,
10, 7, 9, 3, 4, 10, 5
https://play.google.com/store/apps/details?id=com.poriyaan.poriyaan
www.Poriyaan.in
117 CS3301 - DATASTRUCTURES
A cut vertex is a vertex that when removed (with its boundary edges) from a graph creates
more components than previously in the graph.
A cut edge is an edge that when removed (the vertices stay in place) from a graph creates
more components than previously in the graph.
Find the cut vertices and cut edges for the following graphs
Answers
31) The cut vertex is c. There are no cut edges.
32) The cut vertices are c and d. The cut edge is (c,d)
33) The cut vertices are b,c,e and i. The cut edges are: (a,b),(b,c),(c,d),(c,e),(e,i),(i,h)
Applications of graph:
Minimum Spanning Tree
Definition:
A minimum spanning tree exists if and only if G is connected. A minimum spanning
tree of an undirected graph G is a tree formed from graph edges that connects all the vertices
of G at lowest total cost.
The number of edges in the minimum spanning tree is |V| - 1. The minimum spanning
tree is a tree because it is acyclic, it is spanning because it covers every edge.
Application:
• House wiring with a minimum length of cable, reduces cost of the
wiring.
https://play.google.com/store/apps/details?id=com.poriyaan.poriyaan
www.Poriyaan.in
118 CS3301 - DATASTRUCTURES
At any point in the process, two vertices belong to the same set if and only if they are
connected in the current spanning forest. Thus, each vertex is initially in its own set.
• If u and v are in the same set, the edge is rejected, because since they are already
connected, adding (u, v) would form a cycle.
• Otherwise, the edge is accepted, and a union is performed on the two sets containing
u and v.
https://play.google.com/store/apps/details?id=com.poriyaan.poriyaan
www.Poriyaan.in
119 CS3301 - DATASTRUCTURES
https://play.google.com/store/apps/details?id=com.poriyaan.poriyaan
www.Poriyaan.in
120 CS3301 - DATASTRUCTURES
Vertex U, V;
while( edgesaccepted < NUMVERTEX-1 )
{
Pq. deletemin( e ); // e = (u, v)
Settype Uset =ds. find( U, S );
Settype Vset = ds.find( V, S );
if( Uset != Vset )
{
// accept the edge
edgesaccepted++;
ds.setunion( S, Uset, Vset );
} } }
Dijkstra's Algorithm
If the graph is weighted, the problem becomes harder, but we can still use the
ideas from the unweighted case.
Each vertex is marked as either known or unknown. A tentative distance dv is kept for each
vertex. The shortest path length from s to v using only known vertices as intermediates.
The general method to solve the single-source shortest-path problem is known as Dijkstra's
algorithm.
Dijkstra's algorithm proceeds in stages, just like the unweighted shortest-path
algorithm. At each stage, Dijkstra's algorithm selects a vertex v, which has the smallest dv
among all the unknown vertices, and declares that the shortest path from s to v is known.
https://play.google.com/store/apps/details?id=com.poriyaan.poriyaan
www.Poriyaan.in
121 CS3301 - DATASTRUCTURES
In the above graph, assuming that the start node, s, is v1. The first vertex selected is v1, with
path length 0. This vertex is marked known. Now that v1 is known.
Initial configuration table
v Known dv pv
-----------------------------
v1 0 0 0
v2 0 ∞ 0
v3 0 ∞ 0
v4 0 ∞ 0
v5 0 ∞ 0
v6 0 ∞ 0
v7 0 ∞ 0
The vertices adjacent to v1 are v2 and v4. Both these vertices get their entries adjusted, as
indicated below
After v1 is declared known
v Known dv pv
-------------------------------------
v1 1 0 0
v2 0 2 v1
v3 0 ∞ 0
v4 0 1 v1
v5 0 ∞ 0
v6 0 ∞ 0
v7 0 ∞ 0
Next, v4 is selected and marked known. Vertices v3, v5, v6, and v7 are adjacent.
After v4 is declared known
v Known dv pv
---------------------------------
v1 1 0 0
v2 0 2 v1
v3 0 3 v4
v4 1 1 v1
v5 0 3 v4
v6 0 9 v4
v7 0 5 v4
https://play.google.com/store/apps/details?id=com.poriyaan.poriyaan
www.Poriyaan.in
122 CS3301 - DATASTRUCTURES
https://play.google.com/store/apps/details?id=com.poriyaan.poriyaan
www.Poriyaan.in
123 CS3301 - DATASTRUCTURES
https://play.google.com/store/apps/details?id=com.poriyaan.poriyaan
www.Poriyaan.in
124 CS3301 - DATASTRUCTURES
https://play.google.com/store/apps/details?id=com.poriyaan.poriyaan
www.Poriyaan.in
125 CS3301 - DATASTRUCTURES
https://play.google.com/store/apps/details?id=com.poriyaan.poriyaan
www.Poriyaan.in
126 CS3301 - DATASTRUCTURES
https://play.google.com/store/apps/details?id=com.poriyaan.poriyaan
www.Poriyaan.in
127 CS3301 - DATASTRUCTURES
https://play.google.com/store/apps/details?id=com.poriyaan.poriyaan
www.Poriyaan.in
128 CS3301 - DATASTRUCTURES
https://play.google.com/store/apps/details?id=com.poriyaan.poriyaan
www.Poriyaan.in
129 CS3301 - DATASTRUCTURES
https://play.google.com/store/apps/details?id=com.poriyaan.poriyaan
www.Poriyaan.in
130 CS3301 - DATASTRUCTURES
https://play.google.com/store/apps/details?id=com.poriyaan.poriyaan
www.Poriyaan.in
131 CS3301 - DATASTRUCTURES
https://play.google.com/store/apps/details?id=com.poriyaan.poriyaan
www.Poriyaan.in
132 CS3301 - DATASTRUCTURES
https://play.google.com/store/apps/details?id=com.poriyaan.poriyaan
www.Poriyaan.in
133 CS3301 - DATASTRUCTURES
https://play.google.com/store/apps/details?id=com.poriyaan.poriyaan
www.Poriyaan.in
134 CS3301 - DATASTRUCTURES
https://play.google.com/store/apps/details?id=com.poriyaan.poriyaan
www.Poriyaan.in
135 CS3301 - DATASTRUCTURES
https://play.google.com/store/apps/details?id=com.poriyaan.poriyaan
www.Poriyaan.in
136 CS3301 - DATASTRUCTURES
https://play.google.com/store/apps/details?id=com.poriyaan.poriyaan
www.Poriyaan.in
137 CS3301 - DATASTRUCTURES
https://play.google.com/store/apps/details?id=com.poriyaan.poriyaan
www.Poriyaan.in
138 CS3301 - DATASTRUCTURES
https://play.google.com/store/apps/details?id=com.poriyaan.poriyaan
www.Poriyaan.in
139 CS3301 - DATASTRUCTURES
https://play.google.com/store/apps/details?id=com.poriyaan.poriyaan
www.Poriyaan.in
140 CS3301 - DATASTRUCTURES
https://play.google.com/store/apps/details?id=com.poriyaan.poriyaan
www.Poriyaan.in
141 CS3301 - DATASTRUCTURES
https://play.google.com/store/apps/details?id=com.poriyaan.poriyaan
www.Poriyaan.in
142 CS3301 - DATASTRUCTURES
https://play.google.com/store/apps/details?id=com.poriyaan.poriyaan
www.Poriyaan.in
143 CS3301 - DATASTRUCTURES
https://play.google.com/store/apps/details?id=com.poriyaan.poriyaan
www.Poriyaan.in
144 CS3301 - DATASTRUCTURES
https://play.google.com/store/apps/details?id=com.poriyaan.poriyaan
www.Poriyaan.in
145 CS3301 - DATASTRUCTURES
https://play.google.com/store/apps/details?id=com.poriyaan.poriyaan
www.Poriyaan.in
146 CS3301 - DATASTRUCTURES
https://play.google.com/store/apps/details?id=com.poriyaan.poriyaan
www.Poriyaan.in
147 CS3301 - DATASTRUCTURES
2. Closed Hashing – Collide elements are stored at another slot in the table. Ensures
that all the elements are stored directly into the hash table. Eg Open addressing.
Rehashing and Extendible Hashing
Open addressing : Linear Probing, Quadratic Probing and Double Hashing
The first strategy, commonly known as either open hashing, or separate chaining, is to keep a
list of all elements that hash to the same value. For convenience, our lists have headers.
hash(x) = x mod 10. (The table size is 10)
To perform an insert, we traverse down the appropriate list to check whether the element is
already in place. If the element turns out to be new, it is inserted either at the front of the list
or at the end of the list. New elements are inserted at the front of the list.
struct listnode
{
elementtype element;
position next;
};
struct hashtbl
{
int tablesize;
https://play.google.com/store/apps/details?id=com.poriyaan.poriyaan
www.Poriyaan.in
148 CS3301 - DATASTRUCTURES
LIST *thelists;
};
https://play.google.com/store/apps/details?id=com.poriyaan.poriyaan
www.Poriyaan.in
149 CS3301 - DATASTRUCTURES
{
position p;
LIST L;
L = H->thelists[ hash( key, H->tablesize) ];
p = L->next;
while( (p != NULL) && (p->element != key) )
p = p->next;
return p;
}
https://play.google.com/store/apps/details?id=com.poriyaan.poriyaan
www.Poriyaan.in
150 CS3301 - DATASTRUCTURES
In a closed hashing system, if a collision occurs, alternate cells are tried until an empty cell is
found. More formally, cells h0(x), h1(x), h2(x), . . . are tried in succession where hi(x) =
(hash(x) + F(i) mod tablesize), with F(0) = 0.
The function, F , is the collision resolution strategy. Because all the data goes inside the table,
a bigger table is needed for closed hashing than for open hashing. Generally, the load factor
should be below = 0.5 for closed hashing.
Quadratic Probing
https://play.google.com/store/apps/details?id=com.poriyaan.poriyaan
www.Poriyaan.in
151 CS3301 - DATASTRUCTURES
Quadratic probing is a collision resolution method that eliminates the primary clustering
problem of linear probing. Quadratic probing is what you would expect-the collision function
is
quadratic. The popular choice is F(i) = i2
When 49 collide with 89, the next position attempted is one cell away. This cell is empty, so
49 is placed there. Next 58 collides at position 8. Then the cell one away is tried but another
collision occurs. A vacant cell is found at the next cell tried, which is 22 = 4 away. 58 is thus
placed in cell 2.
{89, 18, 49, 58, 69}
Double Hashing
The last collision resolution method we will examine is double hashing. For double hashing,
one popular choice is f(i) = i h2 (x). This formula says that we apply a second hash function
to x and probe at a distance h2 (x), 2 h2 (x), . . ., and so on. A function such as h2 (x) = R - (x
mod R), with R a prime smaller than H_SIZE, will work well.
https://play.google.com/store/apps/details?id=com.poriyaan.poriyaan
www.Poriyaan.in
152 CS3301 - DATASTRUCTURES
Rehashing
If the table gets too full, the running time for the operations will start taking too long and
inserts might fail for closed hashing with quadratic resolution. This can happen if there are
too many deletions intermixed with insertions.
A solution, then, is to build another table that is about twice as big and scan down the entire
original hash table, computing the new hash value for element and inserting it in the new
table.
As an example, suppose the elements 13, 15, 24, and 6 are inserted into a closed hash table of
size 7. The hash function is h(x) = x mod 7. Suppose linear probing is used to resolve
collisions.
https://play.google.com/store/apps/details?id=com.poriyaan.poriyaan
www.Poriyaan.in
153 CS3301 - DATASTRUCTURES
Rehashing routines
Hashtable rehash( HASH_TABLE H )
{
unsigned int i, old_size;
cell *old_cells;
old_cells = H->the_cells;
old_size = H->table_size;
/* Get a new, empty table */
H = initialize_table( 2*old_size );
/* Scan through old table, reinserting into new */
for( i=0; i<old_size; i++ )
if( old_cells[i].info == legitimate )
insert( old_cells[i].element, H );
free( old_cells );
return H;
}
https://play.google.com/store/apps/details?id=com.poriyaan.poriyaan
www.Poriyaan.in
154 CS3301 - DATASTRUCTURES
Extendible Hashing
If the amount of data is too large to fit in main memory, then is the number of disk accesses
required to retrieve data. As before, we assume that at any point we have n records to store;
the value of n changes over time. Furthermore, at most m records fit in one disk block. We
will use m = 4 in this section. To be more formal, D will represent the number of bits used by
the root, which is sometimes known as the directory. The number of entries in the directory is
thus 2D . dL is the
number of leading bits that all the elements of some leaf have in common. dL will depend on
the particular leaf, and dL<=D.
Suppose that we want to insert the key 100100. This would go into the third leaf, but as the
third leaf is already full, there is no room. We thus split this leaf into two leaves, which are
now determined by the first three bits. This requires increasing the directory size to 3.
https://play.google.com/store/apps/details?id=com.poriyaan.poriyaan
www.Poriyaan.in
155 CS3301 - DATASTRUCTURES
If the key 000000 is now inserted, then the first leaf is split, generating two leaves with dL =
3.
Since D = 3, the only change required in the directory is the updating of the 000 and 001
pointers.
https://play.google.com/store/apps/details?id=com.poriyaan.poriyaan
www.Poriyaan.in
156 CS3301 - DATASTRUCTURES
https://play.google.com/store/apps/details?id=com.poriyaan.poriyaan
www.Poriyaan.in
157 CS3301 - DATASTRUCTURES
https://play.google.com/store/apps/details?id=com.poriyaan.poriyaan
www.Poriyaan.in
158 CS3301 - DATASTRUCTURES
2MARKS
1. Explain the term data structure.
The data structure can be defined as the collection of elements and all the possible
operations which are required for those set of elements. Formally data structure can be
defined as a data structure is a set of domains D, a set of domains F and a set of
axioms A. this triple (D,F,A) denotes the data structure d.
2. What do you mean by non-linear data structure? Give example.
The non-linear data structure is the kind of data structure in which the data may be
arranged in hierarchical fashion. For example- Trees and graphs.
3. What do you linear data structure? Give example.
The linear data structure is the kind of data structure in which the data is
linearly arranged. For example- stacks, queues, linked list.
4. Enlist the various operations that can be performed on data structure.
Various operations that can be performed on the data structure are
• Create
• Insertion of element
• Deletion of element
• Searching for the desired element
• Sorting the elements in the data structure
• Reversing the list of elements.
5. What is abstract data type? What are all not concerned in an ADT?
The abstract data type is a triple of D i.e. set of axioms, F-set of functions and A-
Axioms in which only what is to be done is mentioned but how is to be done is not
mentioned. Thus ADT is not concerned with implementation details.
6. List out the areas in which data structures are applied extensively.
Following are the areas in which data structures are applied extensively.
• Operating system- the data structures like priority queues are
used for scheduling the jobs in the operating system.
• Compiler design- the tree data structure is used in parsing the source
https://play.google.com/store/apps/details?id=com.poriyaan.poriyaan
www.Poriyaan.in
159 CS3301 - DATASTRUCTURES
program.
Stack data structure is used in handling recursive calls.
• Database management system- The file data structure is used in
database management systems. Sorting and searching techniques
can be applied on these data in the file.
• Numerical analysis package- the array is used to perform the
numerical analysis on the given set of data.
• Graphics- the array and the linked list are useful in graphics applications.
• Artificial intelligence- the graph and trees are used for the
applications like building expression trees, game playing.
https://play.google.com/store/apps/details?id=com.poriyaan.poriyaan
www.Poriyaan.in
160 CS3301 - DATASTRUCTURES
12. State the properties of LIST abstract data type with suitable example.
Various properties of LIST abstract data type are
(i) It is linear data structure in which the elements are arranged adjacent to each
other. (ii) It allows to store single variable polynomial.
(iii)If the LIST is implemented using dynamic memory then it is called linked list.
Example of LIST are- stacks, queues, linked list.
13. State the advantages of circular lists over doubly linked list.
In circular list the next pointer of last node points to head node, whereas in doubly
linked list each node has two pointers: one previous pointer and another is next pointer. The
main advantage of circular list over doubly linked list is that with the help of single
pointer field we can access head node quickly. Hence some amount of memory get saved
because in circular list only one pointer is reserved.
14. What are the advantages of doubly linked list over singly linked list?
The doubly linked list has two pointer fields. One field is previous link field and
another is next link field. Because of these two pointer fields we can access any node
efficiently whereas in singly linked list only one pointer field is there which stores forward
pointer.
15. Why is the linked list used for polynomial arithmetic?
We can have separate coefficient and exponent fields for representing each term of
polynomial. Hence there is no limit for exponent. We can have any number as an exponent.
16. What is the advantage of linked list over arrays?
The linked list makes use of the dynamic memory allocation. Hence the user can
allocate or de allocate the memory as per his requirements. On the other hand, the array
makes use of the static memory location. Hence there are chances of wastage of the
memory or shortage of memory for allocation.
17. What is the circular linked list?
The circular linked list is a kind of linked list in which the last node is connected to the
https://play.google.com/store/apps/details?id=com.poriyaan.poriyaan
www.Poriyaan.in
161 CS3301 - DATASTRUCTURES
20. What is static linked list? State any two applications of it.
➢ The linked list structure which can be represented using arrays is called static linked
list.
➢ It is easy to implement, hence for creation of small databases, it is useful.
➢ The searching of any record is efficient, hence the applications in which the record
need to be searched quickly, the static linked list are used.
16 MARKS
1. Explain the insertion operation in linked list. How nodes are inserted after a specified
node.
2. Write an algorithm to insert a node at the beginning of list?
3. Discuss the merge operation in circular linked lists.
4. What are the applications of linked list in dynamic storage management?
5. How polynomial expression can be represented using linked list?
6. What are the benefit and limitations of linked list?
7. Define the deletion operation from a linked list.
8. What are the different types of data structure?
9. Explain the operation of traversing linked list. Write the algorithm and
give an example.
https://play.google.com/store/apps/details?id=com.poriyaan.poriyaan
www.Poriyaan.in
162 CS3301 - DATASTRUCTURES
UNIT II
2MARKS
1. Define Stack
A Stack is an ordered list in which all insertions (Push operation) and deletion (Pop
operation) are made at one end, called the top. The topmost element is pointed by top. The
top is initialized to -1 when the stack is created that is when the stack is empty. In a stack
S = (a1,an), a1 is the bottom most element and element a is on top of element ai-1. Stack is
also referred as Last In First Out (LIFO) list.
2. What are the various Operations performed on the Stack?
The various operations that are performed on the stack are
CREATE(S) – Creates S as an empty stack.
PUSH(S,X) – Adds the element X to the top of the
stack. POP(S) – Deletes the top most elements from
the stack. TOP(S) – returns the value of top element
from the stack. ISEMTPTY(S) – returns true if
Stack is empty else false. ISFULL(S) - returns true
if Stack is full else false.
3.How do you test for an empty stack?
The condition for testing an empty stack is top =-1, where top is the pointer pointing to
the topmost element of the stack, in the array implementation of stack. In linked list
implementation of stack the condition for an empty stack is the header node link field is
NULL.
4.Name two applications of stack?
Nested and Recursive functions can be implemented using stack. Conversion of
Infix to Postfix expression can be implemented using stack. Evaluation of Postfix
expression can be implemented using stack.
5.Define a suffix expression.
The notation used to write the operator at the end of the operands is called suffix notation.
Suffix notation format : operand operand operator
Example: ab+, where a & b are operands and ‘+’ is addition operator.
6.What do you meant by fully parenthesized expression? Give example.
A pair of parentheses has the same parenthetical level as that of the operator to which
it corresponds. Such an expression is called fully parenthesized expression.
Ex: (a+((b*c) + (d * e))
https://play.google.com/store/apps/details?id=com.poriyaan.poriyaan
www.Poriyaan.in
163 CS3301 - DATASTRUCTURES
13.Write down the function to insert an element into a queue, in which the
queue is implemented as an array. (May 10)
Q – Queue
X – element to added to the queue Q
IsFull(Q) – Checks and true if Queue Q is full
https://play.google.com/store/apps/details?id=com.poriyaan.poriyaan
www.Poriyaan.in
164 CS3301 - DATASTRUCTURES
16 MARKS
1. Write an algorithm for Push and Pop operations on Stack using Linked list. (8)
2. Explain the linked list implementation of stack ADT in detail?
3. Define an efficient representation of two stacks in a given area of memory with n
words and explain.
4. Explain linear linked implementation of Stack and Queue?
a. Write an ADT to implement stack of size N using an array. The elements in
the stack are to be integers. The operations to be supported are PUSH, POP
and DISPLAY. Take into account the exceptions of stack overflow and stack
underflow. (8)
b. A circular queue has a size of 5 and has 3 elements 10,20 and 40 where
F=2 and R=4. After inserting 50 and 60, what is the value of F and R.
Trying to insert 30 at this stage what happens? Delete 2 elements from the
https://play.google.com/store/apps/details?id=com.poriyaan.poriyaan
www.Poriyaan.in
165 CS3301 - DATASTRUCTURES
UNIT III
1.Define tree
Trees are non-liner data structure, which is used to store data items in a shorted sequence. It
represents any hierarchical relationship between any data Item. It is a collection of nodes,
which has a distinguish node called the root and zero or more non-empty sub trees T1,
T2,….Tk. each of which are connected by a directed edge from the root.
5. Define sibling?
Nodes with the same parent are called siblings. The nodes with common
parents are called siblings.
https://play.google.com/store/apps/details?id=com.poriyaan.poriyaan
www.Poriyaan.in
166 CS3301 - DATASTRUCTURES
https://play.google.com/store/apps/details?id=com.poriyaan.poriyaan
www.Poriyaan.in
167 CS3301 - DATASTRUCTURES
A B-tree is a tree data structure that keeps data sorted and allows searches,
insertions, and deletions in logarithmic amortized time. Unlike self-balancing binary search
trees, it is optimized for systems that read and write large blocks of data. It is most
commonly used in database and file systems.
Important properties of a B-tree:
• B-tree nodes have many more than two children.
• A B-tree node may contain more than just a single element.
https://play.google.com/store/apps/details?id=com.poriyaan.poriyaan
www.Poriyaan.in
168 CS3301 - DATASTRUCTURES
https://play.google.com/store/apps/details?id=com.poriyaan.poriyaan
www.Poriyaan.in
169 CS3301 - DATASTRUCTURES
UNIT IV
PART A
1. Write the definition of weighted graph?
A graph in which weights are assigned to every edge is called a weighted graph.
2. Define Graph?
A graph G consist of a nonempty set V which is a set of nodes of the graph, a set E
which is the set of edges of the graph, and a mapping from the set of edges E to set of pairs
of elements of V. It can also be represented as G=(V, E).
3.Define adjacency matrix?
The adjacency matrix is an n x n matrix A whose elements aij are given by
Aij =1 if(vi,vj)exists, otherwise 0
4.Define adjacent nodes?
Any two nodes, which are connected by an edge in a graph, are called adjacent
nodes. For example, if an edge x E is associated with a pair of nodes
(u,v) where u, v V, then we say that the edge x connects the nodes u and v.
5.What is a directed graph?
A graph in which every edge is directed is called a directed graph.
6.What is an undirected graph?
A graph in which every edge is undirected is called an undirected graph.
7.What is a loop?
An edge of a graph, which connects to itself, is called a loop or sling.
8.What is a simple graph?
A simple graph is a graph, which has not more than one edge between a pair of nodes.
9.What is a weighted graph?
A graph in which weights are assigned to every edge is called a weighted graph.
10.Define indegree and out degree of a graph?
In a directed graph, for any node v, the number of edges, which have v as their initial
node, is called the out degree of the node v.
Outdegree: Number of edges having the node v as root node is the outdegree of the node v.
11.Define path in a graph?
The path in a graph is the route taken to reach terminal node from a starting node.
12.What is a simple path?
i. A path in a diagram in which the edges are distinct is called a simple
path. ii. It is also called as edge simple.
https://play.google.com/store/apps/details?id=com.poriyaan.poriyaan
www.Poriyaan.in
170 CS3301 - DATASTRUCTURES
https://play.google.com/store/apps/details?id=com.poriyaan.poriyaan
www.Poriyaan.in
171 CS3301 - DATASTRUCTURES
https://play.google.com/store/apps/details?id=com.poriyaan.poriyaan
www.Poriyaan.in
172 CS3301 - DATASTRUCTURES
• Insertion sort
• Shell sort
• Quick sort
• Radix sort
• Heap sort
• Merge sort
3. Why bubble sort is called so?
The bubble sort gets its name because as array elements are sorted they
gradually
“bubble” to their proper positions, like bubbles rising in a glass of soda.
4. State the logic of bubble sort algorithm.
The bubble sort repeatedly compares adjacent elements of an array. The first and
second elements are compared and swapped if out of order. Then the second and third
elements are compared and swapped if out of order. This sorting process continues
until the last two
elements of the array are compared and swapped if out of order.
5. What number is always sorted to the top of the list by each pass of the Bubble
sort algorithm?
Each pass through the list places the next largest value in its proper place. In essence, each
item “bubbles” up to the location where it belongs.
6. When does the Bubble Sort Algorithm stop?
The bubble sort stops when it examines the entire array and finds that no
"swaps" are needed. The bubble sort keeps track of the occurring swaps by the use of a
flag.
7. State the logic of selection sort algorithm.
It finds the lowest value from the collection and moves it to the left. This is
repeated until the complete collection is sorted.
8. What is the output of selection sort after the 2nd iteration given the
following sequence? 16 3 46 9 28 14
Ans: 3 9 46 16 28 14
9.How does insertion sort algorithm work?
In every iteration an element is compared with all the elements before it. While
comparing if it is found that the element can be inserted at a suitable position, then space is
created for it by shifting the other elements one position up and inserts the desired
https://play.google.com/store/apps/details?id=com.poriyaan.poriyaan
www.Poriyaan.in
173 CS3301 - DATASTRUCTURES
element at the suitable position. This procedure is repeated for all the elements in
the list until we get the sorted elements.
10.What operation does the insertion sort use to move numbers from the unsorted
section to the sorted section of the list?
The Insertion Sort uses the swap operation since it is ordering numbers within a
single list.
11. How many key comparisons and assignments an insertion sort makes in its worst
case?
The worst case performance in insertion sort occurs when the elements of the input
array are in descending order. In that case, the first pass requires one comparison, the
second pass requires two comparisons, third pass three comparisons,….kth pass requires (k-
1), and finally the last pass requires (n-1) comparisons. Therefore, total numbers of
comparisons are:
f(n) = 1+2+3+………+(n-k)+…..+(n-2)+(n-1) = n(n-1)/2 = O(n2)
12. Which sorting algorithm is best if the list is already sorted? Why?
Insertion sort as there is no movement of data if the list is already
sorted and complexity is of the order O(N).
13. Which sorting algorithm is easily adaptable to singly linked lists? Why?
Insertion sort is easily adaptable to singly linked list. In this method there is an
array link of pointers, one for each of the original array elements. Thus the array can be
thought of as a linear link list pointed to by an external pointer first initialized to 0. To
insert the kth element the linked list is traversed until the proper position for x[k] is found,
or until the end of the list is reached. At that point x[k] can be inserted into the list by
merely adjusting the pointers without shifting any elements in the array which reduces
insertion time.
14. Why Shell Sort is known diminishing increment sort?
The distance between comparisons decreases as the sorting
algorithm runs until the last phase in which adjacent elements are compared. In each step,
the sortedness of the sequence is increased, until in the last step it is completely sorted.
15. Which of the following sorting methods would be especially suitable to sort
alist L consisting of a sorted list followed by a few “random” elements?
Quick sort is suitable to sort a list L consisting of a sorted list followed by a few
“random” elements.
https://play.google.com/store/apps/details?id=com.poriyaan.poriyaan
www.Poriyaan.in
174 CS3301 - DATASTRUCTURES
16.What is the output of quick sort after the 3rd iteration given the following
sequence?
24 56 47 35 10 90 82 31
https://play.google.com/store/apps/details?id=com.poriyaan.poriyaan
www.Poriyaan.in
175 CS3301 - DATASTRUCTURES
the use of a hash table or binary search tree will result in more efficient searching, but more
often than not an array or linked list will be used. It is necessary to understand good ways of
searching data structures not designed to support efficient search.
21.What is linear search?
In Linear Search the list is searched sequentially and the position is returned if
the key element to be searched is available in the list, otherwise -1 is returned. The
search in Linear Search starts at the beginning of an array and move to the end, testing for
a match at each item.
22.What is Binary search?
A binary search, also called a dichotomizing search, is a digital scheme for locating a
specific object in a large set. Each object in the set is given a key. The number of keys is
always a power of 2. If there are 32 items in a list, for example, they might be numbered
0 through 31 (binary
00000 through 11111). If there are, say, only 29 items, they can be numbered 0
through 28 (binary 00000 through 11100), with the numbers 29 through31 (binary
11101, 11110, and
11111) as dummy
keys.
23.Define hash function?
Hash function takes an identifier and computes the address of that identifier in the hash
table using some function.
24.Why do we need a Hash function as a data structure as compared to any other
data structure? (may 10)
Hashing is a technique used for performing insertions, deletions, and finds in
constant average time.
25.What are the important factors to be considered in designing the hash function?
(Nov10)
• To avoid lot of collision the table size should be prime
• For string data if keys are very long, the hash function will take long to compute.
26.. What do you mean by hash table?
The hash table data structure is merely an array of some fixed size, containing the
keys. A key is a string with an associated value. Each key is mapped into some number in
the range 0 to tablesize-1 and placed in the appropriate cell.
https://play.google.com/store/apps/details?id=com.poriyaan.poriyaan
www.Poriyaan.in
176 CS3301 - DATASTRUCTURES
16 MARKS
1. Write an algorithm to implement Bubble sort with suitable example.
2. Explain any two techniques to overcome hash collision.
3. Write an algorithm to implement insertion sort with suitable example.
4. Write an algorithm to implement selection sort with suitable example.
5. Write an algorithm to implement radix sort with suitable example.
6. Write an algorithm for binary search with suitable example.
7. Discuss the common collision resolution strategies used in closed hashing system.
8. Given the input { 4371, 1323, 6173, 4199, 4344, 9679, 1989 } and a hash function of
h(X)=X (mod 10) show the resulting:
a. Separate Chaining hash table
b. Open addressing hash table using linear probing
9. Explain Re-hashing and Extendible hashing.
10. Show the result of inserting the keys 2,3,5,7,11,13,15,6,4 into an initially
empty extendible hashing data structure with M=3. (8) (Nov 10)
11. what are the advantages and disadvantages of various collision resolution
strategies? (6)
https://play.google.com/store/apps/details?id=com.poriyaan.poriyaan
Data Structure
Unit I: Lists
Data Structure | Abstract Data Types (ADT) | List ADT | Array Based
Implementation | Concept of Linked List | Linked List Implementation | Difference
Between Array and Linked Listed | Circularly Linked List | Doubly Linked
List | Applications of Lists | Polynomial ADT | Multilists | Two marks Questions with
Answers |
Physics
Basic for Engineering
Electrical and Data Structure
Problem Solving and Science Engineering
Electronics
Python Programming Object Oriented
Programming in C
Programming
Elective-Management
Professional Elective II
Professional Elective IV