SlideShare a Scribd company logo
C And Data Structures
UNIT V
Stack: LIFO concept, Stack operations, Array implementation of stack – Queue:
FIFO concept, Queue operations, Array implementation of queue – Singly
Linked List: concepts, operations – Doubly Linked List: concepts, operations –
Trees: General trees, Binary trees.
Data Structures
Data Structure: Logical or mathematical model of organizing data is
called data structure.
i.e. Data Structure = organized data + Operations.
A data structure is a way to organize and manage data.
 It not only stores data but also provides operations to manipulate
that data.
For example, an array is a type of data structure that stores data
in sequential order.
You can perform various operations on an array, such as finding its
size, sorting the data, retrieving existing data, and modifying data within
it.
Data Structures
Data Structure: Logical or mathematical model of organizing data is called
data structure.
i.e. Data Structure = organized data + Operations.
Classification of Data structures
Linear data structures: A data structure whose elements form a sequence, and every element in the structure has a
unique predecessor and unique successor. Eg. Arrays,linked lists, stacks and queues.
Non-linear data structures: A data structure whose elements do not form a sequence, and there is no unique
predecessor or unique successor. Eg. Trees and graphs.
Array
Introduction
Data structures are classified as either linear or nonlinear.
A data structure is said to be linear if its elements form a sequence or a linear list.
There are two basic ways of representing such linear structures in memory.
One way is to have the linear relationship between the elements represented by means of sequential memory
locations. These linear structures are called arrays.
The other way is to have the linear relationship between the elements represented by means of pointers or links.
These linear structures are called linked lists.
Nonlinear structures are trees and graphs.
Definition:
A array is a list of finite number n of homogeneous data elements such that :
a) The elements of the array are referenced respectively by an index set consisting of n consecutive numbers.
b) The elements of the array are stored respectively in successive memory locations.
Stacks
 A stack is an ordered collection of homogeneous data element where the
insertion and deletion operations take place at one end called the top of the
stack. The other end is called the bottom.
 This means, in particular, that elements are removed from a stack in the
reverse order of that in which they were inserted into the stack.
 An item may be added or removed only from the top of a stack. This means
that the last item to be added to a stack is the first item to be removed.
Accordingly, stacks are also called Last-In First-Out (LIFO) lists.
Applications
• Page-visited history in a Web browser
• Undo sequence in a text editor
• Chain of method calls in a programming language
 Two basic operations associated with stacks are
1.“Push” is the term used to insert an element into a stack.
2.“Pop” is the term used to delete an element from a stack.
5
AAA
BBB
CCC
DDD
EEE
FFF
.
.
.
.
Stacks (Example): Suppose the following 6 elements are pushed, in order, onto an empty
stack: AAA, BBB, CCC, DDD, EEE, FFF. The followin fig shows three ways of picturing such a
stack.
1
2
3
TOP 4
TOP 5
6
FFF 7
EEE 8
DDD 9
CCC .
BBB
.
AAA N-1
N
AAA BBB CCC DDD EEE FFF …
1 2 3 4 5 6 7 8 9 … N-1 N
TOP 6
Memory Representation of stack
A stack can be represented in memory either as an one-dimensional array or as a singly linked list.
Array representation Linked list representation
Array Representation of Stacks
For the array implementation:
Declare an array of fixed size (which determines the maximum size of the stack).
Keep a variable which always points to the “top” of the stack, which contains the array index of the “top” element.
STACK
XXX YYY ZZZ
1 2 3 4 5 6 7 8
TOP 3 MAXSTK
8
The stack is maintained by a linear array STACK: a pointer variable TOP, which contains the location of the top element
of the stack; and a variable MAXSTK which gives the maximum number of elements that can be held by the stack.
The above Figure pictures such an array representation of a stack. Since TOP = 3, the stack has three elements, XXX, YYY
and ZZZ; and since MAXSTK = 8, there is room for 5 more items in the stack.
STACK : A linear array
TOP : A pointer variable, Which contains the location of the top element of the stack.
MAXSTK : Gives the maximum number of elements that can be held by the stack.
TOP = 0 or NULL will indicate that the stack is empty
operations on Stack : PUSH and POP
// Here, STACK is an array to store data.
TOP represents in which location
the
data is to be inserted. MAXSTK is the
maximum size of the stack and
finally, ITEM is the new item to be
added
//This procedure pushes an ITEM onto a
stack
PUSH(STACK,TOP,MAXSTACK,ITEM)
1. [STACK already filled]
//Here, STACK is the array where
data are stored. TOP represents
from which location the data is
to be removed. Top element is
assigned to ITEM.
//This procedure deletes the top
element of STACK and assign it
to the variable ITEM
POP(STACK,TOP,ITEM)
If TOP=MAXSTACK, then
Print OVERFLOW and Return.
2. Set TOP=TOP+1
3. Set STACK[TOP]=ITEM.
4. Return.
1. If TOP=0, then print UNDERFLOW
and Return
2. Set ITEM=STACK[TOP].
3. Set TOP=TOP-1.
4. Return.
9
Pushing
0 1 2 3 4 5 6 7 8 9
STACK
0
17 23 97 44
top = 3 N = 4
 If ITEM=80 is to be inserted, then TOP=TOP+1=4
 STACK[TOP]:= STACK[4]:= ITEM=80
 N=5
 MAXSTACK=10
1 2 3 4 5 6 7 8 9
STACK
17 23 97 44 80
top = 4 N = 5
Poping
0 1 2 3 4 5 6 7 8 9
STACK
17 23 97 44 80
top = 4 N = 5
 If ITEM=80 is to be deleted, then ITEM:=STACK[TOP]= STACK[4]= 80
 TOP=TOP-1=3
 N=4
 MAXSTACK=10
0 1 2 3 4 5 6 7 8 9
STACK
17 23 97 44
top = 3 N = 4
QUEUES
 A Queue is a linear list of elements in which deletions can take place only at one end, called the front, and
insertions can take place only at the other end, called the rear.
 Queues are also called first-in first-out (FIFO) lists, since the first element in a queue will be the first element out of
the queue.
 In other words, the order in which elements enter a queue is the order in which they leave. This contrasts with
stacks, which are Last-in First-out (LIFO) lists.
Examples:
1. The automobiles waiting to pass through an intersection form a queue, in which the first car in line is the first car
through
2. The people waiting in line at a bank(or ticket counter) form a queue, where the first person in line is the first person
to be served.
3. An important example of a queue in computer science occurs in a timesharing system, in which programs with the
same priority form a queue while waiting to be executed.
4. Queue for printing purposes(Documents sent to a shared printer)
REPRESENTATION OF QUEUE
Like Stacks, Queues can also be represented in memory in two
ways. 1. Linear Array
2. One-way Linked List
variables:
increased by 1; this can be implemen ed by the assignment
REAR is increased by 1; this can be implemented by the
assignment
Array Representation of Queues
Queues can be easily represented using linear arrays.
Each of our queues will be maintained by a linear array QUEUE and two pointer
 FRONT, containing the location of the front element of the queue;
and REAR, containing the location of the rear element of the
queue. The condition FRONT = NULL will indicate that the queue is
empty.
when an element is deleted from the
t
queue(called dequeue), the value of FRONT
is
FRONT := FRONT + 1
Similarly, whenever an element is added to the queue (called enqueue), the value of
QUEUE[N].
 REAR := REAR + 1
This means that after N insertions, the rear element of the queue will occupy
CDS artificial intelligence and Machine.docx
condition of
underflow.
OPERATIONS ON QUEUE
The two basic operations that can be performed on a queue are:
Insert (enqueue) : to insert an element at rear of the queue
Delete(dequeue): to delete an element from front of the queue.
Before inserting a new element in the queue, it is necessary to test the condition of overflow.
Overflow occurs when the queue is full and there is no space for a new element.
Similarly, before removing the element from the queue, it is necessary to check the
Underflow occurs when the queue is empty.
Algorithms for Insert and Delete Operations in Linear Queue
For Insert (enqueue )Operation
Insert-Queue(Queue, Rear, Front, N, Item)
Here, Queue is an array where to store data. Rear represents the location in which the data element is to
be inserted and Front represents the location from which the data element is to be removed. Here N is
the maximum size of the Queue and Item is the new item to be added.
1. If Rear = N then Print: Overflow and Return. /*…Queue already filled..*/
2. Set Rear := Rear +1
3. Set Queue[Rear] := Item
4. Return.
For Delete(dequeue) Operation
Delete-Queue(Queue, Front, Rear, Item)
Here, Queue is an array where data are stored. Rear represents the location in which the data
element is to be inserted and Front represents the location from which the data element is to be
removed. Front element is assigned to Item.
1. If Front = N+1 then Print: Underflow and Return. /*…Queue Empty
2. Set Item := Queue[Front]
3. Set Front := Front + 1
4. Return.
Example: Consider the following queue (linear queue).
Rear = 4 and Front = 1 and N = 7
10 50 30 40
1 2 3 4 5 6 7
(1) Insert 20. Now Rear = 5 and Front = 1
10 50 30 40 20
1 2 3 4 5 6 7
(2) Delete Front Element. Now Rear = 5 and Front = 2
50 30 40 20
1 2 3 4 5 6 7
(3) Delete Front Element. Now Rear = 5 and Front = 3
30 40 20
1 2 3 4 5 6 7
(4) Insert 60. Now Rear = 6 and Front = 3
30 40 20 60
1 2 3 4 5 6 7
Linked Lists
• A linked list is a linear collection of data elements (linear data structure).
• Each element is represented by a node.
• Each node is divided into two parts.
1. Info or Data: The information stored in the node.
2. Link or next: A pointer that holds the address of the next node in the sequence.
1. Linked list has a list pointer variable, Head or Start, containing the address of the
1st
node in the list.
• If Start contains NULL value, it means the list is empty.
• The address field of last node in the list contains NULL representing invalid address.
START
3
1
2
3
4
5
6
7
8
INFO
A
M
G
N
O
LINK
5
2
7
4
0
START=3, INFO[3]=M
LINK[3]=2, INFO[2]=A
LINK[2]=5, INFO[5]=N
LINK[5]=4, INFO[4]=G
LINK[4]=7, INFO[7]=O
LINK[7]=0, NULL value, So the list has ended
Linked List as Students in a Classroom
• Each student represents a node in the linked list. Each student has some
information, like their roll number.
• The student with the first roll number (e.g., Student 1) is the starting
point, or the head of the linked list.
• Each student knows who comes next. For example, Student 1 knows
who has roll number 2, Student 2 knows who has roll number 3, and so
on. This is like each node pointing to the next node in the list.
• To find a specific student (e.g., Student 50), start with the first student
and follow the chain of connections until you reach Student 50.
• The last student in the sequence doesn't know of anyone after them,
indicating the end of the list.
• Example:
The following figure shows a linked list having 5 nodes.
Information Field
Link Field
23
Memory Representation of Linked List
• A linked list can be maintained in memory using two linear arrays.
1. Info
2. Link
• Info[K] represents the information part of a node in the list.
• Link[K] represents the nextpointer field of a node in the list.
• The beginning of the list is denoted by the variable Start.
Figure: Linked list & its memory representation
24
arrays have a fixed size that must be specified at the time of
creation.
arrays require shifting of elements, which can be time-
consuming.
that are actually being used, whereas arrays require contiguous memory a
location.
and graphs, whereas arrays are limited to a single
dimension.
elements to be stored in a contiguous block of
memory.
advantages of linked list over arrays
1.Dynamic Size
• Linked lists can grow or shrink dynamically as elements are added or removed, whereas
2.Efficient Insertion and Deletion
• Linked lists allow for efficient insertion and deletion of elements at any position, whereas
3.Good Memory Utilization
• Linked lists can make efficient use of memory, as memory is allocated only
l
for the
elements 4.Flexible Data Structure
• Linked lists can be used to implement various data structures such as stacks, queues, trees,
5.No Fixed Order
• Linked lists do not require elements to be stored in a specific order, whereas arrays require
Linked List Operations: Insertion and Deletion
Insertion:
A B C
tmp X
Item to be
inserted
curr
A B C
X
Single Linked List: Inserting a Node in a Linked List
Steps to insert node at any position of Singly Linked List
Step 1: Create a new node. newNode = createNode(data)
Step 2: Traverse to the n−1th Position and Connect the New Node (newNode.next =
current.next) where current node is the n-1th node.
Single Linked List: Insertion at any position
Step 3: Connect the n−1th Node to the New Node (current.next = newNode) where
current node is the n-1th node.
Linked List Operations: Insertion and Deletion
Deletion Item to be deleted
A B C
curr
tmp
A C
B
Singly Linked List: Deleting a node from the linked list
Steps to Delete the Nth Node from a Singly Linked List:
Step 1: Traverse to the nth node of the singly linked list and also keep reference of n-1th
node in some temp variable say prevNode.
Step 2: Reconnect n-1th node with the n+1th node i.e. prevNode.next = toDelete.next (This
sets the next pointer of the (n−1)th node (prevNode) to point to the (n+1)th node (toDelete.next)).
Single Linked List: Deletion at any Position
Step 3: Free the memory occupied by the nth node i.e. toDelete node.
Doubly Linked List
In a singly linked list, each node has just one pointer that points to the next
node. In contrast, a doubly linked list has two pointers in each node:
one that points to the next node and another that points to the previous node.
This setup allows you to move both forward and backward through the list, making it
a two-way chain.
Each node in a doubly linked list has three components:
Data (INFO): Stores the actual data or value of the node.
Forward Link (NEXT): A pointer to the next node (the address of the next node)
Backward Link (PREV): A pointer to the previous node (the address of the previous
node)
The purpose of a doubly linked list is to allow traversal in both directions while still using
non-contiguous memory.
Similar to a singly linked list, it requires a starting pointer that points to the first node.
The Prev pointer of the first node (A) and the Next pointer of the last node (D) both point to
NULL. The doubly linked list shown has 4 nodes with data points A, B, C, and D.
Each node contains two links: one pointing to the next node and one pointing to the previous node.
Each node in the list consists of two pointers and a data field:
a. Data: Holds the value of the data item within the node.
b. Prev: A pointer that references the previous node. For the first node, Prev points to NULL.
points to NULL.
c. Next: A pointer that links the current node to the next node in the list. The Next pointer of the last node
representation of a linked
list.
The Prev pointer of the f rst
node
node (D) both point to NULL.
indicates that the values do
not
The numbers 1000, 1056, 2004,
We traverse the list until
we
a node.
The image below illustrates the
memory
-1 denotes NULL.

(A) and the Next pointer of
i
the last
This memory representation
need to be stored contiguously.

etc., represent memory
addresses.

encounter -1 in the
Next pointer of
Basic Operations on a Doubly Linked List:
1. Insertion: This operation adds a new element at a specified position in the list.
2. Deletion: This operation removes a node from the list.
Inserting a New Node at the Nth Position in a Doubly Linked List
1. Create a new node: Ptr (or newNode) with the given data.
2. Traverse the list: Reach the (N-1)th node, where temp points to this node.
3. Link the new node to the next node: Ptr.next = temp.next;
4. Link the new node to the previous node: Ptr.prev = temp;
5. Connect temp to the new node: temp.next = Ptr;
6. Adjust the backward link of the following node:
If Ptr.next is not null, set its previous link to Ptr: Ptr.next.prev = Ptr;
Deleting a Node at the Nth Position in a Doubly Linked List
1. Traverse the List:
Reach the (N-1)th node, where temp points to this node.
2. Identify the Node to be Deleted:
nodeToDelete = temp.next; (the node to be deleted is the next node)
3. Link the Previous Node to the Next Node:
temp.next = nodeToDelete.next; (bypass the node to be deleted)
4. Adjust the Backward Link of the Following Node:
If nodeToDelete.next is not null, nodeToDelete.next.prev = temp; (update the previous
pointer) 5. Free the Node:
nodeToDelete = null; (release the memory occupied by the deleted node).
Trees
Trees are a fundamental data structure used in computer science to
represent hierarchical relationships (parent-child relationships) between
elements. It's similar to a real-life tree but inverted, with the root at the
top and branches that spread downwards.
Tree in real life and Computer Science
Trees
Tree Structure in Real Life
Tree Structure in computer science
General Trees
A tree is a finite collection of one or more nodes with the following properties:
i) There is a specially designated node called the root.
ii) The other nodes are divided into n≥0 disjoint sets, T1,…,Tn, where each set is itself a
tree.
T1, T2, …, Tn are called the sub-trees of the root.
A tree consists of a collection of elements or nodes, with each node linked to its
successors
Terminologies
1. Node: A node is a basic unit of a tree. Each node contains data and may link to
other nodes. For example, consider a tree diagram with 13 nodes, each represented
by a letter for simplicity.
2. Edges: The connections between nodes are called edges.
3. Root: The topmost node of a tree. It is the starting point and doesn't have any
parent. In the example, node A is the root.
4. Degree of a Node: The number of subtrees a node has. For instance, node A has
a degree of 3, node C has a degree of 1, and node F has a degree of 0.
5. Leaf (or Terminal) Nodes : A node with no children. It is the endpoint of a branch in
the tree. In the example, nodes {K, L, F, G, M, I, J} are leaf nodes.
6. Non-Terminal Nodes: Nodes that are not leaf nodes; they have at least one child.
7. Children and Parent: The roots of the subtrees of a node X are called its children, and
X is referred to as their parent. For example, the children of node D are H, I, and J, and
the parent of D is A.
8. Siblings: Children of the same parent. For instance, nodes H, I, and J are siblings.
9. Extended Family Terms: The terminology can extend to terms like
grandparents, where, for instance, the grandparent of node M is D.
10. Degree of a Tree: The maximum degree among all the nodes in the tree. In
the example, the tree's degree is 3.
11. Ancestors: All nodes along the path from the root to a specific node. For example,
the ancestors of node M are A, D, and H.
The level of a node in a tree is determined by
assigning the root to level one. If a node is at
level l, its children will be at level l+1
The height or depth of a tree is the maximum
level of any node in the tree.
A forest in a tree structure is a collection of
one or more disjoint trees. The concept of a
forest is closely related to a tree; removing the
root node of a tree transforms it into a forest.
For example, removing the root node A from a
tree would result in a forest of three separate
trees.
• These terms help describe the hierarchical relationships and structure within a tree,
making it easier to understand and work with these data structures.
each node can have at most two
children.
child.
m
atters.
node.
subtrees
Binary Trees
• A binary tree is a type of tree structure where Examples
• Each node can have a left child and a right
• The order of the subtrees in a binary tree
• A binary tree can be empty, meaning it can
•
regular tree, which must have at least one
Definition: A binary tree is a finite set of nodes
separate
.
binary trees called the left and right
o
This makes a binary tree different from
a
have zero
nodes.
either empty or consists of a root node and tw
whe
re each node has at most two children. It
is
NOTE:
• The maximum number of nodes on level i of a binary tree is 2i
, i>=1.
• The maximum number of nodes in a binary tree of depth k is 2k
-1, k>=1.
Full binary tree
A binary tree is a full binary tree, if it contains maximum possible number of nodes in all levels. A full binary tree of
depth k is a binary tree of depth k having 2k
-1 nodes, k>=0.
Complete binary tree
A binary tree is said to be a complete binary tree, if all its levels, except possibly the last level, have the
maximum number of possible nodes, and all the nodes at the last level appear as far left as possible.
Representation of binary trees
Binary trees can be represented by means of:
1] Array – or sequential representation
2] Linked List – or linked representation.
1.Array Representation
In an array representation of a binary tree, the nodes of the tree
are stored in an array in level-order .
This means the root is stored first, followed by the nodes on the
next level from left to right, and so on.
For a binary tree of height h the worst-case array size needed is 2h
−1
Example:
In an array representation of a binary tree with a base index of 1:
Root node: The root node is always at index 1.
Left child: For a node at index i, the left child is at index 2i.
Right child: For a node at index i, the right child is at index 2i + 1.
In this representation:
Odd indexes (like 1, 3, 5, ...) contain nodes that are either root node or right children.
Even indexes (like 2, 4, 6, ...) contain nodes that are left children.
Linked representation
Disadvantages of array representation
.
• This scheme of representation is quite wasteful of
space when binary tree is skewed or number of
elements are small as compared to its height.
• Array representation is useful only when the binary
tree is full or complete.
In a linked representation of a binary tree, each node is
represented by a structure or class containing the
following elements:
1. Data: The value stored in the node.
2. Left Child: A pointer to the left child node.
3. Right Child: A pointer to the right child node.
Examples
Advantages:
Flexible: Linked representations can handle dynamic insertion
and deletion of nodes.
Efficient: Linked representations can be more memory-efficient
than array-based representations, especially for sparse trees.
Disadvantages:
Complex: Linked representations require more complex code to
manage the pointers (or references) between nodes.
node in a tree exactly once in a specific
order.
Inorder, Preorder, and
Postorder.
Binary Tree Traversals
Binary tree traversal refers to the process of visiting each
There are three main traversal methods for binary trees:
1.Inorder Traversal (LNR)
1. Traverse the left subtree in Inorder.
(L) 2. Process the root node.(N)
3. Traverse the right subtree in Inorder.
(R) 2.Preorder Traversal (NLR )
1. Process the root node.(N)
2. Traverse the left subtree in Preorder.(L)
3. Traverse the right subtree in Preorder.
(R) 3.Postorder Traversal (LRN)
1. Traverse the left subtree in Postorder. (L)
2. Traverse the right subtree in Postorder.
(R) 3.Process the root node.(N)
Algorithms for binary Tree traversals
1.Inorder Traversal (LNR)
void inorder(Node root) {
if (root == null) return;
inorder(root.left); // Traverse left subtree
print(root.data ); // Visit root node
inorder(root.right); // Traverse right
subtree }
2. Preorder Traversal
(NLR) void preorder(Node
root) {
if (root == null) return;
3. Postorder Traversal
(LRN) void postorder(Node
root) {
if (root == null) return;
postorder(root.left); // Traverse left subtree
postorder(root.right); // Traverse right subtree
.print(root.data ); // Visit root node
}
print(root.data ); preorder(root.left); preorder(root.right);
}
// Visit root node
// Traverse left
subtree
// Traverse right
subtree
Illustrations for Traversals
• Assume: visiting a node
is printing its label
• Inorder: (LNR)
4 5 6 3 1 8 7 9 11 10 12
• Preorder: (NLR)
1 3 5 4 6 7 8 9 10 11 12
• Postorder: (LRN)
4 6 5 3 8 11 12 10 9 7 1
1
3 7
5 8 9
4
6
10
11 12
53

More Related Content

PPTX
Mca ii dfs u-3 linklist,stack,queue
PPTX
Bsc cs ii dfs u-2 linklist,stack,queue
PPTX
Bca ii dfs u-2 linklist,stack,queue
PPT
Data Structures by Maneesh Boddu
PPTX
chapter three ppt.pptx
PPTX
Stack and Queue
PDF
Stacks,queues,linked-list
PPTX
Stack.pptx
Mca ii dfs u-3 linklist,stack,queue
Bsc cs ii dfs u-2 linklist,stack,queue
Bca ii dfs u-2 linklist,stack,queue
Data Structures by Maneesh Boddu
chapter three ppt.pptx
Stack and Queue
Stacks,queues,linked-list
Stack.pptx

Similar to CDS artificial intelligence and Machine.docx (20)

PPT
Difference between stack and queue
PDF
Queues and Stacks
PPTX
Data Structures Stack and Queue Data Structures
PPT
Stacks, Queues, Deques
PPT
unit 5 stack & queue.ppt
PPTX
data structures and algorithms Unit 1
PPT
23 stacks-queues-deques
PPTX
Stack and Queue by M.Gomathi Lecturer
PPTX
data structures with algorithms vtu 2023 notes.pptx
PPTX
VCE Unit 03vv.pptx
PPTX
Ist year Msc,2nd sem module1
PPTX
Data Structures Algorithms and Applications
PPTX
STACK AND QUEUE CIRCULAR QUEUE PPTS.pptx
PDF
chapter-4-data-structure.pdf
PDF
1.1 ADS data-structure.pdf
PPTX
Data Structure Introduction- Arrays, Matrix, Linked List
PPTX
Stack and its operations, Queue and its operations
PDF
DS Complete notes for Computer science and Engineering
PPTX
DS UNIT2QUEUES.pptx
PPT
Rana Junaid Rasheed
Difference between stack and queue
Queues and Stacks
Data Structures Stack and Queue Data Structures
Stacks, Queues, Deques
unit 5 stack & queue.ppt
data structures and algorithms Unit 1
23 stacks-queues-deques
Stack and Queue by M.Gomathi Lecturer
data structures with algorithms vtu 2023 notes.pptx
VCE Unit 03vv.pptx
Ist year Msc,2nd sem module1
Data Structures Algorithms and Applications
STACK AND QUEUE CIRCULAR QUEUE PPTS.pptx
chapter-4-data-structure.pdf
1.1 ADS data-structure.pdf
Data Structure Introduction- Arrays, Matrix, Linked List
Stack and its operations, Queue and its operations
DS Complete notes for Computer science and Engineering
DS UNIT2QUEUES.pptx
Rana Junaid Rasheed
Ad

Recently uploaded (20)

PPTX
Introduction-to-Literarature-and-Literary-Studies-week-Prelim-coverage.pptx
PPTX
Final Presentation General Medicine 03-08-2024.pptx
PDF
Computing-Curriculum for Schools in Ghana
PPTX
1st Inaugural Professorial Lecture held on 19th February 2020 (Governance and...
DOC
Soft-furnishing-By-Architect-A.F.M.Mohiuddin-Akhand.doc
PDF
Module 4: Burden of Disease Tutorial Slides S2 2025
PPTX
Radiologic_Anatomy_of_the_Brachial_plexus [final].pptx
PDF
ChatGPT for Dummies - Pam Baker Ccesa007.pdf
PDF
The Lost Whites of Pakistan by Jahanzaib Mughal.pdf
PPTX
Microbial diseases, their pathogenesis and prophylaxis
PDF
Updated Idioms and Phrasal Verbs in English subject
PDF
RTP_AR_KS1_Tutor's Guide_English [FOR REPRODUCTION].pdf
PPTX
Cell Types and Its function , kingdom of life
PDF
01-Introduction-to-Information-Management.pdf
PDF
RMMM.pdf make it easy to upload and study
PPTX
Orientation - ARALprogram of Deped to the Parents.pptx
PDF
Complications of Minimal Access Surgery at WLH
PPTX
Final Presentation General Medicine 03-08-2024.pptx
PDF
Weekly quiz Compilation Jan -July 25.pdf
PPTX
UNIT III MENTAL HEALTH NURSING ASSESSMENT
Introduction-to-Literarature-and-Literary-Studies-week-Prelim-coverage.pptx
Final Presentation General Medicine 03-08-2024.pptx
Computing-Curriculum for Schools in Ghana
1st Inaugural Professorial Lecture held on 19th February 2020 (Governance and...
Soft-furnishing-By-Architect-A.F.M.Mohiuddin-Akhand.doc
Module 4: Burden of Disease Tutorial Slides S2 2025
Radiologic_Anatomy_of_the_Brachial_plexus [final].pptx
ChatGPT for Dummies - Pam Baker Ccesa007.pdf
The Lost Whites of Pakistan by Jahanzaib Mughal.pdf
Microbial diseases, their pathogenesis and prophylaxis
Updated Idioms and Phrasal Verbs in English subject
RTP_AR_KS1_Tutor's Guide_English [FOR REPRODUCTION].pdf
Cell Types and Its function , kingdom of life
01-Introduction-to-Information-Management.pdf
RMMM.pdf make it easy to upload and study
Orientation - ARALprogram of Deped to the Parents.pptx
Complications of Minimal Access Surgery at WLH
Final Presentation General Medicine 03-08-2024.pptx
Weekly quiz Compilation Jan -July 25.pdf
UNIT III MENTAL HEALTH NURSING ASSESSMENT
Ad

CDS artificial intelligence and Machine.docx

  • 1. C And Data Structures UNIT V Stack: LIFO concept, Stack operations, Array implementation of stack – Queue: FIFO concept, Queue operations, Array implementation of queue – Singly Linked List: concepts, operations – Doubly Linked List: concepts, operations – Trees: General trees, Binary trees.
  • 2. Data Structures Data Structure: Logical or mathematical model of organizing data is called data structure. i.e. Data Structure = organized data + Operations. A data structure is a way to organize and manage data.  It not only stores data but also provides operations to manipulate that data. For example, an array is a type of data structure that stores data in sequential order.
  • 3. You can perform various operations on an array, such as finding its size, sorting the data, retrieving existing data, and modifying data within it.
  • 4. Data Structures Data Structure: Logical or mathematical model of organizing data is called data structure. i.e. Data Structure = organized data + Operations. Classification of Data structures Linear data structures: A data structure whose elements form a sequence, and every element in the structure has a unique predecessor and unique successor. Eg. Arrays,linked lists, stacks and queues. Non-linear data structures: A data structure whose elements do not form a sequence, and there is no unique predecessor or unique successor. Eg. Trees and graphs.
  • 5. Array Introduction Data structures are classified as either linear or nonlinear. A data structure is said to be linear if its elements form a sequence or a linear list. There are two basic ways of representing such linear structures in memory. One way is to have the linear relationship between the elements represented by means of sequential memory locations. These linear structures are called arrays. The other way is to have the linear relationship between the elements represented by means of pointers or links. These linear structures are called linked lists. Nonlinear structures are trees and graphs. Definition: A array is a list of finite number n of homogeneous data elements such that : a) The elements of the array are referenced respectively by an index set consisting of n consecutive numbers. b) The elements of the array are stored respectively in successive memory locations.
  • 6. Stacks  A stack is an ordered collection of homogeneous data element where the insertion and deletion operations take place at one end called the top of the stack. The other end is called the bottom.  This means, in particular, that elements are removed from a stack in the reverse order of that in which they were inserted into the stack.  An item may be added or removed only from the top of a stack. This means that the last item to be added to a stack is the first item to be removed. Accordingly, stacks are also called Last-In First-Out (LIFO) lists. Applications • Page-visited history in a Web browser • Undo sequence in a text editor • Chain of method calls in a programming language  Two basic operations associated with stacks are 1.“Push” is the term used to insert an element into a stack. 2.“Pop” is the term used to delete an element from a stack. 5
  • 7. AAA BBB CCC DDD EEE FFF . . . . Stacks (Example): Suppose the following 6 elements are pushed, in order, onto an empty stack: AAA, BBB, CCC, DDD, EEE, FFF. The followin fig shows three ways of picturing such a stack. 1 2 3 TOP 4 TOP 5 6 FFF 7 EEE 8 DDD 9 CCC . BBB . AAA N-1 N AAA BBB CCC DDD EEE FFF … 1 2 3 4 5 6 7 8 9 … N-1 N
  • 9. Memory Representation of stack A stack can be represented in memory either as an one-dimensional array or as a singly linked list. Array representation Linked list representation
  • 10. Array Representation of Stacks For the array implementation: Declare an array of fixed size (which determines the maximum size of the stack). Keep a variable which always points to the “top” of the stack, which contains the array index of the “top” element. STACK XXX YYY ZZZ 1 2 3 4 5 6 7 8 TOP 3 MAXSTK 8 The stack is maintained by a linear array STACK: a pointer variable TOP, which contains the location of the top element of the stack; and a variable MAXSTK which gives the maximum number of elements that can be held by the stack. The above Figure pictures such an array representation of a stack. Since TOP = 3, the stack has three elements, XXX, YYY and ZZZ; and since MAXSTK = 8, there is room for 5 more items in the stack. STACK : A linear array TOP : A pointer variable, Which contains the location of the top element of the stack. MAXSTK : Gives the maximum number of elements that can be held by the stack. TOP = 0 or NULL will indicate that the stack is empty
  • 11. operations on Stack : PUSH and POP // Here, STACK is an array to store data. TOP represents in which location the data is to be inserted. MAXSTK is the maximum size of the stack and finally, ITEM is the new item to be added //This procedure pushes an ITEM onto a stack PUSH(STACK,TOP,MAXSTACK,ITEM) 1. [STACK already filled] //Here, STACK is the array where data are stored. TOP represents from which location the data is to be removed. Top element is assigned to ITEM. //This procedure deletes the top element of STACK and assign it to the variable ITEM POP(STACK,TOP,ITEM) If TOP=MAXSTACK, then Print OVERFLOW and Return. 2. Set TOP=TOP+1 3. Set STACK[TOP]=ITEM. 4. Return. 1. If TOP=0, then print UNDERFLOW and Return 2. Set ITEM=STACK[TOP]. 3. Set TOP=TOP-1. 4. Return.
  • 12. 9
  • 13. Pushing 0 1 2 3 4 5 6 7 8 9 STACK
  • 14. 0 17 23 97 44 top = 3 N = 4  If ITEM=80 is to be inserted, then TOP=TOP+1=4  STACK[TOP]:= STACK[4]:= ITEM=80  N=5  MAXSTACK=10 1 2 3 4 5 6 7 8 9 STACK
  • 15. 17 23 97 44 80 top = 4 N = 5
  • 16. Poping 0 1 2 3 4 5 6 7 8 9 STACK
  • 17. 17 23 97 44 80 top = 4 N = 5  If ITEM=80 is to be deleted, then ITEM:=STACK[TOP]= STACK[4]= 80  TOP=TOP-1=3  N=4  MAXSTACK=10 0 1 2 3 4 5 6 7 8 9 STACK
  • 18. 17 23 97 44 top = 3 N = 4
  • 19. QUEUES  A Queue is a linear list of elements in which deletions can take place only at one end, called the front, and insertions can take place only at the other end, called the rear.  Queues are also called first-in first-out (FIFO) lists, since the first element in a queue will be the first element out of the queue.  In other words, the order in which elements enter a queue is the order in which they leave. This contrasts with stacks, which are Last-in First-out (LIFO) lists. Examples: 1. The automobiles waiting to pass through an intersection form a queue, in which the first car in line is the first car through 2. The people waiting in line at a bank(or ticket counter) form a queue, where the first person in line is the first person to be served. 3. An important example of a queue in computer science occurs in a timesharing system, in which programs with the same priority form a queue while waiting to be executed. 4. Queue for printing purposes(Documents sent to a shared printer)
  • 20. REPRESENTATION OF QUEUE Like Stacks, Queues can also be represented in memory in two ways. 1. Linear Array 2. One-way Linked List
  • 21. variables: increased by 1; this can be implemen ed by the assignment REAR is increased by 1; this can be implemented by the assignment Array Representation of Queues Queues can be easily represented using linear arrays. Each of our queues will be maintained by a linear array QUEUE and two pointer  FRONT, containing the location of the front element of the queue; and REAR, containing the location of the rear element of the queue. The condition FRONT = NULL will indicate that the queue is empty. when an element is deleted from the t queue(called dequeue), the value of FRONT is FRONT := FRONT + 1 Similarly, whenever an element is added to the queue (called enqueue), the value of
  • 22. QUEUE[N].  REAR := REAR + 1 This means that after N insertions, the rear element of the queue will occupy
  • 24. condition of underflow. OPERATIONS ON QUEUE The two basic operations that can be performed on a queue are: Insert (enqueue) : to insert an element at rear of the queue Delete(dequeue): to delete an element from front of the queue. Before inserting a new element in the queue, it is necessary to test the condition of overflow. Overflow occurs when the queue is full and there is no space for a new element. Similarly, before removing the element from the queue, it is necessary to check the Underflow occurs when the queue is empty.
  • 25. Algorithms for Insert and Delete Operations in Linear Queue For Insert (enqueue )Operation Insert-Queue(Queue, Rear, Front, N, Item) Here, Queue is an array where to store data. Rear represents the location in which the data element is to be inserted and Front represents the location from which the data element is to be removed. Here N is the maximum size of the Queue and Item is the new item to be added. 1. If Rear = N then Print: Overflow and Return. /*…Queue already filled..*/ 2. Set Rear := Rear +1 3. Set Queue[Rear] := Item 4. Return.
  • 26. For Delete(dequeue) Operation Delete-Queue(Queue, Front, Rear, Item) Here, Queue is an array where data are stored. Rear represents the location in which the data element is to be inserted and Front represents the location from which the data element is to be removed. Front element is assigned to Item. 1. If Front = N+1 then Print: Underflow and Return. /*…Queue Empty 2. Set Item := Queue[Front] 3. Set Front := Front + 1 4. Return.
  • 27. Example: Consider the following queue (linear queue). Rear = 4 and Front = 1 and N = 7 10 50 30 40 1 2 3 4 5 6 7 (1) Insert 20. Now Rear = 5 and Front = 1 10 50 30 40 20 1 2 3 4 5 6 7 (2) Delete Front Element. Now Rear = 5 and Front = 2 50 30 40 20 1 2 3 4 5 6 7 (3) Delete Front Element. Now Rear = 5 and Front = 3 30 40 20 1 2 3 4 5 6 7 (4) Insert 60. Now Rear = 6 and Front = 3 30 40 20 60 1 2 3 4 5 6 7
  • 28. Linked Lists • A linked list is a linear collection of data elements (linear data structure). • Each element is represented by a node. • Each node is divided into two parts. 1. Info or Data: The information stored in the node. 2. Link or next: A pointer that holds the address of the next node in the sequence. 1. Linked list has a list pointer variable, Head or Start, containing the address of the 1st node in the list. • If Start contains NULL value, it means the list is empty. • The address field of last node in the list contains NULL representing invalid address.
  • 29. START 3 1 2 3 4 5 6 7 8 INFO A M G N O LINK 5 2 7 4 0 START=3, INFO[3]=M LINK[3]=2, INFO[2]=A LINK[2]=5, INFO[5]=N LINK[5]=4, INFO[4]=G LINK[4]=7, INFO[7]=O LINK[7]=0, NULL value, So the list has ended
  • 30. Linked List as Students in a Classroom • Each student represents a node in the linked list. Each student has some information, like their roll number. • The student with the first roll number (e.g., Student 1) is the starting point, or the head of the linked list. • Each student knows who comes next. For example, Student 1 knows who has roll number 2, Student 2 knows who has roll number 3, and so on. This is like each node pointing to the next node in the list. • To find a specific student (e.g., Student 50), start with the first student and follow the chain of connections until you reach Student 50. • The last student in the sequence doesn't know of anyone after them, indicating the end of the list.
  • 31. • Example: The following figure shows a linked list having 5 nodes. Information Field Link Field 23
  • 32. Memory Representation of Linked List • A linked list can be maintained in memory using two linear arrays. 1. Info 2. Link • Info[K] represents the information part of a node in the list. • Link[K] represents the nextpointer field of a node in the list. • The beginning of the list is denoted by the variable Start. Figure: Linked list & its memory representation 24
  • 33. arrays have a fixed size that must be specified at the time of creation. arrays require shifting of elements, which can be time- consuming. that are actually being used, whereas arrays require contiguous memory a location. and graphs, whereas arrays are limited to a single dimension. elements to be stored in a contiguous block of memory. advantages of linked list over arrays 1.Dynamic Size • Linked lists can grow or shrink dynamically as elements are added or removed, whereas 2.Efficient Insertion and Deletion • Linked lists allow for efficient insertion and deletion of elements at any position, whereas 3.Good Memory Utilization • Linked lists can make efficient use of memory, as memory is allocated only l for the elements 4.Flexible Data Structure • Linked lists can be used to implement various data structures such as stacks, queues, trees, 5.No Fixed Order • Linked lists do not require elements to be stored in a specific order, whereas arrays require
  • 34. Linked List Operations: Insertion and Deletion Insertion: A B C tmp X Item to be inserted curr A B C
  • 35. X
  • 36. Single Linked List: Inserting a Node in a Linked List Steps to insert node at any position of Singly Linked List Step 1: Create a new node. newNode = createNode(data) Step 2: Traverse to the n−1th Position and Connect the New Node (newNode.next = current.next) where current node is the n-1th node.
  • 37. Single Linked List: Insertion at any position Step 3: Connect the n−1th Node to the New Node (current.next = newNode) where current node is the n-1th node.
  • 38. Linked List Operations: Insertion and Deletion Deletion Item to be deleted A B C curr tmp A C B
  • 39. Singly Linked List: Deleting a node from the linked list Steps to Delete the Nth Node from a Singly Linked List: Step 1: Traverse to the nth node of the singly linked list and also keep reference of n-1th node in some temp variable say prevNode. Step 2: Reconnect n-1th node with the n+1th node i.e. prevNode.next = toDelete.next (This sets the next pointer of the (n−1)th node (prevNode) to point to the (n+1)th node (toDelete.next)).
  • 40. Single Linked List: Deletion at any Position Step 3: Free the memory occupied by the nth node i.e. toDelete node.
  • 41. Doubly Linked List In a singly linked list, each node has just one pointer that points to the next node. In contrast, a doubly linked list has two pointers in each node: one that points to the next node and another that points to the previous node. This setup allows you to move both forward and backward through the list, making it a two-way chain. Each node in a doubly linked list has three components: Data (INFO): Stores the actual data or value of the node. Forward Link (NEXT): A pointer to the next node (the address of the next node) Backward Link (PREV): A pointer to the previous node (the address of the previous node)
  • 42. The purpose of a doubly linked list is to allow traversal in both directions while still using non-contiguous memory. Similar to a singly linked list, it requires a starting pointer that points to the first node. The Prev pointer of the first node (A) and the Next pointer of the last node (D) both point to NULL. The doubly linked list shown has 4 nodes with data points A, B, C, and D. Each node contains two links: one pointing to the next node and one pointing to the previous node. Each node in the list consists of two pointers and a data field: a. Data: Holds the value of the data item within the node. b. Prev: A pointer that references the previous node. For the first node, Prev points to NULL.
  • 43. points to NULL. c. Next: A pointer that links the current node to the next node in the list. The Next pointer of the last node
  • 44. representation of a linked list. The Prev pointer of the f rst node node (D) both point to NULL. indicates that the values do not The numbers 1000, 1056, 2004, We traverse the list until we a node. The image below illustrates the memory -1 denotes NULL.  (A) and the Next pointer of i the last This memory representation need to be stored contiguously.  etc., represent memory addresses.  encounter -1 in the Next pointer of Basic Operations on a Doubly Linked List:
  • 45. 1. Insertion: This operation adds a new element at a specified position in the list. 2. Deletion: This operation removes a node from the list.
  • 46. Inserting a New Node at the Nth Position in a Doubly Linked List 1. Create a new node: Ptr (or newNode) with the given data. 2. Traverse the list: Reach the (N-1)th node, where temp points to this node. 3. Link the new node to the next node: Ptr.next = temp.next; 4. Link the new node to the previous node: Ptr.prev = temp; 5. Connect temp to the new node: temp.next = Ptr; 6. Adjust the backward link of the following node: If Ptr.next is not null, set its previous link to Ptr: Ptr.next.prev = Ptr;
  • 47. Deleting a Node at the Nth Position in a Doubly Linked List 1. Traverse the List: Reach the (N-1)th node, where temp points to this node. 2. Identify the Node to be Deleted: nodeToDelete = temp.next; (the node to be deleted is the next node) 3. Link the Previous Node to the Next Node: temp.next = nodeToDelete.next; (bypass the node to be deleted) 4. Adjust the Backward Link of the Following Node: If nodeToDelete.next is not null, nodeToDelete.next.prev = temp; (update the previous pointer) 5. Free the Node: nodeToDelete = null; (release the memory occupied by the deleted node).
  • 48. Trees Trees are a fundamental data structure used in computer science to represent hierarchical relationships (parent-child relationships) between elements. It's similar to a real-life tree but inverted, with the root at the top and branches that spread downwards. Tree in real life and Computer Science
  • 50. Tree Structure in computer science
  • 51. General Trees A tree is a finite collection of one or more nodes with the following properties: i) There is a specially designated node called the root. ii) The other nodes are divided into n≥0 disjoint sets, T1,…,Tn, where each set is itself a tree. T1, T2, …, Tn are called the sub-trees of the root. A tree consists of a collection of elements or nodes, with each node linked to its successors
  • 52. Terminologies 1. Node: A node is a basic unit of a tree. Each node contains data and may link to other nodes. For example, consider a tree diagram with 13 nodes, each represented by a letter for simplicity. 2. Edges: The connections between nodes are called edges. 3. Root: The topmost node of a tree. It is the starting point and doesn't have any parent. In the example, node A is the root. 4. Degree of a Node: The number of subtrees a node has. For instance, node A has a degree of 3, node C has a degree of 1, and node F has a degree of 0. 5. Leaf (or Terminal) Nodes : A node with no children. It is the endpoint of a branch in the tree. In the example, nodes {K, L, F, G, M, I, J} are leaf nodes. 6. Non-Terminal Nodes: Nodes that are not leaf nodes; they have at least one child. 7. Children and Parent: The roots of the subtrees of a node X are called its children, and X is referred to as their parent. For example, the children of node D are H, I, and J, and the parent of D is A. 8. Siblings: Children of the same parent. For instance, nodes H, I, and J are siblings. 9. Extended Family Terms: The terminology can extend to terms like grandparents, where, for instance, the grandparent of node M is D. 10. Degree of a Tree: The maximum degree among all the nodes in the tree. In the example, the tree's degree is 3. 11. Ancestors: All nodes along the path from the root to a specific node. For example, the ancestors of node M are A, D, and H. The level of a node in a tree is determined by assigning the root to level one. If a node is at level l, its children will be at level l+1 The height or depth of a tree is the maximum level of any node in the tree. A forest in a tree structure is a collection of one or more disjoint trees. The concept of a forest is closely related to a tree; removing the root node of a tree transforms it into a forest. For example, removing the root node A from a tree would result in a forest of three separate trees.
  • 53. • These terms help describe the hierarchical relationships and structure within a tree, making it easier to understand and work with these data structures.
  • 54. each node can have at most two children. child. m atters. node. subtrees Binary Trees • A binary tree is a type of tree structure where Examples • Each node can have a left child and a right • The order of the subtrees in a binary tree • A binary tree can be empty, meaning it can • regular tree, which must have at least one Definition: A binary tree is a finite set of nodes separate . binary trees called the left and right o This makes a binary tree different from a have zero nodes. either empty or consists of a root node and tw whe re each node has at most two children. It is
  • 55. NOTE: • The maximum number of nodes on level i of a binary tree is 2i , i>=1. • The maximum number of nodes in a binary tree of depth k is 2k -1, k>=1.
  • 56. Full binary tree A binary tree is a full binary tree, if it contains maximum possible number of nodes in all levels. A full binary tree of depth k is a binary tree of depth k having 2k -1 nodes, k>=0. Complete binary tree A binary tree is said to be a complete binary tree, if all its levels, except possibly the last level, have the maximum number of possible nodes, and all the nodes at the last level appear as far left as possible.
  • 57. Representation of binary trees Binary trees can be represented by means of: 1] Array – or sequential representation 2] Linked List – or linked representation. 1.Array Representation In an array representation of a binary tree, the nodes of the tree are stored in an array in level-order . This means the root is stored first, followed by the nodes on the next level from left to right, and so on. For a binary tree of height h the worst-case array size needed is 2h −1
  • 58. Example: In an array representation of a binary tree with a base index of 1: Root node: The root node is always at index 1. Left child: For a node at index i, the left child is at index 2i. Right child: For a node at index i, the right child is at index 2i + 1. In this representation: Odd indexes (like 1, 3, 5, ...) contain nodes that are either root node or right children. Even indexes (like 2, 4, 6, ...) contain nodes that are left children.
  • 59. Linked representation Disadvantages of array representation . • This scheme of representation is quite wasteful of space when binary tree is skewed or number of elements are small as compared to its height. • Array representation is useful only when the binary tree is full or complete. In a linked representation of a binary tree, each node is represented by a structure or class containing the following elements: 1. Data: The value stored in the node. 2. Left Child: A pointer to the left child node. 3. Right Child: A pointer to the right child node.
  • 60. Examples Advantages: Flexible: Linked representations can handle dynamic insertion and deletion of nodes. Efficient: Linked representations can be more memory-efficient than array-based representations, especially for sparse trees. Disadvantages: Complex: Linked representations require more complex code to manage the pointers (or references) between nodes.
  • 61. node in a tree exactly once in a specific order. Inorder, Preorder, and Postorder. Binary Tree Traversals Binary tree traversal refers to the process of visiting each There are three main traversal methods for binary trees: 1.Inorder Traversal (LNR) 1. Traverse the left subtree in Inorder. (L) 2. Process the root node.(N) 3. Traverse the right subtree in Inorder. (R) 2.Preorder Traversal (NLR ) 1. Process the root node.(N) 2. Traverse the left subtree in Preorder.(L) 3. Traverse the right subtree in Preorder. (R) 3.Postorder Traversal (LRN) 1. Traverse the left subtree in Postorder. (L) 2. Traverse the right subtree in Postorder. (R) 3.Process the root node.(N)
  • 62. Algorithms for binary Tree traversals 1.Inorder Traversal (LNR) void inorder(Node root) { if (root == null) return; inorder(root.left); // Traverse left subtree print(root.data ); // Visit root node inorder(root.right); // Traverse right subtree } 2. Preorder Traversal (NLR) void preorder(Node root) { if (root == null) return; 3. Postorder Traversal (LRN) void postorder(Node root) { if (root == null) return; postorder(root.left); // Traverse left subtree postorder(root.right); // Traverse right subtree .print(root.data ); // Visit root node } print(root.data ); preorder(root.left); preorder(root.right); }
  • 63. // Visit root node // Traverse left subtree // Traverse right subtree
  • 64. Illustrations for Traversals • Assume: visiting a node is printing its label • Inorder: (LNR) 4 5 6 3 1 8 7 9 11 10 12 • Preorder: (NLR) 1 3 5 4 6 7 8 9 10 11 12 • Postorder: (LRN) 4 6 5 3 8 11 12 10 9 7 1 1 3 7 5 8 9 4 6 10 11 12 53