DS Course File-Kalyani
DS Course File-Kalyani
1
DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING
COURSE FILE
Year/Semester: II/I
2
DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING
INDEX
Page No.
S. No. CONTENT
(From-To)
1. Vision and Mission of the Institute 4
2. Department Vision and Mission 5
3. PEOs, POs and PSOs 6-7
4. Syllabus 8
5. List of COs 9
6. CO PO Mapping 10
7. Lesson Plan 11-13
Unit wise important Questions/ Assignment Questions
8. 14
(Descriptive/Objective Questions)
9. Unit wise Notes
10. Previous Question Papers
3
DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING
Empowering women as innovators, entrepreneurs and thought leaders in the area of Science &
Technology
M2: Foster a culture of innovation and entrepreneurial spirit among students and faculty.
M3: Structure collaborative partnerships with industry and academia to address societal
needs and contemporary technological challenges.
4
DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING
To impart quality education in Computer Science and Engineering for women empowerment.
M1: To make the students strong in fundamental concepts and in problem solving skills.
M2: Imparting value based education for women empowerment.
M3: To bring out creativity in students that would promote innovation, research and entrepreneurship.
5
DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING
PEO-1: Graduates shall have the ability to apply knowledge across the disciplines and in emerging areas
of Computer Science and Engineering for higher studies, research, employability and handle the realistic
problems.
PEO-2: Graduates shall have good communication skills, possess ethical conduct, sense of responsibility
to serve the society, and protect the environment.
PEO-3: Graduates shall have strong foundation in academic excellence, soft skills, managerial skills,
leadership qualities and understand the need for lifelong learning for a successful professional career.
PO2: Problem analysis: Identify, formulate, review research literature, and analyze
complex engineering problems reaching substantiated conclusions using first principles of
mathematics, natural sciences, and engineering sciences.
PO4: Conduct investigations of complex problems: Use research-based knowledge and research
methods including design of experiments, analysis and interpretation of data, and synthesis of
the information to provide valid conclusions.
6
PO5: Modern tool usage: Create, select, and apply appropriate techniques, resources and modern
engineering and IT tools including prediction and modeling to complex engineering activities
with an understanding of the limitations.
PO6: The engineer and society: Apply reasoning informed by the contextual knowledge to
assess societal, health, safety, legal and cultural issues and the consequent responsibilities
relevant to the professional engineering practice.
PO7: Environment and sustainability: Understand the impact of the professional engineering
solutions in societal and environmental contexts, and demonstrate the knowledge of, and
need for sustainable development.
PO8: Ethics: Apply ethical principles and commit to professional ethics and responsibilities and
norms of the engineering practice.
PO9: Individual and team work: Function effectively as an individual, and as a member or
leader in diverse teams, and in multidisciplinary settings.
PO11: Project management and finance: An ability to use the techniques, skills and modern
engineering tools necessary for engineering practice.
PO12: Independent and Life Long Learning: Demonstrate a knowledge and understanding of
contemporary technologies, their applications and limitations, contemporary research in the
broader context of relevant fields.
PSO2: Foundation of Computer System: The ability to interpret the fundamental concepts,
methodologies and the functionality of hardware and software aspects of computer systems.
PSO3: Foundations of Software development: The ability to grasp the software development lifecycle
and methodologies. Acquire competent skills and knowledge of software design process.
7
DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING
SYLLABUS
:DATA STRUCTURES
II Year B.Tech. CSM I-Sem
Course Objectives:
1. Exploring basic data structures such as stacks and queues.
2. Introduces a variety of data structures such as hash tables, search trees, tries, heaps, graphs.
3. Introduces sorting and pattern matching algorithms.
Course Outcomes:
1. Ability to select the data structures that efficiently model the information in a problem
2 .Ability to assess efficiency trade-offs among different data structure implementations or combinations.
3.Implement and know the application of algorithms for sorting and pattern matching.
4.Design programs using a variety of data structures, including hash tables, binary and general tree
structures, search trees, tries, heaps, graphs, and AVL-trees.
UNIT - I
Introduction to Data Structures:abstract data types, Linear list – singly linked list implementation,
insertion, deletion and searching operations on linear list, Stacks-Operations, array and linked
representations of stacks, stack applications, Queues-operations, array and linked representations.
UNIT - II
Dictionaries: linear list representation, skip list representation, operations - insertion, deletion and
searching. Hash Table Representation: hash functions, collision resolution-separate chaining,open
addressing-linear probing, quadratic probing, double hashing, rehashing, extendible hashing
UNIT - III
Search Trees: Binary Search Trees, Definition, Implementation, Operations- Searching, Insertion and
Deletion, B-Trees,B+ Trees,AVL Trees, Definition, Height of an AVL Tree, Operations – Insertion,
Deletion and Searching, Red –Black, Splay Trees.
UNIT – IV
Graphs: Graph Implementation Methods. Graph Traversal Methods.
Sorting: Quick sort,Heap Sort, External Sorting- Model for external sorting, Merge Sort.
UNIT – V
Pattern Matching and Tries: Pattern matching algorithms-Brute force, the Boyer –Moore algorithm, the
Knuth-Morris-Pratt algorithm, Standard Tries, Compressed Tries, Suffix tries.
TEXT BOOKS: 1. Fundamentals of Data Structures in C, 2nd Edition, E. Horowitz, S. Sahni and Susan
Anderson Freed, Universities Press.
2. Data Structures using C – A. S. Tanenbaum, Y. Langsam, and M.J. Augenstein, PHI/Pearson
Education.
REFERENCE BOOKS: 1. Data Structures: A Pseudocode Approach with C, 2nd Edition, R. F. Gilberg
and B.A. Forouzan, Cengage Learning.
8
DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING
1. Ability to select the data structures that efficiently model the information in a problem
3.Implement and know the application of algorithms for sorting and pattern matching.
4.Design programs using a variety of data structures, including hash tables, binary and general tree
structures, search trees, tries, heaps, graphs, and AVL-trees.
9
DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING
COs-POs-PSOs MAPPING
CO-PO MAPPING
PO1 PO2 PO3 PO4 PO5 PO6 PO7 PO8 PO9 PO10 PO11 PO12
C302.1 2 3 2 2 3
C302.2 3 3 3 2 2
C302.3 2 2 3 2 1
C302.4 3 2 3 2 1
Avg. 2.50 2.50 2.75 2.00 1.75
CO-PSO MAPPING
10
DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING
LESSON PLAN
Year/Semester: II/I
Course objectives:
2. Introduces a variety of data structures such as hash tables, search trees, tries, heaps, graphs.
19. Skip list with its operations 20/10/23 Chalk and Board
20. Example with operations of skip list 21/10/23 Chalk and Board
21. Hashing, hash table, hash key, Types of 30/10/23 Chalk and Board
hash function
22. Collision resolution techniques with 1/11/23 PowerPoint
example Presentation
23. Rehashing, extendible hashing with 2/11/23 Chalk and Board
example
24. Trees introduction with basic 3/11/23 Chalk and Board
terminology Unit-III/
25. Binary tree and its types, tree traversals 13 4/11/23 Chalk and Board
26. BST and its operations 6/11/23 Chalk and Board
27. B-Trees Explanation 7/11/23 to 8/11/23 Chalk and Board
28. Examples of B-Trees,adv&disadv 9/11/23 PowerPoint
Presentation
29 B+ Trees Explanation 10/11/23 to Chalk and Board
15/11/23
30. Examples of B+-Trees,adv&disadv 16/11/23to17/11/23 PowerPoint
Presentation
31. AVL tree and its operations, example 18/11/23 to Chalk and Board
21/11/23
32. Red Black tree its operations example 22/11/23 to PowerPoint
for insertion 25/1123 Presentation
33. Deletion operation with example 26/11/23 Chalk and Board
to27/11/23
34. Splay trees introduction ,rotations and 2/12/23 to4/12/23 Chalk and Board
operations
13
Unit wise Important Questions (Descriptive)
Unit-I
Unit-II
Unit-III
1. What is a Binary tree? Explain about the types of binary trees with an example?
2. Discuss in detail about the types of Tree traversal methods?
3. In How many ways we represent a binary tree? Discuss in detail with an example?
4. What is a binary search tree? Explain properties of binary search tree with an example?
5. What are the operations performed on BST?
6. Explain about the operations of an AVL tree with an example?
7. Insert the following list of elements from the AVL tree. Delete the elements 18, 2 and 30 from
the AVL tree 12, 30, 36, 18, 25, 9, 4, 2, 17, 14 , 20, 47.
8. Explain about RED-BLACK trees with an example?
14
9. Construct a Red-Black tree with the following elements 40, 16, 36, 54, 18, 7, 48, 5. Delete
element 18 and add element 66.
10. What is splay tree? Explain about its rotations with an example?
11. Explain about B-Trees with example?
12. Explain about B+-Trees with example?
Unit-IV
1. What is a graph? discuss various types of graphs. Briefly few applications of graph?
2. Write a BFS algorithm with an example?
3. Write a DFS algorithm with an example?
4. Difference between BFS and DFS algorithm with examples
5. Explain about external sorting with an example.
6. Write an algorithm to implement a depth-first search with an example
7. Write an algorithm to Quicksort concept
8. Explain about Quicksort algorithm with example?
9. Perform heap sort algorithm for (10 15 6 2 25 18 16 2 20 4).
10. Explain the process of heap sort with example
11. Compare and contrast different sorting methods?
12. Sort the following list of elements by using Merge sort 30, 56, 78, 99, 12, 43, 10, 24, 85
Unit-V
15
Objective Questions:
2. 8. Inserting an item into the stack when stack is not full is called …………. Operation and deletion of
item form the stack, when stack is not empty is called ………..operation.
A) push, pop
B) pop, push
C) insert, delete
D) delete, insert
3. . ……………. Is a pile in which items are added at one end and removed from the other.
A) Stack
B) Queue
C) List
4. ………… is very useful in situation when data have to stored and then retrieved in reverse order.
A) Stack
B) Queue
C) List
D) Link list
5. Which data structure allows deleting data elements from and inserting at rear?
16
A) Stacks
B) Queues
C) Dequeues
6. . Which of the following data structure can’t store the non-homogeneous data elements?
A) Arrays
B) Records
C) Pointers
D) Stacks
7. A ……. is a data structure that organizes data similar to a line in the supermarket, where the first one
in line is the first one out.
C) Both of them
D) Neither of them
A) Stacks
B) List
C) Strings
D) Trees
A) Graph
B) Trees
C) Binary tree
D) Stack
10. To represent hierarchical relationship between elements, Which data structure is suitable?
17
A) Dequeue
B) Priority
C) Tree
D) Graph
11. 35. What will be the value of top, if there is a size of stack STACK_SIZE is 5
A) 5
B) 6
C) 4
D) None
A) Insertion
B) Deletion
C) Retrieval
D) Traversal
13. There is an extra element at the head of the list called a ……….
A) Antinel
B) Sentinel
C) List header
D) List head
B) only a and b
16. …………………. Is a directed tree in which outdegree of each node is less than or equal to two.
A) Unary tree
B) Binary tree
C) Trinary tree
D) Both B and C
A. Linear arrays
B. Linked lists
C. Queue
D. Stack
18. Which of the following data structure store the homogeneous data elements?
A. Arrays
B. Records
C. Pointers
D. Lists
19. When new data are to be inserted into a data structure, but there is not available space; this situation
is usually called ….
A. Underflow
B. overflow
C. houseful
D. saturated
20. A data structure where elements can be added or removed at either end but not in the middle is called
…
19
A. linked lists
B. stacks
C. queues
D. dequeue
A. creation
B. destruction
C. selection
22. The way in which the data item or items are logically related defines …..
A. storage structure
B. data structure
C. data relationship
D. data operation
23. Which of the following are the operations applicable on primitive data structures?
A. create
B. destroy
C. update
24. The use of pointers to refer elements of a data structure in which elements are logically adjacent is ….
A. pointers
B. linked allocation
C. stack
D. queue
A) Strings
20
B) Lists
C) Stacks
D) Tree
A) Array
B) Tree
C) Graphs
D) Hierarchy
27. The logical or mathematical model of a particular organization of data is called a ………
A) Data structure
B) Data arrangement
C) Data configuration
D) Data formation
A) Multidimensional array
B) Linear array
B) One-dimensional array
C) Vertical array
D) Horizontal array
B) For the size of the structure and the data in the structure are constantly changing
21
C) For both of above situation
A) Linear arrays
B) Linked lists
C) Graphs
D) Trees
32. Each node in a linked list has two pairs of ………….. and ……………….
A) Before deletion
D) After deletion
C) Time consuming
35. A linear list in which each node has pointers to point to the predecessor and successors nodes is
called as..
22
C) Doubly Linked List
36. A ……………….. is a linear list in which insertions and deletions are made to from either end of the
structure.
A) circular queue
B) random of queue
C) priority
D) dequeue
A) finding factorial
B) tower of Hanoi
a. Sorting
b. Merging
c. Inserting
d. Traversal
39. Finding the location of the element with a given value is:
a. Traversal
b. Search
c. Sort
d. None of above
23
c) A structure used for storage
41. If several elements are competing for the same bucket in the hash table, what is it called?
a) Diffusion
b) Replication
c) Collision
a) O(n)
b) O(logn)
c) O(nlogn)
d) O(1)
45. What data structure is used when converting an infix notation to prefix notation?
a) Stack
b) Queue
c) B-Trees
24
d) Linked-list
46. What is the postfix expression for the corresponding infix expression a+b*c+(d*e)
a) abc*+de*
b) abc+*de*
c) a+bc*de+*
d) abc*+(de)*
47. In infix to postfix conversion algorithm, the operators are associated from?
a) right to left
b) left to right
c) centre to left
d) centre to right
a) 2
c) 0 or 1 or 2
d) 0 or 1
c) have to know the maximum number of nodes possible before creation of trees
d) difficult to implement
50. What must be the ideal size of array if the height of tree is ‘l’?
a) 2l-1
b) l-1
c) l
d) 2l
25
51. Which of the following an external sorting technique ( )
55.What is the special property of red-black trees and what root should always be? ( )
a) a color which is either red or black and root should always be black color only
b) height of the tree
60. A BST is traversed in the following order recursively: Right, root, left
The output sequence will be in ( )
26
61. In a binary min heap containing n elements, the largest element can be found in ___ time. ( )
a) O(n) b) O(nlogn) c) O(logn) d) O(1)
64. Which one of the following array elements represents a binary min heap? ( )
a) 12 10 8 25 14 17 b) 8 10 12 25 14 17
c) 25 17 14 12 10 8 d) 14 17 25 10 12 8
65. Space complexity for an adjacency list of an undirected graph having large values of V (vertices) and
E (edges) is ( )
a) O(V) b) O(E*E) c) O(E) d) O(E+V)
68. The number of elements in the adjacency matrix of a graph having 7 vertices is ( )
a) 7 b) 14 c) 36 d) 49
69. What is the number of edges present in a complete graph having n vertices? ( )
a) (n*(n+1))/2 b) (n*(n-1))/2
c) n d) Information given is insufficient
71. What is the maximum number of edges in a bipartite graph having 10 vertices? ( )
a) 24 b) 21 c) 25 d) 16
72. For a given graph G having v vertices and e edges which is connected and has no cycles, which of the
following statements is true? ( )
a) v=e b) v = e+1 c) v + 1 = e d) v = e-1
27
74. Which of the following ways can be used to represent a graph? ( )
a) Adjacency List and Adjacency Matrix b) Incidence Matrix
c) sparse Matrix d) No way to represen
75. The Data structure used in standard implementation of Breadth First Search is? ( )
a) Stack b) Queue
c) Linked List d) Tree
76. The Breadth First Search traversal of a graph will result into? ( )
a) Linked List b) Tree c) Graph with back edges d) Arrays
77. A person wants to visit some places. He starts from a vertex and then wants to visit every place
connected to this vertex and so on. What algorithm he should use? ( )
a) Depth First Search b) Breadth First Search
c) Trim’s algorithm d) Kruskal’s algorithm
78. Depth First Search is equivalent to which of the traversal in the Binary Trees? ( )
a) Pre-order Traversal b) Post-order Traversal
c) Level-order Traversal d) In-order Traversal
80. The Data structure used in standard implementation of Breadth First Search is? ( )
a) Stack b) Queue c) Linked List d) Tree
81. The Depth First Search traversal of a graph will result into? ( )
82. A person wants to visit some places. He starts from a vertex and then wants to visit every vertex till it
finishes from one vertex, backtracks and then explore other vertex from same vertex. What algorithm he
should use? ( )
83 What is the worst case time complexity of KMP algorithm for pattern searching (m = length of text, n
= length of pattern)? ( )
85. Which of the following is the efficient data structure for searching words in dictionaries? ( )
28
a) BST b) Linked List c) Balancded BST d) Trie
86. Which of the following special type of trie is used for fast searching of the full texts? ( )
87. What can be the maximum depth of the trie with n strings and m as the maximum sting the length?
a) log2n b) log2m c) n d) m
a) root is letter a b) path from root to the leat yields the string
c) children of nodes are randomly ordered d) each node stores the associated keys
89. What is a time complexity for finding the longest prefix that is common between suffix in a string?(
)
90.What is a time complexity for finding the longest palindromic substring in a string by using the
generalized suffix tree? ( )
91.What is a time complexity for finding the total length of all string on all edges of a tree? ( )
a) Ɵ (n) b) Ɵ (n!) c) Ɵ (1) d) O (n2)
92. For what size of nodes, the worst case of usage of space in suffix tree seen? ( )
a) n Nodes b) 2n Nodes c) 2n nodes d) n! Nodes
93.An algorithm which tries all the possibilities unless results are satisfactory is and generally is time
consuming is ( )
a) Brute Force b) Divide and Conquer
c) Dynamic programming algorithms d) None of the mentioned
95.To perform the heap sort, you need to create a tree with all nodes greater than their ( )
29
96 Which of the following algorithms formed the basis for the Quick search algorithm? ( )
a) Boyer-Moore’s algorithm b) Parallel string matching algorithm
c) Binary Search algorithm d) Linear Search algorithm
98. What is the worst case running time in searching phase of Boyer-Moore’s algorithm? ( )
a) O(n) b) O(log n) c) O(m+n) d) O(mn)
99. Procedure of sorting algorithms for larger records that does not fit in main memory and are stored on
disk is classified as ( )
100. In external sorting, number of runs that can be merged in every pass are called ( )
3. In a stack, if a user tries to remove an element from empty stack it is called _________
4. Pushing an element into stack already having five elements and stack size of 5, then stack becomes
8. A linear list of elements in which deletion can be done from one end (front) and insertion can take
place only at the other end (rear) is known as a ?
30
9. A queue follows __________
11 If the elements “A”, “B”, “C” and “D” are placed in a queue and are deleted one at a time, in what
order will they be removed?-----------------------
12. 6. A data structure in which elements can be inserted or deleted at/from both the ends but not in the
middle is? ------------------------------
13. 7. A normal queue, if implemented using an array of size MAX_SIZE, gets full when the condition is
-------------------------
15. A linear collection of data elements where the linear node is given by means of pointer is called?
16. In linked list each node contain minimum of two fields. One field is data field to store the data
second field is?
31
24.. In a circular queue, how do you increment the rear end of the queue?
26.. In linked list implementation of a queue, from where is the item deleted?
27.. In linked list implementation of a queue, the important condition for a queue to be empty is?
28. The essential condition which is checked before insertion in a linked queue is?
29. Name one real world scenarios that you would associate with a stack data structure?
31.Convert the following infix expressions into its equivalent postfix expressions
(A+B⋀D)/(E–F)+G
32. In linked list each node contain minimum of two fields. One field is data field to store the data second
field is?
37 How many steps are involved in creating a hash function using a multiplication method?
38. If several elements are competing for the same bucket in the hash table, what is it called?
40. A linear list in which each node has pointers to point to the predecessor and successors nodes is
called as..
41. A ……………….. is a linear list in which insertions and deletions are made to from either end of the
structure.
42. A data structure where elements can be added or removed at either end but not in the middle is called
…
43. The way in which the data item or items are logically related defines …..
32
44. A ……. is a data structure that organizes data similar to a line in the supermarket, where the first one
in line is the first one out.
47. To represent hierarchical relationship between elements, Which data structure is suitable?
48. What will be the value of top, if there is a size of stack STACK_SIZE is 5
50. There is an extra element at the head of the list called a ……….
54. Nodes which are subtrees of another node are called _______________.
55. In Binary Search Trees, the keys of all elements are __________________.
56. If a node is a leaf node, then its left child and right child field are filled with _____________.
57. The process where two rotations are required to balance a tree is called _________________.
58. When the height of the left subtree and right subtree of a node in an AVL Tree are equal the
balancing factor is _____________________.
59. The __________________ of a vertex is the number of edges this vertex has that are connected to
other vertices.
63. The process of sorting a list stored in a file in secondary memory is known as _________________.
64. In a maxheap data structure, the_______________ element is placed in the root of the heap.
65. In a minheap data structure, the _______________ element is placed in the root of the heap.
33
68. The other name for tire is __________________.
70._____________is special type of trie is used for fast searching of the full texts.
73. In depth first search graph, _______________technique is used to remove a vertex from stack.
74. In red black tree, every path from a given node to any of its descendant leaves must contain the
___________________ number of black nodes.
76. A red-black tree is a self-balancing tree structure that applies a ______________ to each of its nodes.
77. In an AVL tree, the heights of the two child subtrees of any node differ by at most_______________
78. The operation of processing each element in the list is known as ____________________
79. A graph is said to be __________________ if every vertex' u ' in graph ' G ' is adjacent to every
other vertex ' v ' in graph ' G ',
81. ________________was the first linear time complexity algorithm for string matching
84. A trie searches the time complexity of a string depends upon the _________________the string
85. In trie, every node except the root stores a ___________________ value
86. In tries, if any two strings have a common prefix then they will have the ____________ ancestors
89. A __________________is also known as generate and test10. Sequential search is also considered as
and referred as __________________.
90. In a __________________ every new node operation is performed such that it moves to the root of
the tree.
34
91.If preorder of the binary tree is A B D H E C F I G J K, then the postorder of the binary tree will be
____________________
92. what is the value which cannot be a balance factor of any node of an AVL tree ____________
93.In zig rotation, every node moves one position to the _______________ from its current position.
94.In zag rotation, every node moves one position to the ______________ from its current position.
95.The ___________________in splay tree ,every node moves two positions to the right from its current
position
96.The Zag-Zag Rotation in splay tree,every node moves two positions to the ___________from its
current position.
97.In zig-zag rotation, every node moves one position to the ________________followed by one position
to the ___________________from its current position
98.In_____________, every node moves one position to the left followed by one position to the right
from its current position
99.In splay tree ,before deleting the element, we first need to _______________that element and then
delete it from the root position
100. In an AVL tree, the heights of the two child subtrees of any node differ by at
most_______________
UNIT-I
Introduction To Datastructures
Data Structure
Data Structure can be defined as the group of data elements which provides
an efficient way of storing and organising data in the computer so that it can
be used efficiently. Some examples of Data Structures are arrays, Linked
List, Stack, Queue, etc.
35
Need of Data Structures
Processor speed: To handle very large amout of data, high speed
processing is required, but as the data is growing day by day to the
billions of files per entity, processor may fail to deal with that much
amount of data.
36
Linear Data Structures: A data structure is called linear if all of
its elements are arranged in the linear order. In linear data
structures, the elements are stored in nonhierarchical way where
each element has the successors and predecessors except the first
and last element.
The elements of array share the same variable name but each one
carries a different index number known as subscript. The array can
be one dimensional, two dimensional or multidimensional.
Non Linear Data Structures: This data structure does not form a
sequence i.e. each item or element is connected with two or more
other items in a non-linear arrangement. The data elements are not
arranged in sequential structure.
If the size of data structure is n then we can only insert n-1 data
elements into it.
What is an Algorithm?
An algorithm is a process or a set of rules required to perform
calculations or some other problem-solving operations especially
by a computer. The formal definition of an algorithm is that it
contains the finite set of instructions which are being carried in a
specific order to perform the specific task.
39
Characteristics of an Algorithm
The following are the characteristics of an algorithm:
sum=0;
// Suppose we have to calculate the sum of n
numbers.
for i=1 to n
sum=sum+i
;
// when the loop ends then sum holds the sum of the
n numbers return sum;
In the above code, the time complexity of the loop statement will be
atleast n, and if the value of n increases, then the time complexity
also increases. While the complexity of the code, i.e., return sum will
40
be constant as its value is not dependent on the value of n and will
provide the result in one step only. We generally consider the worst-
time complexity as it is the maximum time taken for any given input
size.
o Space complexity: An algorithm's space complexity is the
amount of space required to solve a problem and produce an
output. Similar to the time complexity, space complexity is
also expressed in big O notation.
For an algorithm, the space is required for the following purposes:
Array
o Arrays are defined as the collection of similar type of data items
stored at contiguous memory locations.
o Arrays are the derived data type in C programming language
which can store the primitive type of data such as int, char,
double, float, etc.
o Array is the simplest data structure where each data element can
be randomly accessed by using its index number.
o For example, if we want to store the marks of a student in 6
subjects, then we don't need to define different variable for the
marks in different subject. instead of that, we can define an array
which can store the marks in each subject at a the contiguous
memory locations.
41
Properties of the Array
1. Each element is of same data type and carries a same size i.e. int =
4 bytes.
2. Elements of the array are stored at contiguous memory locations
where the first element is stored at the smallest memory location.
3. Elements of the array can be randomly accessed since we can
calculate the address of each element of the array with the given
base address and the size of data element.
#include
<stdio.h>
void main ()
{
int marks[6] =
{56,78,88,76,56,89);
int i; float avg;
for (i=0; i<6; i++ )
{
avg = avg + marks[i];
}
printf(avg);
}
Time Complexity
42
Access O(1) O(1)
Space Complexity
Advantages of Array
o Array provides the single name for the group of variables of the
same type therefore, it is easy to remember the name of all the
elements of an array. o Traversing an array is a very simple process,
we just need to increment the base address of the array in order to
visit each element one by one.
o Any element in the array can be directly accessed by using the
index.
43
2D Array
2D array can be defined as an array of arrays. The 2D array is
organized as matrices which can be represented as the collection
of rows and columns.
int arr[max_rows][max_columns];
44
Initializing 2D Arrays
We know that, when we declare and initialize one dimensional
array in C programming simultaneously, we don't need to specify
the size of the array. However this will not work with 2D arrays. We
will have to define at least the second dimension of the array.
Exampl
e:
#include
<stdio.h
> void
main ()
{ i
nt
arr[3]
[3],i,j;
for
(i=0;i
<3;i+
+)
{
for (j=0;j<3;j++)
{ printf("Ent
er a[%d][%d]: ",i,j);
scanf("%d",&arr[i]
[j]);
}
}
45
printf("\n printing the elements ....\n");
for(i=0;i<3;i++)
{
printf("\
n");
for
(j=0;j<3
;j++)
{
printf("%d\
t",arr[i][j]);
}
}
}
List ADT
• The data is generally stored in key sequence in a list which has
a head structure consisting of count, pointers and address of
compare function needed to compare the data in the list.
46
Stack ADT
• In Stack ADT Implementation instead of data being stored in each node, the
pointer to data is stored.
The program allocates memory for the data and address is passed to
the stack ADT.
A Stack contains elements of the same type arranged in sequential
order. All operations take place at a single end that is top of the stack
and following operations can be performed:
• push() – Insert an element at one end of the stack called top.
• pop() – Remove and return the element at the top of the stack, if it is
not empty.
• peek() – Return the element at the top of the stack without removing
it, if the stack is not empty.
• size() – Return the number of elements in the stack.
• isEmpty() – Return true if the stack is empty, otherwise return false.
• isFull() – Return true if the stack is full, otherwise return false.
47
Queue ADT
• The queue abstract data type (ADT) follows the basic design of the
stack abstract data type.
• Each node contains a void pointer to the data and the link pointer to
the next element in the queue. The program’s responsibility is to
allocate memory for storing the data.
Linked List
There are two major drawbacks of using array:
48
o We cannot insert more than 3 elements in the above example
because only 3 spaces are allocated for 3 elements.
o In the case of an array, lots of wastage of memory can occur. For
example, if we declare an array of 50 size but we insert only 10
elements in an array. So, in this case, the memory space for
other 40 elements will get wasted and cannot be used by
another variable as this whole space is occupied by an array.
o In array, we are providing the fixed-size at the compile-time, due to
which wastage of memory occurs. The solution to this problem is to
use the linked list
49
In the above figure, we can observe that each node contains the
data and the address of the next node. The last node of the linked
list contains the NULL value in the address part.
How can we declare the Linked list?
The declaration of an array is very simple as it is of single type. But
the linked list contains two parts, which are of two different types,
i.e., one is a simple variable, and the second one is a pointer
variable. We can declare the linked list by using the user-defined
data type known as structure.The structure of a linked list can be
defined as:
struct node
{
int
data;
struct
node
*next;
}
oDynamicdatastructure:
The size of the linked list is not fixed as it can vary according to our
requirements.
o InsertionandDeletion:
50
Insertion and deletion in linked list are easier than array as the
elements in an array are stored in a consecutive location. In
contrast, in the case of a linked list, the elements are stored in a
random location. The complexity for insertion and deletion of
elements from the beginning is O(1) in the linked list, while in the
case of an array, the complexity would be O(n).
o Memoryefficient
Its memory consumption is efficient as the size of the linked list
can grow or shrink according to our requirements.
o Implementation
Both the stacks and queues can be implemented using a linked
list.
Singly
Linked
list o
Doubly
Linked
list o
Circular
Linked
list
51
Singly Linked list
It is the commonly used linked list in programs. If we are talking
about the linked list, it means it is a singly linked list. The singly
linked list is a data structure that contains two parts, i.e., one is the
data part, and the other one is the address part, which contains the
address of the next or the successor node. The address part in a
node is also known as a pointer.
struct node
{
int
data;
struct
node
*next;
}
52
Complexity
Node Creation
struct node
{
int
data;
struct
node
*next;
};
struct node *head, *ptr;
ptr = (struct node *)malloc(sizeof(struct node *));
53
There are various linked list operations that allow us to perform different actions on linked
lists.
For example, the insertion operation adds a new element to the linked list.
Here's a list of basic linked list operations that we will cover in this article.
• Traversal - access each element of the linked list
• Insertion - adds a new element to the linked list
struct node {
int data;
struct node *next;
};
Displaying the contents of a linked list is very simple. We keep moving the temp
When temp is NULL , we know that we have reached the end of the linked list so
• Store data
55
for(int i=2; i < position; i+
+) { if(temp->next != NULL)
{ temp = temp->next;
} } newNode->next =
temp->next; temp->next =
newNode;
• Store data
56
temp->next = newNode;
You can delete either from the beginning, end or from a particular position.
head = head->next;
57
for(int i=2; i< position; i+
+) { if(temp->next!=NULL)
{ temp = temp->next;
}
} temp->next = temp-
>next->next;
You can search an element on a linked list using a loop using the following steps. We
are finding item on a linked list.
• Make head as the current node.
• Run a loop until the current node is NULL because the last element points to NULL .
• In each iteration, check if the key of the node is equal to item . If it the key matches the
item, return true otherwise return false .
We will use a simple sorting algorithm, Bubble Sort, to sort the elements of a linked list
1. Make the head as the current node and create another node index for
later use.
6. Check if the data of the current node is greater than the next node. If it is
greater, swap current and index .
59
A node is represented as
struct
node {
int data;
struct
node
*next;
struct
node
*prev; }
Each struct node has a data item, a pointer to the previous struct node, and a
pointer to the next struct node.
/* Initialize nodes */
struct node *head;
struct node *one =
NULL; struct node *two
= NULL; struct node
*three = NULL;
/* Connect nodes */
one->next = two; one-
>prev = NULL;
two->next =
three; two->prev
= one;
three->next =
NULL;
three->prev = two;
60
/* Save address of first node
in head */ head = one;
In the above code, one, two, and three are the nodes with data items 1,
2,
and 3 respectively.
• For node one: next stores the address of two and prev stores null (there
is no
node before it)
• For node two: next stores the address of three and prev stores the address of
one
• For node three: next stores null (there is no node after it) and prev stores the
address of two.
Let's add a node with value 6 at the beginning of the doubly linked
list we made above. 1. Create a new node
• allocate memory for newNode
• assign the data to newNode .
61
2. Set prev and next pointers of new node
• point next of newNode to the first node of the doubly linked list
• point prev to null
62
// assign data to newNode
newNode->data = data;
// point next of newNode to the first node of the doubly linked list
newNode->next = (*head);
// point previous of the first node (now first node is the second
node) to newNode if ((*head) != NULL)
(*head)->prev = newNode;
Let's add a node with value 6 after node with value 1 in the doubly linked
list.
63
3. Set the prev pointer of new node and the next node
• assign the value of prev of next node to the prev of newNode
• assign the address of newNode to the prev of next node
64
// assign data to newNode
newNode->data = data;
Let's add a node with value 6 at the end of the doubly linked list.
66
1. Delete the First Node of Doubly Linked List
if (*head == del_node)
*head = del_node->next;
if (del_node->prev != NULL)
del_node->prev->next = del_node->next;
free(del);
If del_node is an inner node (second node), we must have to reset the value
of next and prev of the nodes before and after the del_node .
For the node before the del_node (i.e. first node)
Assign the value of next of del_node to the next of the first node. For
the node after the del_node (i.e. third node)
Assign the value of prev of del_node to the prev of the third node.
67
Code for Deletion of the Inner Node
if (del_node->next != NULL)
del_node->next->prev =
del_node->prev;
if (del_node->prev != NULL)
del_node->prev->next =
del_node->next;
In this case, we are deleting the last node with value 3 of the doubly linked list.
Here, we can simply delete the del_nodeand make the next of node before del_nodepoint
to NULL.
if (del_node->prev != NULL)
del_node->prev->next =
del_node->next;
68
Circular Linked List
A circular linked list is a variation of a linked list in which the last element is linked to the first
• for singly linked list, next pointer of last item points to the first item
• In the doubly linked list, prev pointer of the first item points to the last item as well.
A three-member circular singly linked list can be created as:
/* Initialize nodes */
struct node *head;
struct node *one =
NULL; struct node *two
= NULL; struct node
*three = NULL;
/* Connect nodes */
one->next = two; two-
>next = three; three-
>next = one;
69
/* Save address of first node in
head */ head = one;
Here, in addition to the last node storing the address of the first node, the first node will
In the above image, although item 2 was kept last, it was removed
first. This is exactly how the LIFO (Last In First Out) Principle
works.
#define SIZE 10
voi
d
72
pus
h(i
nt)
;
voi
d
pop
();
voi
d
dis
pla
y()
;
int
stack[SIZE],
top = -1;
void main() {
int value,
choice;
clrscr();
while(1){
printf("\n\
n***** MENU
*****\n");
printf("1.
Push\n2. Pop\
n3. Display\
n4. Exit");
printf("\
nEnter your
choice: ");
scanf("%d",&c
73
hoice);
switch(choice
){
case 1: printf("Enter the value to be insert: ");
scanf("%d",&value);
push(value);
break;
case 2: pop();
break;
case 3: display();
break;
case 4: exit(0);
v
o
i
d
p
o
p
74
(
)
{
i
f
(
t
o
p
=
=
-
1
)
p
r
i
n
t
f
(
"
\
n
S
t
a
c
k
75
i
s
E
m
p
t
y
!
!
!
D
e
l
e
t
i
o
n
i
s
n
o
t
p
o
s
s
76
i
b
l
e
!
!
!
"
)
;
e
l
s
e
{
p
r
i
n
t
f
(
"
\
n
D
e
l
e
t
e
77
d
%
d
"
,
s
t
a
c
k
[
t
o
p
]
)
;
t
o
p
-
-
;
} } void display()
{ if(top == -1)
printf("\nStack is
Empty!!!"); else{
78
int i; printf("\
nStack elements are:\n");
for(i=top; i>=0; i--)
printf("%d\n",stack[i]);
Infix
Notati
on o
79
Prefix
Notati
on o
Postfi
x
Notati
on
Infix Notation
Example: A + B, (C - D) etc.
• All these expressions are in infix notation because the operator comes between
the operands.
• Prefix Notation
80
A*B *AB AB*
81
3 Empty 3
* * 3
3 * 33
/ / 33*
( /( 33*
4 /( 33*4
- /(- 33*4
1 /(- 33*41
) - 33*41-
+ + 33*41-/
6 + 33*41-/6
* +* 33*41-/62
2 +* 33*41-/62
Empty 33*41-/62*+
Reverse a Data:
To reverse a given set of data, we need to reorder the data so that
the first and last elements are exchanged, the second and second
last element are exchanged, and so on for all other elements.
82
Example: Suppose we have a string Welcome, then on reversing it
would be Emoclew.
o Reversing
a string o
Converting
Decimal to Binary
Reverse a String
A Stack can be used to reverse the characters of a string. This can
be achieved by simply pushing one by one each character onto the
Stack, which later can be popped from the
Stack one by one. Because of the last in first out property of the
Stack, the first character of the Stack is on the bottom of the Stack
and the last character of the String is on the Top of the Stack and
after performing the pop operation in the Stack, the Stack returns
the String in Reverse order.
Backtracking
Backtracking is another application of Stack. It is a recursive
algorithm that is used for solving the optimization problem.
Delimiter Checking
The common application of Stack is delimiter checking, i.e., parsing
that involves analyzing a source program syntactically. It is also
called parenthesis checking. When the compiler translates a source
program written in some programming language such as C, C++ to
a machine language, it parses the program into multiple individual
parts such as variable names, keywords, etc. By scanning from left
to right. The main problem encountered while translating is the
unmatched delimiters. We make use of different types of delimiters
83
include the parenthesis checking (,), curly braces {,} and square
brackets [,], and common delimiters /* and */. Every opening
delimiter must match a closing delimiter, i.e., every opening
parenthesis should be followed by a matching closing parenthesis.
84
Stack using linked list
Stack using an array
- drawback
If we implement the stack using an array, we need to specify the array size at the
beginning(at compile time).
We can't change the
izesof an array at runtime. So, it will only work for a fixed number
of elements.
Solution
We can implement the stack using the linked list.
In the linked list, we can change its size at runtime.
#include<stdio.h>
#include<stdlib.h>
void
push(
);
void
pop()
;
void
displ
ay();
struc
t
node
85
{ in
t val;
struct
node
*next
;
};
struct node *head;
void main ()
{
int choice=0; printf("\n*********Stack
operations using linked list*********\n");
printf("\n----------------------------------------------\n");
while(choice != 4)
{
printf("\n\nChose one from the
below options...\n"); printf("\
n1.Push\n2.Pop\n3.Show\n4.Exit");
printf("\n Enter your choice \n");
scanf("%d",&choice);
switch(choice)
c
a
s
e
1
:
{
push();
break;
} case
2: {
pop();
86
break;
}
case 3:
{ d
isplay();
break;
}
case 4:
{ p
rintf("Exiting...
.");
break;
}
defa
ult:
{
printf("Please Enter valid
choice ");
}
};
}
}
void push ()
{
i
n
t
v
a
l
;
87
printf("not able to push the
element");
}
e
l
s
e
{
printf("Enter the
value");
scanf("%d",&val);
if(head==NULL)
{
ptr->val =
val;
ptr -> next =
NULL;
head=ptr;
e
l
s
e
{
ptr->val =
val;
ptr->next =
head;
head=ptr;
}
printf("Item pushed");
}
}
88
void pop()
{ in
t item;
struct
node
*ptr;
if (head
==
NULL)
{
printf("Underflow");
}
e
l
s
e
{
item = head->val;
ptr = head;
head = head->next;
free(ptr);
printf("Item
popped");
}
}
void display()
{ i
nt i;
struct
node
*ptr;
ptr=h
ead;
if(ptr
==
NULL)
89
{
printf("Stack is empty\n");
e
l
s
e
{
printf("Printing Stack elements \n");
while(ptr!=NULL)
{
printf("%d\
n",ptr->val);
ptr = ptr->next;
}
}
}
Queue
1.A queue can be defined as an ordered list which enables insert
operations to be performed at one end called REAR and delete
operations to be performed at another end called FRONT.
3.For example, people waiting in line for a rail ticket form a queue.
90
Applications of Queue
Due to the fact that queue performs actions on first in first out
basis which is quite fair for the ordering of actions. There are
various applications of queues discussed as below.
1. Queues are widely used as waiting lists for a single shared resource
like printer, disk, CPU.
2. Queues are used in asynchronous transfer of data (where data is not
being transferred at the same rate between two processes) for eg.
pipes, file IO, sockets.
3. Queues are used as buffers in most of the applications like MP3
media player, CD player, etc.
4. Queue are used to maintain the play list in media players in order to
add and remove the songs from the play-list.
5. Queues are used in operating systems for handling interrupts.
Complexity
Time Complexity
Data Space
Structure Compleity
Queue θ(n) θ(n) θ(1) θ(1) O(n) O(n) O(1) O(1) O(n)
91
Operations on Queue
Enqueue Operation
• check if the queue is full
for the first element, set the value of FRONT to 0
•
• increase the index by 1
REAR
Dequeue Operation
• check if the queue is empty
92
return the value pointed by FRONT
•
• increase the index by 1
FRONT
• for the last element, reset the values of FRONT and REAR to -1
Algorithm
o Step1: IFREAR=MAX-1
WriteOVERFLOW
Gotostep
[END OF IF] o
Step2: IFFRONT=-
1andREAR=-1
SETFRONT=REAR=0
ELSE
SETREAR=REAR+1
[END OF IF] o Step
3: Set QUEUE[REAR]
= NUM o Step 4: EXIT
Otherwise, keep increasing the value of front and return the item
stored at the front end of the queue at each time.
93
Algorithm
o Step1: IFFRONT=-1orFRONT>REAR
WriteUNDERFLOW
ELSE
SETVAL=QUEUE[FRONT]
SETFRONT=FRONT+1
[
END
OF
IF] o
Ste
p 2:
EXI
T
Implementation of Queue
There are two ways of implementing the Queue:
94
Menu driven program to implement queue using array
#include<stdio.h>
#include
<stdlib.h
>
#define
maxsize 5
void
insert();
void
delete();
void
display();
int front
= -1, rear
= -1; int
queue[ma
xsize];
void
main ()
{
int choice;
while(choice
!= 4)
{
printf("\n*************************Main Menu*****************************\n");
printf("\
n=================================================
======
==========\n");
printf("\n1.insert an element\n2.Delete an element\n3.Display
the queue\n4.Exit\n"); printf("\nEnter your choice ?");
scanf("%d",&choice); switch(choice)
95
{
ca
se
1:
ins
ert(
);
br
ea
k;
ca
se
2:
del
ete
();
br
ea
k;
ca
se
3:
dis
pla
y();
br
ea
k;
ca
se
4:
exi
t(0)
;
br
ea
k;
def
aul
t:
printf("\nEnter valid choice??\n");
96
}
}
}
void insert()
{ int item;
printf("\nEnter the
element\n");
scanf("\n
%d",&item);
if(rear ==
maxsize-1)
{
printf("\
nOVERFLOW\n");
return;
}
if(front == -1 && rear == -1)
{
fr
o
n
t
=
0;
r
e
a
r
=
0;
e
l
s
e
{
97
rear = rear+1;
}
queue[rear] = item;
printf("\nValue
inserted ");
}
void delete()
{
int item; if
(front == -1 ||
front > rear)
{
printf("\
nUNDERFLOW\n");
return;
e
l
s
e
{
item =
queue[front];
if(front == rear)
{
front
=-
1;
rear
=-
1;
}
else
98
{
front = front + 1;
}
printf("\nvalue deleted ");
}
void display()
{
int
i;
if(r
ea
r
=
=-
1)
{
printf("\nEmpty queue\n");
e
l
s
e
99
Drawback of array implementation
Although, the technique of creating a queue is easy, but there are
some drawbacks of using this technique to implement a queue.
100
Operation on Linked Queue
There are two basic operations which can be implemented on the
Algorithm
o Step 1: Allocate the space
for the new node PTR o Step 2:
SET PTR -> DATA = VAL o
Step3: IFFRONT=NULL
SETFRONT=REAR=PTR
SETFRONT->NEXT=REAR->NEXT=NULL
ELSE
SETREAR->NEXT=PTR
SETREAR=PTR
SETREAR->NEXT=NULL
[
END
OF
IF] o
Ste
p 4:
END
Deletion
Deletion operation removes the element that is first inserted
among all the queue elements. Firstly, we need to check either the
list is empty or not. The condition front == NULL becomes true if
the list is empty, in this case , we simply write underflow on the
console and make exit.
101
Otherwise, we will delete the element that is pointed by the pointer
front. For this purpose, copy the node pointed by the front pointer
into the pointer ptr. Now, shift the front pointer, point to its next
node and free the node pointed by the node ptr.
Algorithm
o Step1: IFFRONT=NULL
Write"Underflow"
GotoStep5
[END OF IF o Step
2: SET PTR = FRONT o
Step 3: SET
FRONT = FRONT -> NEXT
o Step 4: FREE PTR
o Step 5: END
{
ca
se
1:
ins
ert(
);
br
ea
k;
ca
se
2:
del
ete
();
br
103
ea
k;
ca
se
3:
dis
pla
y();
br
ea
k;
ca
se
4:
exi
t(0)
;
br
ea
k;
def
aul
t:
printf("\nEnter valid choice??\n");
}
}
}
void insert()
{
struct
node
*ptr;
int item;
104
printf("\
nOVERFLOW\n");
return;
e
l
s
e
{
printf("\nEnter
value?\n");
scanf("%d",&i
tem); ptr
-> data =
item;
if(front ==
NULL)
{ fron
t = ptr;
rear = ptr;
front -> next
= NULL;
rear -> next =
NULL;
e
l
s
e
{
rear ->
next = ptr;
105
rear = ptr;
rear->next =
NULL;
}
}
}
void delete ()
{
struct
node *ptr;
if(front ==
NULL)
{
printf("\
nUNDERFLOW\n");
return;
e
l
s
e
{ ptr
= front;
front =
front ->
next;
free(ptr);
}
}
void display()
{
struct
node
*ptr;
ptr =
106
front;
if(front
== NULL)
{
printf("\nEmpty queue\n");
e
l
s
e
{ printf("\nprinting
values .....\n");
while(ptr != NULL)
{
printf("\n%d\
n",ptr -> data);
ptr = ptr -> next;
}
}
}
Circular Queue
There was one limitation in the array implementation of Queue
. If the rear reaches to the end position of the Queue then there might be possibility that
some vacant spaces are left in the beginning which cannot be utilized. So, to overcome such
limitations, the concept of the circular queue was introduced.
107
What is a Circular Queue?
A circular queue is similar to a linear queue as it is also based on
the FIFO (First In First Out) principle except that the last position is
connected to the first position in a circular queue that forms a
circle. It is also known as a Ring Buffer.
108
queue, the memory is managed efficiently by placing the elements
in a location which is unused.
o CPU Scheduling: The operating system also uses the circular
queue to insert the processes and then execute them.
o Traffic system: In a computer-control traffic system, traffic light is
one of the best examples of the circular queue. Each light of traffic
light gets ON one by one after every interval of time. Like red light
gets ON for one minute then yellow light for one minute and then
green light. After green light, the red light gets ON.
Deque
o The dequeue stands for Double Ended Queue. In the queue,
the insertion takes place from one end while the deletion
takes place from another end. The end at which the insertion
occurs is known as the rear end whereas the end at which
the deletion occurs is known as front end.
Priority Queue
A priority queue is a special type of queue in which each element is
associated with a priority and is served according to its priority. If
elements with the same priority occur, they are served according to
their order in the queue.
109
Priority Queue Representation
List ADT
• The data is generally stored in key sequence in a list which has
a head structure consisting of count, pointers and address of
compare function needed to compare the data in the list.
Stack ADT
• In Stack ADT Implementation instead of data being stored in each node, the
pointer to data is stored.
The program allocates memory for the data and address is passed to
the stack ADT.
110
A Stack contains elements of the same type arranged in sequential
order. All operations take place at a single end that is top of the stack
and following operations can be performed:
• push() – Insert an element at one end of the stack called top.
• pop() – Remove and return the element at the top of the stack, if it is
not empty.
• peek() – Return the element at the top of the stack without removing
it, if the stack is not empty.
• size() – Return the number of elements in the stack.
• isEmpty() – Return true if the stack is empty, otherwise return false.
• isFull() – Return true if the stack is full, otherwise return false.
Queue ADT
• The queue abstract data type (ADT) follows the basic design of the
stack abstract data type.
111
• Each node contains a void pointer to the data and the link pointer to
the next element in the queue. The program’s responsibility is to
allocate memory for storing the data.
UNIT-II
112
113
114
115
5.Rehashing
116
117
118
119
120
121
122
Skip list:
A skip list is a probabilistic data structure. The skip list is used to store a sorted list
of elements or data with a linked list. It allows the process of the elements or data
to view efficiently. In one single step, it skips several elements of the entire list,
which is why it is known as a skip list.
The skip list is an extended version of the linked list. It allows the user to search,
remove, and insert the element very quickly.
123
It is built in two layers:
The lowest layer of the skip list is a common sorted linked list, and the top layers
of the skip list are like an "express line" where the elements are skipped.
Let's take an example to understand the working of the skip list. In this example,
we have 14 nodes, such that these nodes are divided into two layers, as shown in
the diagram.
The lower layer is a common line that links all nodes, and the top layer is an
express line that links only the main nodes, as you can see in the diagram.
Suppose you want to find 47 in this example. You will start the search from the
first node of the express line and continue running on the express line until you
find a node that is equal a 47 or more than 47.
You can see in the example that 47 does not exist in the express line, so you search
for a node of less than 47, which is 40. Now, you go to the normal line with the
help of 40, and search the 47, as shown in the diagram.
124
Insertion operation: It is used to add a new node to a particular location in a
specific situation.
a = L → header
update[i] = a
a = a → forward[0]
lvl = random_Level()
update[i] = L → header
L → level = lvl
for i = 0 to level do
a = L → header
update[i] = a
a = a → forward[0]
for i = 0 to L → level do
free(a)
L → level = L → level - 1
a = L → header
126
loop invariant: a → key level down to 0 do.
a = a → forward[0]
Example 1: Create a skip list, we want to insert these following keys in the empty
skip list.
6 with level 1.
29 with level 1.
22 with level 4.
9 with level 3.
17 with level 1.
4 with level 2.
Ans:
127
Step 2: Insert 29 with level 1
128
Step 5: Insert 17 with level 1
129
Example 2: Consider this example where we want to search for key 17.
Ans:
130
Advantages of the Skip list
If you want to insert a new node in the skip list, then it will insert the node very
fast because there are no rotations in the skip list.
The skip list is simple to implement as compared to the hash table and the binary
search tree.
It is very simple to find a node in the list because it stores the nodes in sorted form.
The skip list algorithm can be modified very easily in a more specific structure,
such as indexable skip lists, trees, or priority queues.
The skip list searches the node much slower than the linked list.
Dictionary:
131
Dictionary is one of the important Data Structures that is usually used to store data
in the key-value format. Each element presents in a dictionary data structure
compulsorily have a key and some value is associated with that particular key.
In other words, we can also say that Dictionary data structure is used to store the
data in key-value pairs.
In Dictionary or associative array, the relation or association between the key and
the value is known as the mapping.
example
'Nova' : 84,
'Charlie' : 22,
'Henry' : 75,
'Roxanne' : 92,
'Elsa' : 29 }
Instead of using the numerical index of the data we can use the dictionary names
to return values
search(K key)
132
(returns the value associated with the given key) –
Operations on dictionaries
insert or update a value (typically, if the key does not exist in the dictionary, the
key-value pair is inserted; if the key already exists, its corresponding value is
overwritten with the new one)
Note that items in a dictionary are unordered, so loops over dictionaries will
return items in an arbitrary order
Implementations on dictionaries
• hash tables
• AVL trees
• red-black trees
• DataItem Define a data item having some data, and key based on which search is
to be conducted in hashtable.
struct DataItem
int data;
int key;
};
Hash Method Define a hashing method to compute the hash code of the key of the
data item.
134
135
struct nlist { /* table entry: */
};
unsigned hashval;
hashval = *s + 31 * hashval;
if (strcmp(s, np->name) == 0)
char *strdup(char *);/* install: put (name, defn) in hashtab */struct nlist
*install(char *name, char *defn)
unsigned hashval;
return NULL;
hashval = hash(name);
np->next = hashtab[hashval];
hashtab[hashval] = np;
return NULL;
return np;
137
{
char *p;
if (p != NULL)
strcpy(p, s);
return p;
138
Unit 3
PRELIMINARIES :
TREE : A tree is a finite set of one or more nodes such that there is a specially
designated node called the Root, and zero or more non empty sub trees T1, T2
.........................................................................................................................................................................................................Tk, each of
B C D E
F G H I J
K L M
139
NODE : Item of Information.
LEAF : A node which doesn‘t have children is called leaf or Terminal node. Here B, K,
L, G, H, M, J are leafs.
SIBLINGS : Children of the same parents are said to be siblings, Here B, C, D, E are
siblings, F, G are siblings. Similarly I, J & K, L are siblings.
PATH : A path from node n1 to nk is defined as a sequence of nodes n1, n2,n3 nk suchthat
ni is the parent of ni+1. for 1 i k . There is exactly only one path from each
node to root.
In fig 3.1.1 path from A to L is A, C, F, L. where A is the parent for C, C is the
parent of F and F is the parent of L.
LENGTH : The length is defined as the number of edges on the path.
In fig 3. 1.1 the length for the path A to L is 3.
DEGREE : The number of subtrees of a node is called its degree.
140
In fig 3.1.1
Degree of A is 4 Degree of C is 2
Degree of D is 1 Degree of H is
0.
* The degree of the tree is the maximum degree of any node in the tree.
In fig 3.1.1 the degree of the tree is 4.
LEVEL : The level of a node is defined by initially letting the root be at level one, if a node is at level L
then its children are at level L + 1.
Level of A is 1.
Level of B, C, D, is 2. Level of F, G, H, I, J
is 3 Level of K, L, M is 4.
DEPTH : For any node n, the depth of n is the length of the unique path from root to n.
HEIGHT : For any node n, the height of the node n is the length of the longest path from n to the leaf.
Degree of a Node
Binary Tree
141
Binary Tree Representation
A node of a binary tree is represented by a structure containing a data part and two
pointers to other structures of the same type.
struct node
int data;
struct node
*left; struct
node *right;
};
Binary search tree is a data structure that quickly allows us to maintain a sorted list of
numbers.
It is called a binary tree because each tree node has a maximum of two children.
It is called a search tree because it can be used to search for the presence of a
number in O(log(n)) time.
The properties that separate a binary search tree from a regular binary tree is
1. All nodes of left subtree are less than the root node
2. All nodes of right subtree are more than the root node
Insert Operation
The very first insertion creates the tree. Afterwards, whenever an element is to
be inserted, first locate its proper location. Start searching from the root node,
then if the data is less than the key value, search for the empty location in the
left subtree and insert the data. Otherwise, search for the empty location in the
right subtree and insert the data.
143
Algorithm
If root is NULL
else
144
Implementation
tempNode->data = data;
tempNode->leftChild = NULL;
tempNode->rightChild = NULL;
root = tempNode;
} else {
current = root;
parent = NULL;
while(1) {
parent = current;
current = current->leftChild;
parent->leftChild = tempNode;
return;
Search Operation
Whenever an element is to be searched, start searching from the root node,
then if the data is less than the key value, search for the element in the left
subtree. Otherwise, search for the element in the right subtree. Follow the same
algorithm for each node.
Algorithm
If root.data is equal to search.data
return root 145
else
else
If data found
return node
endwhile
The implementation of this algorithm should look like this.
while(current->data != data) {
if(current != NULL)
printf("%d ",current->data);
current = current->leftChild;
current = current->rightChild;
Deletion
There are 3 cases that can happen when you are trying to delete a node.
If it has,
1. No subtree (no children): This one is the easiest one. You can simply
just delete the node, without any additional actions required.
146
2. One subtree (one child): You have to make sure that after the node is
deleted, its child is then connected to the deleted node's parent.
147
3. Two subtrees (two children): You have to find and replace the node
you want to delete with its inorder successor (the leftmost node in the
right subtree).
o Delete Operation
else
if (root->left == NULL)
return temp;
return temp;
root->key = temp->key;
148
}
return root;
149
TREE TRAVERSALS:
Traversal is a process to visit all the nodes of a tree and may print their values
too. Because, all nodes are connected via edges (links) we always start from the
root (head) node. That is, we cannot randomly access a node in a tree. There
are three ways which we use to traverse a tree −
In-order Traversal
Pre-order Traversal
Post-order Traversal
Generally, we traverse a tree to search or locate a given item or key in the tree
or to print all the values it contains.
In-order Traversal
In this traversal method, the left subtree is visited first, then the root and later
the right sub- tree. We should always remember that every node may represent
a subtree itself.
If a binary tree is traversed in-order, the output will produce sorted key values in
an ascending order.
We start from A, and following in-order traversal, we move to its left subtree B.
B is also traversed in-order. The process goes on until all the nodes are visited.
The output of inorder traversal of this tree will be −
o D→B→E→A→F→C→G
Algorithm
Until all nodes are traversed −
150
void inorder_traversal(struct node* root) {
if(root != NULL) {
inorder_traversal(root->leftChild);
printf("%d ",root->data);
inorder_traversal(root->rightChild);
Pre-order Traversal
In this traversal method, the root node is visited first, then the left subtree
and finally the right subtree.
151
We start from A, and following pre-order traversal, we first visit A itself and then
move to its left subtree B. B is also traversed pre-order. The process goes on
until all the nodes are visited. The output of pre-order traversal of this tree will
be −
o A→B→D→E→C→F→G
Algorithm
Until all nodes are traversed −
printf("%d ",root->data);
pre_order_traversal(root->leftChild);
pre_order_traversal(root->rightChild);
Post-order Traversal
In this traversal method, the root node is visited last, hence the name. First we
traverse the left subtree, then the right subtree and finally the root node.
We start from A, and following Post-order traversal, we first visit the left subtree
B. B is also traversed post-order. The process goes on until all the nodes are
visited. The output of post-order traversal of this tree will be −
152
o D→E→B→F→G→C→A
Algorithm
Until all nodes are traversed −
post_order_traversal(root->leftChild);
post_order_traversal(root->rightChild);
153
Non Linear Data Structures - Trees 3.1
Example : -
Traverse the given tree using inorder, preorder and postorder
traversals. (1)
+ -
/
A B C
D E
Fig. 3.2.7
Inorder :A+B*C-D/E
Preorder :*+AB-C/DE
Postorder : A B + C D E / -*
20
10 30
5 15 25 40
Fig. 3.2.8
Inorder :5 10 15 20 25 30 40
Preorder : 20 10 5 15 30 25 40
Postorder :5 15 10 25 40 30 20
154
Non Linear Data Structures - Trees 3.1
3.1 BINARY
TREE
Definition
:-
Binary Tree is a tree in which no node can have more than two children.
Maximum number of nodes at level i of a binary tree is 2i+1.
15
18 20
8 5 10
int Element;
};
COMPARISON
BETWEEN GENERAL
TREE & BINARY
TREE
155
Non Linear Data Structures - Trees 3.1
15 A
18 20 10 B C
2 3
4 5 6 7
8 9 10 11 12 13 14 15
Fig. 3.3.2 A
Complete Full Binary
Binary Tree : Tree
A complete binary tree height h has between 2h and 2h+1 - 1 In the bottom level the
of
elements should be filled from left to right.
156
Non Linear Data Structures - Trees 3.1
2 3
4 5 6 7
8 9
157
Non Linear Data Structures - Trees 3.1
B C A B C D E F G
1 2 3 4 5 6 7
D E F G
The elements are represented using pointers. Each node in linked representation has
three fields, namely,
* Pointer to the left
* Data field
* Pointer to the right subtree
In leaf nodes, both the pointer fields are assigned as NULL.
A
A
B C
C
F
B G
D E F D E G
Non Linear Data Structures - Trees 3.1
Struct cellspace
{
Exam
ple 1:
Righ
t Label
Lef
sibling
t
chil
d
A
B C
A D
B C
D Null D Null
2 Cellspace
Null B 11
5
5
5 A Null
1 2 C Null
0
Non Linear Data Structures - Trees 3.1
Lef
Label
t
mos
Right
t
siblin
chil
g
d
B C
D E F G H
I J K
B C
F H
D E G
I J K
1 2 B 10
2 15 D 3
14 E 11
3
1 A Null
7
13 C Null
Null F Null
Null H Null
T
7
Null G 12l
10
Null K Null
11 Null I 16
Null J Null
12
13
14
15
16
Example : -
ab + c *
The first two symbols are operand, so create a one node tree and push the pointer on to the
stack.
a b
a b
Fig. 3.4.2 (b)
Next the operand C is read, so a one node tree is created and the pointer to it is pushed
onto the stack.
stack + c
.
a b
Now ‗*‘ is read, so two trees are merged and the pointer to the final tree is pushed onto the
+ c
a b
Treap - Randomized data structure used in wireless networking and memory allocation.
T-tree - Though most databases use some form of B-tree to store data on the drive,
databases which keep all (most) their data in memory often use T-trees to do so.
BTree :
19
10 22
7 20
21
Fig. 3.6.1
Binary Search Tree Comparision
Between Binary Tree & Binary
Search Tree
Binary Tree
Binary Search Tree
* A tree is said to be a binary * A binary search tree is a binary tree in which the
tree if it has atmost two key values in the left node is less than the root
childrens. and the keyvalues in the right node is greater
than the root.
7 8 3 10
3 10 12 1 7 12
1 8
5 10
5 10
15
5 10
5 10
15
15
20
20
18
After 20 After 18
Find : -
* Otherwise, Check the value X with the root node value (i.e. T data)
(1) If X is equal to T data, return T.
(2) If X is less than T data, Traverse the left of T recursively.
(3) If X is greater than T data, traverse the right of T recursively.
8 8
5 15 5 15
3 10 3 10
5 15 5 15
3 10 3 10
5 15
Find Min :
This operation returns the position of the smallest element in the tree.
To perform FindMin, start at the root and go left as long as there is a left child. The
stopping point is the smallest element.
(b) T! = NULL and T left!=NULL, Traverse left Traverse left
10
5 15
Min T
3 8
if (T! = NULL)
return T;
FindMax
FindMax routine return the position of largest elements in the tree. To perform a
FindMax, start at the root and go right as long as there is a right child. The stopping point is the
largest element.
Non Linear Data Structures - Trees 3.2
if (T = = NULL)
return NULL ;
return T;
}
Recursive Routine for Findmax
Example :-
Root T
10 10
8 15 8 15 T
5 20 5 20
10
8 15 Max
5 20 T
Non Linear Data Structures - Trees 3.2
BST DELETION:
Deletion operation is the complex operation in the Binary search tree. To delete an
element, consider the following three possibilities.
Delete : 8
4 10
4 10
If the node has one child, it can be deleted by adjusting its parent pointer that points to
its child node.
Non Linear Data Structures - Trees 3.2
To Delete 5
7
4 10
4 10
3 5
3 6
6
To delete 5, the pointer currently pointing the node 5 is now made to its child node 6.
Example 1 :
To Delete 5 : 10
5 15
3 8
(a)
Non Linear Data Structures - Trees 3.2
* Now the value 7 is replaced in the position of 5.
10
7 15
(b)
10
3 8
15
(c)
10 25
Example 2 : - To Delete 25
20 35
5
30
32
Non Linear Data Structures - Trees 3.2
(a)
Non Linear Data Structures - Trees 3.2
10 30
20 35
5
32
(b)
15
32
(c)
Non Linear Data Structures - Trees 3.2
15
10 30
20 35
5
Binary Search Tree after deleting 25
32
(d)
Non Linear Data Structures - Trees 3.2
AVL TREE (ADELSON - VELSKILLAND LANDIS) Non Linear Data Structures - Trees 3.2
AVL Tree
AVL Tree is invented by GM Adelson - Velsky and EM Landis in 1962. The tree is named AVL in
honour of its inventors.
AVL Tree can be defined as height balanced binary search tree in which each node is associated with
a balance factor which is calculated by subtracting the height of its right sub- tree from that of its left
sub-tree.
Tree is said to be balanced if balance factor of each node is in between -1 to 1, otherwise, the
tree will be unbalanced and need to be balanced.
If balance factor of any node is 0, it means that the left sub-tree and right sub-tree contain equal
height.
If balance factor of any node is -1, it means that the left sub-tree is one level lower than the right
sub-tree.
An AVL tree is given in the following figure. We can see that, balance factor associated with each
node is in between -1 and +1. therefore, it is an example of AVL tree.
Operations on AVL tree Non Linear Data Structures - Trees 3.2
SN Operation Description
1 Insertion Insertion in AVL tree is performed in the same way as it is performed in a binary
search tree. However, it may lead to violation in the AVL tree
property and therefore the tree may
Nonneed
Linearbalancing. The
Data Structures tree can be balanced
- Trees 3.2
by applying rotations.
2 Deletion Deletion can also be performed in the same way as it is performed in a binary
search tree. Deletion may also disturb the balance of the tree therefore, various
types of rotations are used to rebalance the tree.
AVL tree controls the height of the binary search tree by not letting it to be skewed. The time taken
for all operations in a binary search tree of height h is O(h). However, it can be extended to O(n) if
the BST becomes skewed (i.e. worst case). By limiting this height to log n, AVL tree imposes an upper
bound on each operation to be O(log n) where n is the number of nodes.
AVL Rotations
We perform rotation in AVL tree only in case if Balance Factor is other than -1, 0, and 1. There are
basically four types of rotations which are as follows:
The first two rotations LL and RR are single rotations and the next two rotations LR and RL are
double rotations
1. RR Rotation
When BST becomes unbalanced, due to a node is inserted into the right subtree of the right subtree
of A, then we perform RR rotation, RR rotation is an anticlockwise rotation, which is applied on the
edge below a node having balance factor -2
Non Linear Data Structures - Trees 3.2
In above example, node A has balance factor -2 because a node C is inserted in the right subtree of A
right subtree. We perform the RR rotation on the edge below A.
2. LL Rotation
When BST becomes unbalanced, due to a node is inserted into the left subtree of the left subtree of
C, then we perform LL rotation, LL rotation is clockwise rotation, which is applied on the edge below
a node having balance factor 2.
In above example, node C has balance factor 2 because a node A is inserted in the left subtree of C
left subtree. We perform the LL rotation on the edge below A.
3. LR Rotation
Double rotations are bit tougher than single rotation which has already explained above. LR rotation
= RR rotation + LL rotation, i.e., first RR rotation is performed on subtree and then LL rotation is
performed on full tree, by full tree we mean the first node from the path of inserted node whose
balance factor is other than -1, 0, or 1.
State Action
A node B has been inserted intoNon
theLinear
rightData
subtree of A
Structures the left subtree of
- Trees 3.2C,
because of which C has become an unbalanced node having balance factor 2.
This case is L R rotation where: Inserted node is in the right subtree of left
subtree of C
Balance factor of each node is now either -1, 0, or 1, i.e. BST is balanced now.
4. RL Rotation
As already discussed, that double rotations are bit tougher than single rotation which has already
explained above. R L rotation = LL rotation + RR rotation, i.e., first LL rotation is
performed on subtree and then RR rotation is performed on full tree, by full tree we mean the
Non Linear Data Structures - Trees 3.2
first node from the path of inserted node whose balance factor is other than -1, 0, or 1.
State Action
A node B has been inserted into the left subtree of C the right subtree of A, because
of which A has become an unbalanced node having balance factor -
2. This case is RL rotation where: Inserted node is in the left subtree of right subtree
of A
After performing LL rotation, node A is still unbalanced, i.e. having balance factor -
2, which is because of the right-subtree of the right-subtree node A.
If < rootKey, call insertion algorithm on the left subtree of the current node until the
leaf node is reached.
Else if > rootKey, call insertion algorithm on the right subtree of current
node until the leaf node is reached.
newNod
e
newKe rootKe
y y
newKe
y
newKe
y
a. If
balanceFact > 1, it means the height of the left subtree is greater than that of
or
the right subtree. So, do a right rotation or left-right rotation
a. If newNodeKe < leftChildKe do right rotation. Non Linear Data Structures - Trees 3.2
y y
b. Else, do left-right rotation.
b. If < -1, it means the height of the right subtree is greater than that of
the left subtree. So, do right rotation or right-left rotation
balanceFact
or
a. If is the leaf
nodeToBeDelet node (ie. does not have any child), then
ed
remove nodeToBeDeleted.
nodeToBeDelet nodeToBeDelet
b. If has one child, then substitute the contents of
ed ed with
that of the child. Remove the child.
3. Rebalance the tree if the balance factor of any of the nodes is not equal to -1, 0 or 1.
a. If
balanceFact
of currentNod > 1,
a. If or e
balanceFact of leftChil >= 0, do right rotation.
or d
b. Right-rotate for balancing the tree
c. Else do left-right rotation.
A full Binary tree is a special type of binary tree in which every parent
node/internal node has either two or no children.
A perfect binary tree is a type of binary tree in which every internal node has exactly two
child nodes and all the leaf nodes are at the same level.
3. The last leaf element might not have a right sibling i.e. a complete binary tree
doesn't have to be a full binary tree.
Complete Binary Tree
A degenerate or pathological tree is the tree having a single child either left or right.
It is a type of binary tree in which the difference between the height of the left and the
right subtree for each node is either 0 or 1.
Structure The structure of the binary tree is that the first The binary search tree is one of the
node or the topmost node is known as the root types of binary tree that has the value
node. Each node in a binary tree contains the of all the nodes in the left subtree
left pointer and the right pointer. The left lesser or equal to the root node, and the
pointer contains the address of the left subtree, value of all the nodes in a right subtree
whereas right pointer contains the address of are greater than or equal to the value of
right subtree. the root node.
Operations The operations that can be implemented on a Binary search trees are the sorted
binary tree are insertion, deletion, and binary trees that provide fast insertion,
traversal. deletion and search. Lookups mainly
implement binary search as all the keys
are arranged in sorted order.
types Four types of binary trees are Full Binary Tree, There are different types of binary
Complete Binary Tree, Perfect Binary Tree, search trees such as AVL trees, Splay
and Extended Binary Tree. tree, Tango trees, etc.
Since in Binary tree every node has at most 2 children, next level
would have twice nodes, i.e. 2 *
Non Linear Data Structures - Trees 3.20
2) 2) The Maximum number of nodes in a binary tree of
height ‘h’ is 2h – 1.
Red-Black tree
The red-Black tree is a binary search tree. Each node in the Red-black tree
contains an extra bit that represents a color to ensure that the tree is
balanced during any operations performed on the tree like insertion,
deletion, etc
The Red-Black tree is used because the AVL tree requires many
rotations when the tree is large, whereas the Red-Black tree requires a
maximum of two rotations to balance the tree.
The main difference between the AVL tree and the Red-Black tree is
that the AVL tree is strictly balanced, while the Red-Black tree is not
completely height-balanced.
So, the AVL tree is more balanced than the Red-Black tree, but the
Red-Black tree guarantees O(log2n) time for all operations like
insertion, deletion, and searching.
As the name suggests that the node is either colored in Red or Black color.
Sometimes no rotation is required, and only recoloring is needed to balance the
tree.
1. If the tree is empty, then we create a new node as a root node with the color
black.
2. If the tree is not empty, then we create a new node as a leaf node with a
color red.
3. If the parent of a new node is black, then exit.
4. If the parent of a new node is Red, then we have to check the color of the
parent's sibling of a new node.
4b) If the color is Red then we recolor the node. We will also check whether
the parents' parent of a new node is the root node or not; if it is not a root
node, we will recolor and recheck the node.
Step 1: Initially, the tree is empty, so we create a new node having value
10. This is the first node of the tree, so it would be the root node of the tree.
As we already discussed, that root node must be black in color, which is
shown below:
Non Linear Data Structures - Trees 3.20
Step 2: The next node is 18. As 18 is greater than 10 so it will come at the
right of 10 as shown below.
We know the second rule of the Red Black tree that if the tree is not empty
then the newly created node will have the Red color. Therefore, node 18 has
a Red color, as shown in the below figure:
Now we verify the third rule of the Red-Black tree, i.e., the parent of the new
node is black or not. In the above figure, the parent of the node is black in
color; therefore, it is a Red-Black tree.
Step 3: Now, we create the new node having value 7 with Red color. As 7 is
less than 10, so it will come at the left of 10 as shown below.
Now we verify the third rule of the Red-Black tree, i.e., the parent of the new
node is black or not. As we can observe, the parent of the node 7 is black in
color, and it obeys the Red-Black tree's properties.
Step 4: The next element is 15, and 15 is greater than 10, but less than 18,
so the new node will be created at the left of node 18. The node 15 would be
Red in color as the tree is not empty.
The above tree violates the property of the Red-Black tree as it has Red-red
parent-child relationship. Now we have to apply some rule to make a Red-
Black tree. The rule 4 says that if the new node's parent is Red, then
we have to check the color of the parent's sibling of a new node. The
new node is node 15; the parent of the new node is node 18 and the sibling
of the parent node is node 7. As the color of the parent's sibling is Red in
color, so we apply the rule 4a. The rule 4a says that we have to recolor both
Non Linear Data Structures - Trees 3.20
the parent and parent's sibling node. So, both the nodes, i.e., 7 and 18,
would be recolored as shown in the below figure.
We also have to check whether the parent's parent of the new node is the
root node or not. As we can observe in the above figure, the parent's parent
of a new node is the root node, so we do not need to recolor it.
Step 5: The next element is 16. As 16 is greater than 10 but less than 18
and greater than 15, so node 16 will come at the right of node 15. The tree
is not empty; node 16 would be Red in color, as shown in the below figure:
In the above figure, we can observe that it violates the property of the
parent-child relationship as it has a red-red parent-child relationship. We
have to apply some rules to make a Red-Black tree. Since the new node's
parent is Red color, and the parent of the new node has no sibling, so
rule 4a will be applied. The rule 4a says that some rotations and recoloring
would be performed on the tree.
Since node 16 is right of node 15 and the parent of node 15 is node 18. Node
15 is the left of node 18. Here we have an LR relationship, so we require to
perform two rotations. First, we will perform left, and then we will perform
the right rotation. The left rotation would be performed on nodes 15 and 16,
where node 16 will move upward, and node 15 will move downward. Once
the left rotation is performed, the tree looks like as shown in the below
figure:
Non Linear Data Structures - Trees 3.20
After rotation, node 16 and node 18 would be recolored; the color of node 16
is red, so it will change to black, and the color of node 18 is black, so it will
change to a red color as shown in the below figure:
Step 6: The next element is 30. Node 30 is inserted at the right of node 18.
As the tree is not empty, so the color of node 30 would be red.
Non Linear Data Structures - Trees 3.20
The color of the parent and parent's sibling of a new node is Red, so rule 4b
is applied. In rule 4b, we have to do only recoloring, i.e., no rotations are
required. The color of both the parent (node 18) and parent's sibling (node
15) would become black, as shown in the below image.
We also have to check the parent's parent of the new node, whether it is a
root node or not. The parent's parent of the new node, i.e., node 30 is node
16 and node 16 is not a root node, so we will recolor the node 16 and
changes to the Red color. The parent of node 16 is node 10, and it is not in
Red color, so there is no Red-red conflict.
Step 7: The next element is 25, which we have to insert in a tree. Since 25
is greater than 10, 16, 18 but less than 30; so, it will come at the left of node
30. As the tree is not empty, node 25 would be in Red color. Here Red-red
conflict occurs as the parent of the newly created is Red color.
Step 8: The next element is 40. Since 40 is greater than 10, 16, 18, 25, and
30, so node 40 will come at the right of node 30. As the tree is not empty,
node 40 would be Red in color. There is a Red-red conflict between nodes 40
and 30, so rule 4b will be applied.
As the color of parent and parent's sibling node of a new node is Red so
recoloring would be performed. The color of both the nodes would become
black, as shown in the below image.
After recoloring, we also have to check the parent's parent of a new node,
i.e., 25, which is not a root node, so recoloring would be performed, and the
color of node 25 changes to Red.
Non Linear Data Structures - Trees 3.21
After recoloring, red-red conflict occurs between nodes 25 and 16. Now node
25 would be considered as the new node. Since the parent of node 25 is red
in color, and the parent's sibling is black in color, rule 4a would be applied.
Since 25 is at the right of the node 16 and 16 is at the right of its parent, so
there is an RR relationship. In the RR relationship, left rotation is performed.
After left rotation, the median element 16 would be the root node, as shown
in the below figure.
After rotation, recoloring is performed on nodes 16 and 10. The color of node
10 and node 16 changes to Red and Black, respectively as shown in the
below figure.
Step 9: The next element is 60. Since 60 is greater than 16, 25, 30, and 40,
so node 60 will come at the right of node 40. As the tree is not empty, the
color of node 60 would be Red.
As we can observe in the above tree that there is a Red-red conflict occurs.
The parent node is Red in color, and there is no parent's sibling exists in the
tree, so rule 4a would be applied. The first rotation would be performed. The
RR relationship exists between the nodes, so left rotation would be
performed.
Non Linear Data Structures - Trees 3.21
When left rotation is performed, node 40 will come upwards, and node 30
will come downwards, as shown in the below figure:
After rotation, the recoloring is performed on nodes 30 and 40. The color of
node 30 would become Red, while the color of node 40 would become black.
The above tree is a Red-Black tree as it follows all the Red-Black tree
properties.
Step 2:
Initially, we are having the address of the root node. First, we will apply BST
to search the node. Since 30 is greater than 10 and 20, which means that 30
is the right child of node 20. Node 30 is a leaf node and Red in color, so it is
simply deleted from the tree.
If we want to delete the internal node that has one child. First, replace the
value of the internal node with the value of the child node and then simply
delete the child node.
We cannot delete the internal node; we can only replace the value of that
node with another value. Node 20 is at the right of the root node, and it is
having only one child, node 30. So, node 20 is replaced with a value 30, but
the color of the node would remain the same, i.e., Black. In the end, node 20
(leaf node) is deleted from the tree.
If we want to delete the internal node that has two child nodes. In this case,
we have to decide from which we have to replace the value of the internal
node (either left subtree or right subtree). We have two ways:
Non Linear Data Structures - Trees 3.21
o Inorder predecessor: We will replace with the largest value that
exists in the left subtree.
o Inorder successor: We will replace with the smallest value that
exists in the right subtree.
Suppose we want to delete node 30 from the tree, which is shown below:
Node 30 is at the right of the root node. In this case, we will use the inorder
successor. The value 38 is the smallest value in the right subtree, so we will
replace the value 30 with 38, but the node would remain the same, i.e., Red.
After replacement, the leaf node, i.e., 30, would be deleted from the tree.
Since node 30 is a leaf node and Red in color, we need to delete it (we do
not have to perform any rotations or any recoloring).
Case 2: If the root node is also double black, then simply remove the double
black and make it a single black.
Case 3: If the double black's sibling is black and both its children are black.
We cannot simply delete node 15 from the tree as node 15 is Black in color.
Node 15 has two children, which are nil. So, we replace the 15 value with a
nil value. As node 15 and nil node are black in color, the node becomes
double black after replacement, as shown in the below figure.
In the above tree, we can observe that the double black's sibling is
black in color and its children are nil, which are also black. As the
double black's sibling and its children have black so it cannot give its black
color to neither of these. Now, the double black's parent node is Red so
double black's node add its black color to its parent node. The color of the
node 20 changes to black while the color of the nil node changes to a single
black as shown in the below figure.
After adding the color to its parent node, the color of the double black's
sibling, i.e., node 30 changes to red as shown in the below figure.
In the above tree, we can observe that there is no longer double black's
problem exists, and it is also a Red-Black tree.
Initially, the 15 is replaced with a nil value. After replacement, the node
becomes double black. Since double black's sibling is Red so color of the
node 20 changes to Red and the color of the node 30 changes to Black.
Once the swapping of the color is completed, the rotation towards the
double black would be performed. The node 30 will move upwards and the
node 20 will move downwards as shown in the below figure.
In the above tree, we can observe that double black situation still exists in
the tree. It satisfies the case 3 in which double black's sibling is black as well
as both its children are black. First, we remove the double black from the
node and add the black color to its parent node. At the end, the color of the
double black's sibling, i.e., node 25 changes to Red as shown in the below
figure.
Non Linear Data Structures - Trees 3.21
In the above tree, we can observe that the double black situation has been
resolved. It also satisfies the properties of the Red Black tree.
Case 5: If double black's sibling is black, sibling's child who is far from the
double black is black, but near child to double black is red.
o Swap the color of double black's sibling and the sibling child which is
nearer to the double black node.
o Rotate the sibling in the opposite direction of the double black.
o Apply case 6
First, we replace the value 1 with the nil value. The node becomes double
black as both the nodes, i.e., 1 and nil are black. It satisfies the case 3 that
implies if DB's sibling is black and both its children are black. First, we
remove the double black of the nil node. Since the parent of DB is Black, so
when the black color is added to the parent node then it becomes double
black. After adding the color, the double black's sibling color changes to Red
as shown below.
We can observe in the above screenshot that the double black problem still
exists in the tree. So, we will reapply the cases. We will apply case 5
because the sibling of node 5 is node 30, which is black in color, the child of
node 30, which is far from node 5 is black, and the child of the node 30
which is near to node 5 is Red. In this case, first we will swap the color of
node 30 and node 25 so the color of node 30 changes to Red and the color
of node 25 changes to Black as shown below.
Non Linear Data Structures - Trees 3.21
Once the swapping of the color between the nodes is completed, we need to
rotate the sibling in the opposite direction of the double black node. In this
rotation, the node 30 moves downwards while the node 25 moves upwards
as shown below.
As we can observe in the above tree that double black situation still exists.
So, we need to case 6. Let's first see what is case 6.
Now we will apply case 6 in the above example to solve the double black's
situation.
In the above example, the double black is node 5, and the sibling of node 5
is node 25, which is black in color. The far child of the double black node is
node 30, which is Red in color as shown in the below figure:
First, we will swap the colors of Parent and its sibling. The parent of node 5 is
node 10, and the sibling node is node 25. The colors of both the nodes are
black, so there is no swapping would occur.
Non Linear Data Structures - Trees 3.21
In the second step, we need to rotate the parent in the double black's
direction. After rotation, node 25 will move upwards, whereas node 10 will
move downwards. Once the rotation is performed, the tree would like, as
shown in the below figure:
In the next step, we will remove double black from node 5 and node 5 will
give its black color to the far child, i.e., node 30. Therefore, the color of node
30 changes to black as shown in the below figure.
Splay Tree
Splay trees are the self-balancing or self-adjusted binary search trees.
A splay tree contains the same operations as a Binary search tree, i.e.,
Insertion, deletion and searching, but it also contains one more operation,
i.e., splaying. So. all the operations in the splay tree are followed by
splaying.
Splay trees are not strictly balanced trees, but they are roughly balanced
trees. Let's understand the search operation in the splay-tree.
To search any element in the splay tree, first, we will perform the standard
binary search tree operation. As 7 is less than 10 so we will come to the left
of the root node. After performing the search operation, we need to perform
splaying. Here splaying means that the operation that we are performing on
any element should become the root node after performing some
rearrangements. The rearrangement of the tree will be done through the
rotations.
Note: The splay tree can be defined as the self-adjusted tree in which any operation
performed on the element would rearrange the tree so that the element on which operation
has been performed becomes the root node of the tree.
Rotations
There are six types of rotations used for splaying:
The following are the factors used for selecting a type of rotation:
Scenario 1: If the node is the right of the parent and the parent is also right
of its parent, then zig zig right right rotation is performed.
Scenario 2: If the node is left of a parent, but the parent is right of its
parent, then zig zag right left rotation is performed.
Scenario 3: If the node is right of the parent and the parent is right of its
parent, then zig zig left left rotation is performed.
Scenario 4: If the node is right of a parent, but the parent is left of its
parent, then zig zag right-left rotation is performed.
To rearrange the tree, we need to perform some rotations. The following are
the types of rotations in the splay tree:
o Zig rotations
The zig rotations are used when the item to be searched is either a root
node or the child of a root node (i.e., left or the right child).
The following are the cases that can exist in the splay tree while
searching:
Case 2: If the search item is a child of the root node, then the two scenarios
will be there:
1. If the child is a left child, the right rotation would be performed, known
as a zig right rotation.
2. If the child is a right child, the left rotation would be performed, known
as a zig left rotation.
Non Linear Data Structures - Trees 3.22
Let's look at the above two scenarios through an example.
Step 2: Once the element is found, we will perform splaying. The right
rotation is performed so that 7 becomes the root node of the tree, as shown
below:
Step 2: Once the element is found, we will perform splaying. The left
rotation is performed so that 20 element becomes the root node of the tree.
Non Linear Data Structures - Trees 3.22
Again, we will perform zig right rotation, node 7 will move downwards, and
node 1 will come upwards as shown below:
Non Linear Data Structures - Trees 3.22
As we observe in the above figure that node 1 has become the root node of
the tree; therefore, the searching is completed.
In order to search 20, we need to perform two left rotations. Following are
the steps required to search 20 node:
Step 2: The second step is to perform splaying. In this case, two left
rotations would be performed. In the first rotation, node 10 will move
downwards, and node 15 would move upwards as shown below:
In the second left rotation, node 15 will move downwards, and node 20
becomes the root node of the tree, as shown below:
Non Linear Data Structures - Trees 3.22
Till now, we have read that both parent and grandparent are either in RR or
LL relationship. Now, we will see the RL or LR relationship between the
parent and the grandparent.
Step 2: Since node 13 is at the left of 15 and node 15 is at the right of node
10, so RL relationship exists. First, we perform the right rotation on node 15,
and 15 will move downwards, and node 13 will come upwards, as shown
below:
Non Linear Data Structures - Trees 3.22
Still, node 13 is not the root node, and 13 is at the right of the root node, so
we will perform left rotation known as a zag rotation. The node 10 will move
downwards, and 13 becomes the root node as shown below:
As we can observe in the above tree that node 13 has become the root
node; therefore, the searching is completed. In this case, we have first
performed the zig rotation and then zag rotation; so, it is known as a zig zag
rotation.
Step 2: Since node 9 is at the right of node 7, and node 7 is at the left of
node 10, so LR relationship exists. First, we perform the left rotation on node
7. The node 7 will move downwards, and node 9 moves upwards as shown
below:
Non Linear Data Structures - Trees 3.22
Still the node 9 is not a root node, and 9 is at the left of the root node, so we
will perform the right rotation known as zig rotation. After performing the
right rotation, node 9 becomes the root node, as shown below:
As we can observe in the above tree that node 13 is a root node; therefore,
the searching is completed. In this case, we have first performed the zag
rotation (left rotation), and then zig rotation (right rotation) is performed, so
it is known as a zag zig rotation.
In the insertion operation, we first insert the element in the tree and then
perform the splaying operation on the inserted element.
Non Linear Data Structures - Trees 3.22
15, 10, 17, 7
Step 2: The next element is 10. As 10 is less than 15, so node 10 will be the
left child of node 15, as shown below:
Step 4: The next element is 7. As 7 is less than 17, 15, and 10, so node 7
will be left child of 10.
Still the node 7 is not a root node, it is a left child of the root node, i.e., 17.
So, we need to perform one more right rotation to make node 7 as a root
node as shown below:
Types of Deletions:
1. Bottom-up splaying
Non Linear Data Structures - Trees 3.23
2. Top-down splaying
Bottom-up splaying
In bottom-up splaying, first we delete the element from the tree and then we
perform the splaying on the deleted node.
The deletion is still not completed. We need to splay the parent of the
deleted node, i.e., 10. We have to perform Splay(10) on the tree. As we can
observe in the above tree that 10 is at the right of node 7, and node 7 is at
the left of node 13. So, first, we perform the left rotation on node 7 and then
we perform the right rotation on node 13, as shown below:
Still, node 10 is not a root node; node 10 is the left child of the root node. So,
we need to perform the right rotation on the root node, i.e., 14 to make node
10 a root node as shown below:
Non Linear Data Structures - Trees 3.23
o Now, we have to delete the 14 element from the tree, which is shown below:
As we know that we cannot simply delete the internal node. We will replace
the value of the node either using inorder predecessor or inorder
successor. Suppose we use inorder successor in which we replace the value
with the lowest value that exist in the right subtree. The lowest value in the
right subtree of node 14 is 15, so we replace the value 14 with 15. Since
node 14 becomes the leaf node, so we can simply delete it as shown below:
Still, the deletion is not completed. We need to perform one more operation,
i.e., splaying in which we need to make the parent of the deleted node as
the root node. Before deletion, the parent of node 14 was the root node, i.e.,
10, so we do need to perform any splaying in this case.
Top-down splaying
Non Linear Data Structures - Trees 3.23
In top-down splaying, we first perform the splaying on which the deletion is
to be performed and then delete the node from the tree. Once the element
is deleted, we will perform the join operation.
Step 1: In top-down splaying, first we perform splaying on the node 16. The
node 16 has both parent as well as grandparent. The node 16 is at the right
of its parent and the parent node is also at the right of its parent, so this is a
zag zag situation. In this case, first, we will perform the left rotation on node
13 and then 14 as shown below:
The node 16 is still not a root node, and it is a right child of the root node, so
we need to perform left rotation on the node 12 to make node 16 as a root
node.
Non Linear Data Structures - Trees 3.23
Once the node 16 becomes a root node, we will delete the node 16 and we
will get two different trees, i.e., left subtree and right subtree as shown
below:
As we know that the values of the left subtree are always lesser than the
values of the right subtree. The root of the left subtree is 12 and the root of
the right subtree is 17. The first step is to find the maximum element in the
left subtree. In the left subtree, the maximum element is 15, and then we
need to perform splaying operation on 15.
As we can observe in the above tree that the element 15 is having a parent
as well as a grandparent. A node is right of its parent, and the parent node is
also right of its parent, so we need to perform two left rotations to make
node 15 a root node as shown below:
Non Linear Data Structures - Trees 3.23
After performing two rotations on the tree, node 15 becomes the root node.
As we can see, the right child of the 15 is NULL, so we attach node 17 at the
right part of the 15 as shown below, and this operation is known as
a join operation.
Non Linear Data Structures - Trees 3.23
Non Linear Data Structures - Trees 3.23
Compare General Tree and binary tree?
10
5 15
2 12
14
PART - B
1
0
1
4
1
1
6 2
2
1
8 3
1 3 5
7 9
6. Write the function to perform AVL single rotation and double rotation.
7. Explain about the RED-BLACK trees with its properties?
8. Construct a RED-BLACK tree by inserting 10,5,2,6,3,4,17,16 delete5,2.
9. Construct splay tree for the
following values: 1, 2, 3, 4, 5, 6, 7, 8
UNIT-IV GRAPHS
Graphs Terminology
A graph is a pictorial representation of a set of objects where some pairs of objects are connected by links. The
interconnected objects are represented by points termed as vertices, and the links that connect the vertices are
called edges.
Formally, a graph is a pair of sets (V, E), where V is the set of vertices and E is the set of edges, connecting the pairs
of vertices. Take a look at the following graph −
Weighted: In a weighted graph, each edge is assigned a weight or cost. Consider a graph of 4
nodes as in the diagram below. As you can see each edge has a weight/cost assigned to it. If you
want to go from vertex 1 to vertex 3, you can take one of the following 3 paths:
o 1 -> 2 -> 3
o 1 -> 3
o 1 -> 4 -> 3
Therefore the total cost of each path will be as follows: - The total cost of 1 -> 2 -> 3 will
be (1 + 2) i.e. 3 units - The total cost of 1 -> 3 will be 1 unit - The total cost of 1 -> 4 -> 3
will be (3 + 2) i.e. 5 units
Cyclic: A graph is cyclic if the graph comprises a path that starts from a vertex and ends at
the same vertex. That path is called a cycle. An acyclic graph is a graph that has no cycle.
A tree is an undirected graph in which any two vertices are connected by only one path. A
tree is an acyclic graph and has N - 1 edges
Nonwhere N Structures
Linear Data is the number
- Trees of vertices.
3.24
Each node in
a graph may have one or multiple parent nodes. However, in a tree, each node (except the
root node) comprises exactly one parent node.
A tree cannot contain any cycles or self loops, however, the same does not apply to graphs.
Two vertices are adjacent if they are endpoints of the same edge.
Outgoing edges of a vertex are directed edges that the vertex is the origin.
Incoming edges of a vertex are directed edges that the vertex is the destination.
Parallel edges or multiple edges are edges of the same type and end-vertices
Self-loop is an edge with the end vertices the same vertex
Simple graphs have no parallel edges or self-loops Non Linear Data Structures - Trees 3.24
Properties Non Linear Data Structures - Trees 3.24
If graph, G, has m edges then Σv∈G deg(v) = 2m
More Terminology
Path is a sequence of alternating vertices and edges such that each successive vertex is connected by the edge.
Frequently only the vertices are listed especially if there are no parallel edges.
Cycle is a path that starts and end at the same vertex.
Simple path is a path with distinct vertices. Directed path is a path of only directed edges Directed cycle is
a cycle of only directed edges. Sub-graph is a subset of vertices and edges.
Connected graph has all pairs of vertices connected by at least one path.
Connected component is the maximal connected sub-graph of a unconnected graph. Forest is a graph
without cycles.
Tree is a connected forest (previous type of trees are called rooted trees, these are free trees)
Spanning tree is a spanning subgraph that is also a tree.
More Properties
If G is an undirected graph with n vertices and m edges:
If G is connected then m ≥ n - 1
If G is a tree then m = n - 1
If G is a forest then m ≤ n – 1
Graph operations:
For example, suppose there is a new city we want to add to our map of train
routes. AddVertex(graph, G) would give:
• AddEdge:
For example, adding the city was not enough, we also need to say how the rail lines
connect it to other cities. Thus, we might do an AddEdge(graph, C, G), giving:
• IsReachable:
Reports whether we can get there from here.
For example, we might want to know whether we can get to city E from city A:
IsReachable(graph, E, A) would report a true value.
Again, we might want to know whether we can get to city D from city E:
IsReachable(graph, D, E) would report a false value.
The basic operations provided by a graph data structure G usually include:1]
Non Linear Data Structures - Trees 3.24
adjacent(G,x, y): tests whether there is an edge from the vertex x to the vertex y;
neighbors(G, x): lists all vertices y such that there is an edge from the vertex x to the vertex y;
Graph representation
You can represent a graph in many ways. The two most common ways of representing a graph is as follows:
Adjacency matrix
An adjacency matrix is a VxV binary matrix A. Element Ai,j is 1 if there is an edge from vertex i to vertex j else Ai,jis
0.
Note: A binary matrix is a matrix in which the cells can have only one of two possible values - either a 0 or 1.
The adjacency matrix can also be modified for the weighted graph in which instead of storing 0 or 1 in Ai,j, the
weight or cost of the edge will be stored.
In an undirected graph, if Ai,j = 1, then Aj,i = 1. In a directed graph, if Ai,j = 1, then Aj,i may or may not be 1.
Adjacency matrix provides constant time access (O(1) ) to determine if there is an edge between two
nodes. Space complexity of the adjacency matrix is O(V2).
The adjacency matrix of the following graph is:
i/j : 1 2 3 4
1:0101
2:1010
3:0101
4:1010
Non Linear Data Structures - Trees 3.24
The adjacency matrix of the following graph is:
i/j: 1 2 3 4
1:0100
2:0001
3:1001
4:0100
Non Linear Data Structures - Trees 3.24
Adjacency list
The other way to represent a graph is by using an adjacency list. An adjacency list is an array A of separate lists.
Each element of the array Ai is a list, which contains all the vertices that are adjacent to vertex i.
For a weighted graph, the weight or cost of the edge is stored along with the vertex in the list using pairs. In an
undirected graph, if vertex j is in list Ai then vertex i will be in list Aj.
The space complexity of adjacency list is O(V + E) because in an adjacency list information is stored only for those
edges that actually exist in the graph. In a lot of cases, where a matrix is sparse using an adjacency matrix may not
be very useful. This is because using an adjacency matrix will take up a lot of space where most of the elements
will be 0, anyway. In such cases, using an adjacency list is better.
Note: A sparse matrix is a matrix in which most of the elements are zero, whereas a dense matrix is a matrix in
which most of the elements are non-zero.
Consider the same undirected graph from an adjacency matrix. The adjacency list of the graph is as follows:
A1 → 2 → 4
A2 → 1 → 3
A3 → 2 → 4
A4 → 1 → 3
Non Linear Data Structures - Trees 3.24
Consider the same directed graph from an adjacency matrix. The adjacency list of the graph is as follows:
A1 → 2
A2 → 4
A3 → 1 → 4
A4 → 2
Graph Traversal:
Graph traversal is a technique used for a searching vertex in a graph. The graph traversal is also
used to decide the order of vertices is visited in the search process. A graph traversal finds the
edges to be used in the search process without creating loops. That means using graph traversal
we visit all the vertices of the graph without getting into looping path.
There are two graph traversal techniques and they are as follows...
Depth First Search (DFS) algorithm traverses a graph in a depthward motion and uses a stack to remember to get the
next vertex to start a search, when a dead end occurs in any iteration.
Non Linear Data Structures - Trees 3.24
As in the example given above, DFS algorithm traverses from S to A to D to G to E to B first, then to F and lastly to C.
It employs the following rules.
Rule 1 − Visit the adjacent unvisited vertex. Mark it as visited. Display it. Push it in a stack.
Rule 2 − If no adjacent vertex is found, pop up a vertex from the stack. (It will pop up all the vertices
from the stack, which do not have adjacent vertices.)
Rule 3 − Repeat Rule 1 and Rule 2 until the stack is empty.
4
Visit D and mark it as visited and
put onto the stack. Here, we have
B and C nodes, which are
adjacent to D and both are
unvisited. However, we shall
again choose in an alphabetical
order.
As C does not have any unvisited adjacent node so we keep popping the stack until we find a node that has an
unvisited adjacent node. In this case, there's none and we keep popping until the stack is empty
Breadth First Search
Non Linear Data Structures - Trees 3.25
Breadth First Search (BFS) algorithm traverses a graph in a breadthward motion and uses a queue to remember to
get the next vertex to start a search, when a dead end occurs in any iteration.
As in the example given above, BFS algorithm traverses from A to B to E to F first then to C and G lastly to D. It
employs the following rules.
Rule 1 − Visit the adjacent unvisited vertex. Mark it as visited. Display it. Insert it in a queue.
Rule 2 − If no adjacent vertex is found, remove the first vertex from the queue.
Rule 3 − Repeat Rule 1 and Rule 2 until the queue is empty.
We start from
visiting S(starting node), and
mark it as visited.
3
We then see an unvisited adjacent
node from S. In this example, we
have three nodes but
alphabetically we choose A,
mark it as visited and enqueue it.
From A we have D as
unvisited adjacent node. We mark
it as visited and enqueue it.
At this stage, we are left with no unmarked (unvisited) nodes. But as per the algorithm we keep on dequeuing in
order to get all unvisited nodes. When the queue gets emptied, the program is over.
SORTING
Non Linear Data Structures - Trees 3.25
Sorting refers to the operation or technique of arranging and rearranging sets of data in some
specific order. A collection of records called a list where every record has one or more fields. The
fields which contain a unique value for each record is termed as the key field. For example, a
phone number directory can be thought of as a list where each record has three fields - 'name' of
the person, 'address' of that person, and their 'phone numbers'. Being unique phone number can
work as a key to locate any record in the list.
The techniques of sorting can be divided into two categories. These are:
Internal Sorting
External Sorting
Internal Sorting: If all the data that is to be sorted can be adjusted at a time in the main memory, the internal
sorting method is being performed.
External Sorting: When the data that is to be sorted cannot be accommodated in the memory at the same time
and some has to be kept in auxiliary memory such as hard disk, floppy disk, magnetic tapes etc, then external
sorting methods are performed.
Bubble Sort
We take an unsorted array for our example. Bubble sort takes Ο(n2) time so we're keeping it short and
precise.
Bubble sort starts with very first two elements, comparing them to check which one is greater.
In this case, value 33 is greater than 14, so it is already in sorted locations. Next, we compare 33 with 27.
Non Linear Data Structures - Trees 3.25
We find that 27 is smaller than 33 and these two values must be swapped.
Next we compare 33 and 35. We find that both are in already sorted positions.
We know then that 10 is smaller 35. Hence they are not sorted.
We swap these values. We find that we have reached the end of the array. After one iteration, the array
should look like this −
To be precise, we are now showing how an array should look like after each iteration. After the second
iteration, it should look like this −
Non Linear Data Structures - Trees 3.25
Notice that after each iteration, at least one value moves at the end.
And when there's no swap required, bubble sorts learns that an array is completely sorted.
Algorithm
We assume list is an array of n elements. We further assume that swapfunction swaps the values of
the given array elements.
begin BubbleSort(list)
return list
Pseudocode
We observe in algorithm that Bubble Sort compares each pair of array element unless the whole array
is completely sorted in an ascending order. This may cause a few complexity issues like what if the
array needs no more swapping as all the elements are already ascending.
To ease-out the issue, we use one flag variable swapped which will help us see if any swap has
Non Linear Data Structures - Trees 3.25
happened or not. If no swap has occurred, i.e. the array requires no more processing to be sorted, it
will come out of the loop.
Pseudocode of BubbleSort algorithm can be written as follows −
list.count;
end for
end for
Insertion Sort
Non Linear Data Structures - Trees 3.26
We take an unsorted array for our example.
It finds that both 14 and 33 are already in ascending order. For now, 14 is in sorted sub-list.
It swaps 33 with 27. It also checks with all the elements of sorted sub-list. Here we see that the sorted
sub-list has only one element 14, and 27 is greater than 14. Hence, the sorted sub-list remains sorted
after swapping.
By now we have 14 and 27 in the sorted sub-list. Next, it compares 33 with 10.
We swap them again. By the end of third iteration, we have a sorted sub-list of 4 items.
This process goes on until all the unsorted values are covered in a sorted sub-list. Now we shall see
some programming aspects of insertion sort.
Algorithm
Now we have a bigger picture of how this sorting technique works, so we can derive simple steps by
which we can achieve insertion sort.
Step 1 − If it is the first element, it is already sorted. return 1;
Pseudocode
procedure insertionSort( A : array of items ) int Non Linear Data Structures - Trees 3.26
holePosition
int valueToInsert
for i = 1 to length(A) inclusive do:
valueToInsert = A[i] holePosition = i
For the first position in the sorted list, the whole list is scanned sequentially. The first position where 14
is stored presently, we search the whole list and find that 10 is the lowest value.
So we replace 14 with 10. After one iteration 10, which happens to be the minimum value in the list,
appears in the first position of the sorted list.
For the second position, where 33 is residing, we start scanning the rest of the list in a linear manner.
We find that 14 is the second lowest value in the list and it should appear at the second place. We
swap these values.
After two iterations, two least values are positioned at the beginning in a sorted manner.
The same process is applied to the rest of the items in the array. Following is a pictorial
depiction of the entire sorting process −
Non Linear Data Structures - Trees 3.26
for i = 1 to n - 1
/* set current element as minimum*/ min = i
i+1 to n
if list[j] < list[min] then
min = j;
end if
end for
External sorting is a term for a class of sorting algorithms that can handle massive amounts of
data. External sorting is required when the data being sorted do not fit into the main memory of a
computing device (usually RAM) and instead they must reside in the slower external memory
(usually a hard drive). External sorting typically uses a hybrid sort-merge strategy. In the sorting
phase, chunks of data small enough to fit in main memory are read, sorted, and written out to a
temporary file. In the merge phase, the sorted sub-files are combined into a single larger file.
One example of external sorting is the external merge sort algorithm, which sorts chunks that each
fit in RAM, then merges the sorted chunks together. We first divide the file into runs such that
the size of a run is small enough to fit into main memory. Then sort each run in main memory
using merge sort sorting algorithm. Finally merge the resulting runs together into successively
bigger runs, until the file is sorted.
One example of external sorting is the external merge sort algorithm, which is a K-way merge
algorithm. It sorts chunks that each fit in RAM, then merges the sorted chunks together.
The algorithm first sorts M items at a time and puts the sorted lists back into external memory For
example, for sorting 900 megabytes of data using only 100 megabytes of RAM:
1. Read 100 MB of the data in main memory and sort by some conventional method,
like quicksort.
2. Write the sorted data to disk.
3. Repeat steps 1 and 2 until all of the data is in sorted 100 MB chunks (there are 900MB /
100MB = 9 chunks), which now need to be merged into one single output file.
4. Read the first 10 MB (= 100MB / (9 chunks + 1)) of each sorted chunk into input buffers in
main memory and allocate the remaining 10 MB for an output buffer. (In practice, it might
provide better performance to make the output buffer larger and the input buffers slightly
smaller.)
5. Perform a 9-way merge and store the result in the output buffer. Whenever the output
buffer fills, write it to the final sorted file and empty it. Whenever any of the 9 input
buffers empties, fill it with the next 10 MB of its associated 100 MB sorted chunk until no
more data from the chunk is available. This is the key step that makes external merge sort
work externally -- because the merge algorithm only makes one pass sequentially through
each of the chunks, each chunk does not have to be loaded completely; rather, sequential
parts of the chunk can be loaded as needed.
Historically, instead of a sort, sometimes a replacement-selection algorithm was used to perform
Non Linear Data Structures - Trees 3.26
the initial distribution, to produce on average half as many output chunks of double the length.
Merge Sort
We know that merge sort first divides the whole array iteratively into equal halves unless the atomic
values are achieved. We see here that an array of 8 items is divided into two arrays of size 4.
This does not change the sequence of appearance of items in the original. Now we divide these two
arrays into halves.
We further divide these arrays and we achieve atomic value which can no more be divided.
Now, we combine them in exactly the same manner as they were broken down. Please note the color
codes given to these lists.
We first compare the element for each list and then combine them into another list in a sorted
manner. We see that 14 and 33 are in sorted positions. We compare 27 and 10 and in the target list of
2 values we put 10 first, followed by 27. We change the order of 19 and 35 whereas 42 and 44 are
placed sequentially.
Non Linear Data Structures - Trees 3.27
In the next iteration of the combining phase, we compare lists of two data values, and merge them into
a list of found data values placing all in a sorted order.
After the final merging, the list should look like this −
Merge sort works with recursion and we shall see our implementation in the same way.
Heap Sort
Heap sort is a comparison based sorting technique based on Binary Heap data structure. It is similar to
selection sort where we first find the maximum element and place the maximum element at the end.
We repeat the same process for remaining element.
Let us first define a Complete Binary Tree. A complete binary tree is a binary tree in which every level,
except possibly the last, is completely filled, and all nodes are as far left as possible
A Binary Heap is a Complete Binary Tree where items are stored in a special order such that value in a
parent node is greater(or smaller) than the values in its two children nodes. The former is called as max
heap and the latter is called min heap. The heap can be represented by binary tree or array.
Since a Binary Heap is a Complete Binary Tree, it can be easily represented as array and array based
representation is space efficient. If the parent node is stored at index I, the left child can be calculated
by 2 * I + 1 and right child by 2 * I + 2 (assuming the indexing starts at 0).
Heapify procedure can be applied to a node only if its children nodes are heapified. So the heapification
must be performed in the bottom up order.
Lets understand with the help of an example:
Radix Sort
The lower bound for Comparison based sorting algorithm (Merge Sort, Heap Sort, Quick-Sort .. etc) is
Ω(nLogn), i.e., they cannot do better than nLogn.
Counting sort is a linear time sorting algorithm that sort in O(n+k) time when elements are in range
from 1 to k.
We can’t use counting sort because counting sort will take O(n2) which is worse than comparison based
sorting algorithms. Can we sort such an array in linear time? Radix Sort is the answer. The idea of
Radix Sort is to do digit by digit sort starting from least significant digit to most significant digit. Radix
sort uses counting sort as a subroutine to sort.
Non Linear Data Structures - Trees 3.27
In first raw we check whether first letter of the pattern is matched with
the first letter of the text. It is mismatched, because "S" is the first letter of
pattern and "T" is the first letter of text. Then we move the pattern by one
position. Shown in second raw.
Then check first letter of the pattern with the second letter of text. It is
also mismatched. Likewise we continue the checking and moving process.
In fourth raw we can see first letter of the pattern matched with text. Then
we do not do any moving but we increase testing letter of the pattern. We
only move the position of pattern by one when we find mismatches. Also in
last raw, we can see all the letters of the pattern matched with the some
letters of the text continuously.
From the above example, |text|= 24. but we do comparisons only for first 16
letters. Then we can get |text|= 16 and |pattern|=6.
In worst case we should have to compare these 6 letters of the
pattern with 6 letters of the text in each position. Also we can see there are
11 positions for first 16 letters of text. that is (|text|-|pattern|+1) = 11.
Positions we can find using number of raw's in above example.
Therefore, all the comparisons for above example is {|pattern|*[|text|-|
pattern|+1]}. This means, to match our pattern with first 16 letters of text
we should have to make 6*11 comparisons. But if |text|<<<|pattern|, we
simply get all the comparison by |text|*|pattern|. Therefore running time
should be O(|pattern|*|text|).
Advantages
Non Linear Data Structures - Trees 3.27
1. Very simple technique and also that does not require any
preprocessing. Therefore total running time is the same as its matching
time.
Disadvantages
1. Very inefficient method. Because this method takes only one position
movement in each time.
for i ← 0 to n-m do
j←0
j++
if j = m then return i
return -1
#include<stdio.h>
#include<string.h>
char t[100],p[50];
void main()
{
int pos;
clrscr();
printf("Enter the Source String ");
scanf("%s",t);
printf("Enter the pattern ");
scanf("%s",p);
pos=brute_force();
if(pos==-1)
printf("%s pattern not found in text",p);
else
printf("%s pattern found at index %d",p,pos);
getch();
}
int brute_force() Non Linear Data Structures - Trees 3.27
{
int n,j,m,i;
n=strlen(t);
m=strlen(p);
for(i=0;i<n;i++)
{
j=0;
while(j<m && t[i+j]==p[j])
{
j++;
if(j==m)
return i+1; //pattern found
}
}
return -1; //pattern not found
}
case 1
int badchar[NO_OF_CHARS];
else
/* Shift the pattern so that the bad character
in text aligns with the last occurrence of
it in pattern. The max function is used to
make sure that we get a positive shift.
We may get a negative shift if the last
occurrence of bad character in pattern
is on the right side of the current
character. */
s += max(1, j - badchar[txt[s+j]]);
}
}
Knuth-Morris-Pratt algorithm
KMP Algorithm is one of the most popular patterns matching algorithms. KMP stands
for Knuth Morris Pratt. KMP algorithm was invented by Donald Knuthand Vaughan
Pratt together and independently by James H Morris in the year 1970. In the year
1977, all the three jointly published KMP Algorithm.
KMP algorithm was the first linear time complexity algorithm for string matching.
KMP algorithm is one of the string matching algorithms used to find a Pattern in a Text.
KMP algorithm is used to find a "Pattern" in a "Text". This algorithm campares character
by character from left to right. But whenever a mismatch occurs, it uses a preprocessed
table called "Prefix Table" to skip characters comparison while matching. Some times
prefix table is also known as LPS Table. Here LPS stands for "Longest proper Prefix
which is also Suffix".
Non=Linear
Step 4 - If both are matched then set LPS[j] i+1 Data
and Structures
increment - Trees 3.28
both i & j values
by one. Goto to Step 3.
Step 5 - If both are not matched then check the value of variable 'i'. If it is '0' then
set LPS[j] = 0 and increment 'j' value by one, if it is not '0' then set i = LPS[i-1].
Goto Step 3.
Step 6- Repeat above steps until all the values of LPS[] are filled.
if (j == M)
{
printf("Found pattern at index %d \n", i-j);
j = lps[j-1];
}
else if(pat[j] != txt[i])
{
if(j != 0)
j = lps[j-1];
else
i = i+1;
}
}
}
int main()
{
printf("\n ENTER THE TEXT : "); Non Linear Data Structures - Trees 3.28
gets(txt);
printf("\n ENTER THE PATTERN : ");
gets(pat);
KMPSearch();
return 0;
}
output:-
Tries
All the search trees are used to store the collection of numerical values but
they are not suitable for storing the collection of words or strings. Trie is a
data structure which is used to store the collection of strings and makes
searching of a pattern in words more easy. The term trie came from the
word retrieval. Trie data structure makes retrieval of a string from the
collection of strings more easily. Trie is also called as Prefix Tree and
some times Digital Tree. A trie is defined as follows...
Trie is a tree like data structure used to store collection of strings.
A trie can also be defined as follows...
Trie is an efficient information storage and retrieval data structure.
The trie data structure provides fast pattern matching for string data values.
Using trie, we bring the search complexity of a string to the optimal limit. A
trie searches a string in O(m) time complexity, where m is the length of the
string.
In trie, every node except the root stores a character value. Every node in
trie can have one or a number of children. All the children of a node are
alphabetically ordered. If any two strings have a common prefix then they
will have the same ancestors.
Example
Non Linear Data Structures - Trees 3.28
Th
e root node has no label !!!
Each internal
node of T can have ≤ |Σ| # of keys
The keys are stored
in alphabetical order inside an internal node
The trie T has s ext
ernal nodes
string in S
(And index (= location)
Non Linear Data Structures - Trees 3.29
information are stored for
these strings)
The path from
the root node to an external node yields exactly one
string in S
o Example:
S = { bear, bell, bid, bull, buy, sell,
stock, stop } (s = 8)
Trie:
Note:
There
are 8 leaf nodes !!
se a binary tree
This
implementation is called a bitwise trie
Implementing a trie using an array of references
o Array implementation:
Node structure:
Non Linear Data Structures - Trees 3.29
o Example:
In
other words, the implementation stores
a sequences of bits
Non Linear Data Structures - Trees 3.29
Example:
This is
because a path from the root
node to one external node corresponds to 1
key
The height of the trie T on
the set S = the length of the longest string ∈ S
This
Non Linear Data Structures - Trees
is because a path from the root 3.29
node to one external node corresponds
to 1 key
strings ∈ S
T on the set S = O(n), where n = # characters in the
In the worst
case, every character in the keys are different
E.g.:
p = root;
if ( nextChar ∈ p.char[] )
{
nextLink = link associated with the char
entry;
p = nextLink; // Traverse
}
else
{
p = new TrieNode(); // Create new
node
/*
------------------------------------------------------
When the while loop ends, we have found or
created
the external node for the key "k"
------------------------------------------------------
*/
insert v into node p;
}
Look
up in a map is O(lg(n)) where n = #
entries !!!
So a trie has performance levels that is
similar to a hash table !!!
Unlike a hash table, a trie can
provide an alphabetical ordering
Non Linear of the -entries
Data Structures Trees by key 3.29
o Solution:
Add a special termination symbol
◊ to the alphabet Σ
The t
ermination symbol ◊ has
the lower value in the alphabet
I.e.,: termination symbol
◊ preceeds every character in
the alphabet Σ !!
Non Linear Data Structures - Trees 3.29
Example:
o Note:
Compressed tries
o A compressed trie is a trie with one additional rule:
o Redundant node:
An internal node v is redundant if
Node
v is not the root node, and
Node
v has 1 child node
Example:
A chain of edges:
Node
s v1, v2, ..., vk−1 are redundant
Node
s v0 and vk are not redundant
Example:
Compression algorithm:
Replace:
a red
undant chain or edges (v0,v1), (v1,v2), ...,
(vk−1,vk)
by one edge:
(v0 ,v
k )
Replace:
the la
bel vk
by the label: Non Linear Data Structures - Trees 3.30
v1 v2 .
.. vk
Example:
Before compression:
After compression:
Result:
Non Linear Data Structures - Trees 3.30
Another example:
Result:
Non Linear Data Structures - Trees 3.30
A compressed trie
T storing s strings (keys) has: s external nodes
A compressed trie
T storing s strings (keys) has: O(s) total number of
nodes
Because
a compressed trie is in the worst case comparable to
a binary tree
In a binary tree:
Hence:
# internal nodes in a compress
trie < s
Non Linear Data Structures - Trees 3.30
Suffix tries
Suffix tree is a compressed trie of all the suffixes of a given string. Suffix
trees help in solving a lot of string related problems like pattern matching,
finding distinct substrings in a given string, finding longest palindrome etc.
Before going to suffix tree, let's first try to understand what a compressed
trie is.
Consider the following set of strings:
{"banana","nabd","bcdef","bcfeg","aaaaaa","aaabaa" }
A standard trie for the above set of strings will look like:
And a compressed trie for the given set of strings will look like:
Non Linear Data Structures - Trees 3.30
Before going to construction of suffix trees, there is one more thing that
should be understood, Implicit Suffix Tree. In Implicit suffix trees, there are
atmost N leaves, while in normal one there should be exactly N leaves. The
reason for atmost N leaves is one suffix being prefix of another suffix.
Following example will make it clear. Consider the string "banana"
Implicit Suffix Tree for the above string is shown in image below:
A Suffix Tree for a given text is a compressed trie for all suffixes of the
given text. We have discussed Standard Trie. Let us
understand Compressed Trie with the following array of words.
{bear, bell, bid, bull, buy, sell, stock, stop}
Following is standard trie for the above input set of words.
Following is the compressed trie. Compress Trie is obtained from standard
trie by joining chains of single nodes. The Non
nodes
Linearof a Structures
Data compressed- Trees trie can 3.30
be stored by storing index ranges at the nodes.
Please note that above steps are just to manually create a Suffix Tree. We
will be discussing actual algorithm and implementation in a separate post.
How to search a pattern in the built suffix tree?
We have discussed above how to build a Suffix Tree which is needed as a
preprocessing step in pattern searching. Following are abstract steps to
search a pattern in the built Suffix Tree.
1) Starting from the first character of the pattern and root of Suffix Tree, do
following for every character. Non Linear Data Structures - Trees 3.31
…..a) For the current character of pattern, if there is an edge from the
current node of suffix tree, follow the edge.
…..b) If there is no edge, print “pattern doesn’t exist in text” and return.
2) If all characters of pattern have been processed, i.e., there is a path from
root for characters of the given pattern, then print “Pattern found”.
Let us consider the example pattern as “nan” to see the searching process.
Following diagram shows the path followed for searching “nan” or “nana”.
Suffix tree can be used for a wide range of problems. Following are some
famous problems where Suffix Trees provide optimal time complexity
solution.
1) Pattern Searching
2) Finding the longest repeated substring
3) Finding the longest common substring
4) Finding the longest palindrome in a string
Non Linear Data Structures - Trees 3.31
OR
9. What is collision? Explain different collision resolution techniques with examples. [10]
{15, 20, 24, 10, 13, 7, 30, 36, 25, 42, 29}
OR
11. Write short notes on:
1 .a Explain about
Trewith an
0) AVL e example.
b Give the typesTrie [5+
) examples forof s. 5]
O
R
1 .a Write Spl Tree with an.
1) about ay example
b Discuss about Binary Search [5+
) Tree operations. 5]
--ooO--
- oo -
JN
20 04 TU
17 H
A U
Non Linear Data Structures - Trees 3.31
Non Linear Data Structures - Trees 3.31
Non Linear Data Structures - Trees 3.31
Non Linear Data Structures - Trees 3.31
319
Non Linear Data Structures - Trees 3.32
www.ManaResults.co.in
320
Non Linear Data Structures - Trees 3.32
321