0% found this document useful (0 votes)
33 views22 pages

DS MID Term

mid term practicles

Uploaded by

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

DS MID Term

mid term practicles

Uploaded by

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

Mid Term/Practice Examination

STUDENT NAME: Rohit Maurya

URN: 2022-M-05102002

COURSE NAME: Data Structure using C++

COURSE CODE: MC502E


PROGRAM &
MCA (DS)
SPECIALIZATION:
ACADEMIC YEAR: 2022-2023

MOOC TITLE:
Link:

Topics covered under MOOC Content:


1. Linked List
2. Binary Trees
3. Circular Queue
4. Stack and Queue
5. Decision Tree
1. LinkedList
1.LinkedList
A linked list is a type of linear data structure in which the elements are not stored in
consecutive memory locations. A linked list's elements are linked using pointers, as shown in
the image below:

Traverse a Linked List


Traversing a linked list refers to accessing the nodes of a linked list in order to process it.
Normally, we use the traverse operation to display the contents of a linked list or to search
for an element within it. The following algorithm is for traversing a linked list.
Algorithm:-

Step 1: [INITIALIZE] SET PTR = HEAD


Step 2: Repeat Steps 3 and 4 while PTR != NULL
Step 3: Apply process to PTR -> DATA
Step 4: SET PTR = PTR->NEXT
[END OF LOOP]
Step 5: EXIT
Inserting Elements to a Linked List
Take into consideration the linked list in the illustration. Consider the scenario where we
want to add a new node to the list as the first node, with the value 24. Seen in the figure is a
connected list. As the list's last node, let's say we wish to add a new node with the data 24.
Algorithm for front:-
Step 1: IF AVAIL = NULL
Write OVERFLOW
Go to Step 7
[END OF IF]
Step 2: SET NEW_NODE = AVAIL
Step 3: SET AVAIL = AVAIL -> NEXT
Step 4: SET NEW_NODE -> DATA = VAL
Step 5: SET NEW_NODE -> NEXT = HEAD
Step 6: SET HEAD = NEW_NODE
Step 7: EXIT

Algorithm For End:-


Step 1: IF AVAIL = NULL
Write OVERFLOW
Go to Step 10
[END OF IF]
Step 2: SET NEW_NODE = AVAIL
Step 3: SET AVAIL = AVAIL -> NEXT
Step 4: SET NEW_NODE -> DATA = VAL
Step 5: SET NEW_NODE -> NEXT = NULL
Step 6: SET PTR = HEAD
Step 7: Repeat Step 8 while PTR -> NEXT != NULL
Step 8: SET PTR = PTR -> NEXT
[END OF LOOP]
Step 9: SET PTR -> NEXT = NEW_NODE
Step 10: EXIT

Delete a node for Linked List


 Check to see if the linked list is empty. If the list is empty, exit.
 Set HEAD to point to the second node.
 Get rid of the first node from memory.
 Go to the bottom of the list.
 Set the next pointer of the second last node to NULL.
 Remove the last node from memory.

Algorithm for front;-


Step 1: IF HEAD = NULL
Write UNDERFLOW
Go to Step 5
[END OF IF]
Step 2: SET PTR = HEAD
Step 3: SET HEAD = HEAD -> NEXT
Step 4: FREE PTR
Step 5: EXIT

Algorithm for End


Step 1: IF HEAD = NULL
Write UNDERFLOW
Go to Step 8
[END OF IF]
Step 2: SET PTR = HEAD
Step 3: Repeat Steps 4 and 5 while PTR -> NEXT != NULL
Step 4: SET PREPTR = PTR
Step 5: SET PTR = PTR -> NEXT
[END OF LOOP]
Step 6: SET PREPTR -> NEXT = NULL
Step 7: FREE PTR
Step 8: EXIT

Conclusion
Int this topic we learn about LinkedList what is the working how to store inserting deleting
searching operation.
2. Binary Trees
2.Binary Trees
The Binary tree indicates that the node can only have two children. Because the binary name
implies 'two,' each node can have either zero, one, or two children.
Properties of Binary Tree
 The maximum number of nodes at each level of I is 2i.
 The length of the path from the root node to the leaf node defines the tree's height. The
height of the tree shown above is three. As a result, the maximum number of nodes at
height 3 is (1+2+4+8) = 15. In general, at height h, the maximum number of nodes
possible is (20 + 21 + 22+....2h) = 2h+1 -1.
 At height h, the smallest number of nodes possible is h+1.
 The height of the tree will be maximum if the number of nodes is minimal. In contrast,
if the number of nodes is maximised, the tree's height is minimised.

The minimum height can be computed as:


As we know that,
n = 2h+1 -1
n+1 = 2h+1

The maximum height can be computed as:


As we know that,
n = h+1
h= n-1

Binary Tree Traversal


The binary tree data structure differs from the linear data structure. There is only one logical
way to traverse a linear data structure (e.g., arrays, linked lists, etc.). We begin at the
beginning and work our way through each element. A binary tree is a non-linear data
structure with a few different traversal options.

There are three variations of the Depth- first traversal


1. In-order tree traversal.
2. Preorder Tree traversal
3. Postorder traversal
Inorder Binary Tree Traversal
In the in-order binary tree traversal, we the right sub-tree should be visited last, then the
left sub-tree, then the current node. Here is a high-level preorder BST traversal
algorithm. For these examples, we're using recursion, but you could easily build the
same traversal using a while loop.
1. Traverse the left sub-tree (keep visit the left sub tree until you reach leaf node).
2. Visit the current node.
3. Traverse the left sub-tree. (same as #1)

Preorder Binary Search Tree Traversl


The pre-order binary tree traversal involve visit the current node followed by left sub-tree
and finally the right sub-tree. Here is a high-level algorithm for preorder BST traversal. We
are using recursion for these examples, you can also implement the same traversal using
while loop.
1. Visit current node.
2. Traverse the left sub-tree.
3. Traverse right sub-tree.

Postorder Binary Tree Traversal


We walk through the left and right subtrees of the post-order binary tree before arriving at
the root node. Here is the algorithm for the post-order BST traversal.
1. Traverse the left sub tree.
2. Traverse the right sub tree.
3. Visit the node
Conclusion
In this topic we learn about binary tree working of binary tree in this topic we covered searching
and traversal operation and its type .
3.Circular Queue
Circular Queue
There was one limitation in Queue's array implementation. If the back reaches the end of the
queue, there is a chance that some empty spaces will be left at the beginning that cannot be
used. To overcome these constraints, the concept of the circular queue was introduced. A
circular queue is similar to a linear queue in that it is also based on the FIFO (First In First
Out) principle, except that the last position in a circular queue is connected to the first
position. It is also referred to as a Ring Buffer.

Conclusion
In this topic we learn about Circular Queue which are the a part of queue
In this queue the last node refer the first data address so it working like a circle after last comes
first and before first come last .
4. Stack and Queue
Stack
A stack is an Abstract Data Type (ADT) that is found in almost all programming languages.
It is called a stack because it behaves like a real-world stack, such as a deck of cards or a pile
of plates.
A real-world stack can only perform operations at one end. For instance, we can only place
or remove a card or plate from the top of the stack. Similarly, Stack ADT allows all data
operations at only one end. We can only access the top element of a stack at any given time.

Because of this feature, it has a LIFO data structure. LIFO is an abbreviation for Last-in-
First-Out. The element that was placed (inserted or added) last is accessed first in this case.
In stack terminology, an insertion operation is referred to as a PUSH operation, while a
removal operation is referred to as a POP operation.

Array, Structure, Pointer, and Linked List can all be used to implement a stack. Stacks can
be either fixed in size or dynamically resized. Stack will be implemented using arrays in this
case, resulting in a fixed size stack implementation.

Fundamental Operations
Initializing the stack, using it, and then de-initializing it are examples of stack operations. Aside
from these fundamentals, a stack is used for the two primary operations listed below.

Pushing (storing) an element on the stack with push().


pop() is used to remove (access) an element from the stack.

Push Operation
The process of putting a new data element onto stack is known as a Push Operation. Push
operation involves a series of steps −

Step 1 − Checks if the stack is full.

Step 2 − If the stack is full, produces an error and exit.

Step 3 − If the stack is not full, increments top to point next empty space.

Step 4 − Adds data element to the stack location, where top is pointing.

Step 5 − Returns success.

Algorithm for PUSH Operation

A simple algorithm for Push operation can be derived as follows −

begin procedure push: stack, data

if stack is full
return null
endif
top ← top + 1
stack[top] ← data
end procedure
Pop Operation
A pop operation is when you access the content while removing it from the
stack. The data element is not actually destroyed in the array implementation of the
pop() action; rather, top is decremented to a lower place in the stack to point to the
subsequent value. However, pop() actually deallocates memory space and removes a
data element in linked-list implementation.

A Pop operation may involve the following steps −

Step 1 − Checks if the stack is empty.

Step 2 − If the stack is empty, produces an error and exit.

Step 3 − If the stack is not empty, accesses the data element at which top is
pointing.

Step 4 − Decreases the value of top by 1.

Step 5 − Returns success.


Algorithm for Pop Operation
A simple algorithm for Pop operation can be derived as follows –
begin procedure pop: stack

if stack is empty
return null
endif

data ← stack[top]
top ← top - 1
return data

end procedure

Queue
Queues are abstract data structures that are similar to Stacks. A queue, unlike a stack, is open
at both ends. The one end is always used to insert data (enqueue), while the other end is
always used to remove data (dequeue). The queue employs the First-In-First-Out (FIFO)
method, which means that the data item stored first will be accessed first.
Representation of a Queue
We now understand that in a queue, we access both ends for different reasons. The following
diagram attempts to explain queue representation as a data structure.

Fundamental Operations
Queue operations may include initialising or defining the queue, using it, and then
erasing it completely from memory. In this section, we will attempt to comprehend
the fundamental operations associated with queues.

 enqueue() adds (stores) a queue item.


 dequeue() removes (allows access to) an item from the queue.

Enqueue Operation
Queues keep two data pointers, one in front and one in the back. As a result, its
operations are more difficult to implement than stacks'.

To enqueue (insert) data into a queue, perform the following steps:

Step 1 Determine whether the queue is full.

Step2: If the queue is full, throw an overflow error and exit.

Step 3: If the queue isn't full, move the rear pointer to the next empty space.

Step 4: Add a data element to the queue location indicated by the rear arrow.

Return success in step 5.


Operation Dequeue
Accessing data from the queue entails two steps: accessing the data where the front is
pointing and removing the data after access. To perform a dequeue operation, take
the following steps:

Step 1 Determine whether the queue is empty.

If the queue is empty, throw an underflow error and exit.

Step 3: If the queue is not empty, access the data indicated by the front.

Step 4: Move the front pointer to the next available data element.

Step 5: Successful return.


Conclusion
In this topic we learn about stack and queue, it’s insertion and deletion operation
Working rear front and operation of push and pop.
5. Decision Tree
Decision Tree
The most powerful and widely used tool for classification and prediction is the Decision
Tree. A Decision tree is a tree structure that looks like a flowchart, with each internal node
representing a test on an attribute, each branch representing a test outcome, and each leaf
node (terminal node) holding a class label.

A decision tree is a non-parametric supervised learning algorithm that can be used for
classification as well as regression tasks. It has a tree structure that is hierarchical and
consists of a root node, branches, internal nodes, and leaf nodes.

A decision tree, as shown in the diagram above, begins with a root node that has no
incoming branches. The root node's outgoing branches then feed into the internal nodes, also
known as decision nodes. Both node types evaluate the available features to form
homogeneous subsets, which are denoted by leaf nodes or terminal nodes. The leaf nodes
represent all of the dataset's possible outcomes. As an example, suppose you were deciding
whether or not to go surfing. You could use the following decision rules to help you decide:
Conclusion
In this topic we learn about decision tree how it work how it take decision what is the
manner of it .

You might also like