0% found this document useful (0 votes)
3 views5 pages

Data Structure Micro

The document provides an overview of data structures, their classifications, and operations. It explains the differences between static and dynamic data structures, as well as algorithms and flowcharts, highlighting their characteristics and applications. Additionally, it covers concepts like stack operations, expression types, and conditions like stack overflow and underflow.

Uploaded by

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

Data Structure Micro

The document provides an overview of data structures, their classifications, and operations. It explains the differences between static and dynamic data structures, as well as algorithms and flowcharts, highlighting their characteristics and applications. Additionally, it covers concepts like stack operations, expression types, and conditions like stack overflow and underflow.

Uploaded by

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

Unit – 1 Determinism: Algorithms are deterministic, meaning that given integers, whereas a primitive data type like int

hat given integers, whereas a primitive data type like int can only store
the same inputs and performed under the same conditions, one integer at a time12.
1. What is data structure ? Explain the classification of Data they will always produce the same outputs1.
Structure? What is the difference between Static & Dynamic Data
Structure?
Efficiency: Algorithms strive to be efficient in terms of time
A data structure is a way of organizing and storing data in a Static Data Structures: These have a fixed size and memory is
and resources. They aim to solve problems or perform tasks in
computer so that it can be accessed and used efficiently 12. It allocated at compile-time1. The size is fixed and cannot be
a reasonable amount of time and with optimal use of
refers to the logical or mathematical representation of data, as modified during runtime1. This means that their memory size
computational resources1.
well as the implementation in a computer program3. Data cannot be changed during program execution1. Examples of
structures allow programs to store and process data static data structures include arrays1.
effectively4. Generality: An algorithm should be designed to solve a Dynamic Data Structures: These have a variable size and
Data structures can be classified into two broad categories3: specific problem or perform a specific task, but it should also memory is allocated at run-time1. The size can be modified
be applicable to a broader class of situations or scenarios1. during runtime1. This means that their memory size can be
changed during program execution1. Memory can be
Linear data structure: Data elements are arranged
dynamically allocated or deallocated during program
sequentially, and each element is connected to its previous What is abstract data type? Explain with a suitable execution1. Examples of dynamic data structures include
and next element. Examples include arrays, lists, stacks, and example. linked lists1
queues5.
Unit – 2
An Abstract Data Type (ADT) is a type or class for objects
Non-linear data structure: Data elements are not arranged in a whose behavior is defined by a set of values and a set of 1. What is Stack? What is FIFO? How to represent stack into
sequential manner. Examples include trees and graphs5 operations1. The definition of ADT only mentions what memory?
operations are to be performed but not how these operations
will be implemented1. It does not specify how data will be
What are the different operations that are performed on Data
organized in memory and what algorithms will be used for
Structure implementing the operations1. It is called “abstract” because it Using the contiguous memory like an array: In this
gives an implementation-independent view1. representation, the stack is implemented using the
Different operations that can be performed on data structures For example, a List is an abstract data type that can be array2. Variables used in this case are STACK (the name of the
include: implemented using a dynamic array and linked list2. A Queue is array storing stack elements), TOP (storing the index where
another example of an ADT, which can be implemented using the last element is stored in array representing the stack), and
linked list-based queue, array-based queue, and stack-based MAX (defining that how many elements, maximum count, can
Traversing: Visiting each element of the data structure only queue2. In these examples, we know the data that we are be stored in the array representing the stack)2.
once1. storing and the operations that can be performed on the data,
but we don’t know about the implementation details2. This is
because every programming language has a different Using the non-contiguous memory like a linked list: In this
Searching: Finding an element in a data structure that satisfies implementation strategy2. representation, the stack is implemented using the dynamic
one or more conditions1. data structure Linked List2. Using linked list for Application of
stack make a dynamic stack. You don’t have the need to
Write some applications of Data Structure. define the maximum number of elements in the
Inserting: Adding elements of the same type in a data stack2. Pointers (links) to store addresses of nodes used are
structure1. TOP (storing the address of topmost element of the Linked list
Data structures are fundamental in computer science and
storing the stack)2
have a wide range of applications. Here are some examples:
2
Deleting: Removing a node from the data structure .
2. Write down some applications of Stack
Operating Systems: Data structures are used in the
Sorting: Arranging the elements in a certain order3. management and organization of data in operating systems1.
A Stack is a linear data structure that follows a Last In First
Out (LIFO) principle and has various applications1234:
What is Algorithm & Flowchart? Differentiate between algorithm & Graphics: In computer graphics, data structures are used to
represent images, objects, and other visual elements1. Function Calls and Recursion: When a function is called, the
flowchart. current state of the program is pushed onto the stack. When
the function returns, the state is popped from the stack to
An algorithm is a step-by-step procedure to solve a problem or resume the previous function’s execution 2.
Compilers: Compilers use data structures like syntax trees and
perform a task12. It refers to a set of instructions that define
symbol tables to parse and translate source code 2.
the execution of work to get the expected results12. Algorithms
can be written in natural language, pseudocode, or Undo/Redo Operations: The undo-redo feature in various
programming languages13. Sometimes algorithms are difficult Databases: Databases use data structures like B-trees and applications uses stacks to keep track of the previous
to understand, and it is also difficult to show looping and hash indexes for efficient data retrieval2. actions2.
branching using an algorithm2.
A flowchart, on the other hand, is a visual representation of an
algorithm or process1243. It uses various symbols to show the Artificial Intelligence: In AI, data structures are used to model Expression Evaluation: Stacks are used for evaluating
operations and decisions to be followed in a program12. A and solve complex problems2. arithmetic expressions consisting of operands and
flowchart can make it easier to understand the process and operators13.
identify potential problems with it4.
Here are some differences between an algorithm and a Networking: Data structures are used in networking to manage
flowchart1: and route data efficiently2. Memory Management: Stacks are used for systematic
memory management1.

An algorithm is a step-by-step procedure to solve a problem, Games: In game development, data structures are used to
while a flowchart is a diagram created with different shapes to store game states, player data, and other information 2. Backtracking: Stacks are used for backtracking, i.e., to check
show the flow of data1. parenthesis matching in an expression1.

These are just a few examples. The choice of data structure


The algorithm is complex to understand, while a flowchart is depends on the specific requirements of the task at hand Task Scheduling: In real-time applications, stacks are used for
easy to understand1. task scheduling4.

What is the difference between Linear & Non-Linear Data


In the algorithm, plain text is used, while in the flowchart, 3. Why stack follow LIFO principle.
symbols/shapes are used1. Structure.
A Stack is a basic data structure where insertion and deletion
Linear Data Structures: In these structures, data elements are of data takes place at one end called the top of the stack1. It
The algorithm is easy to debug, while a flowchart is hard to arranged sequentially or linearly, where each element is follows the Last In First Out (LIFO) principle 1. This principle
debug1. connected to its previous and next element1. Examples include dictates that the last element added to the stack is the first
arrays, stacks, queues, and linked lists1. Linear data structures one to be removed1. In other words, the most recently added
are easy to implement because computer memory is arranged element is always at the top, and any operations on the stack
The algorithm does not follow any rules, while the flowchart in a linear way1. They involve only a single level, allowing all will affect this top element1.
follows rules to be constructed1.
elements to be traversed in a single run1.
The LIFO principle is used in stacks for several reasons:
The algorithm is the pseudo-code for the program, while a Non-Linear Data Structures: In these structures, data elements
flowchart is just a graphical representation of that logic1. are not arranged sequentially or linearly1. Examples include
trees and graphs1. Non-linear data structures are not as easy Function Call Tracking: Stacks help computers remember
to implement as linear data structures1. They involve multiple what functions they’re doing. When you call a function, its
4. What are the characteristics of Algorithm? levels, so all elements cannot be traversed in a single details are pushed onto the stack. When the function finishes,
run1. Non-linear data structures are more memory-friendly, its details are popped from the stack2.
meaning they use memory more efficiently1.
The characteristics of an algorithm are as follows:
Undo/Redo Functionality: We have undo and redo buttons in
What is the difference between Primitive & Non-Primitive Data many apps, like text editors. Stacks help with this. When you
Well-defined steps: Algorithms consist of a specific and do something, it’s pushed onto the stack. Undoing means
Structure?
unambiguous set of instructions or steps that can be followed popping the last action and redoing means pushing it back
to perform a task or solve a problem1. on2.
Primitive Data Structures: These are the basic data structures
that include fundamental data types like integers, floats,
characters, and pointers12. They can hold a single type of Expression Evaluation: Stacks are used for solving math
Input and output: Algorithms take inputs, which are the initial value12. For example, an integer variable can hold an integer
data provided to the algorithm, and produce outputs, which are problems with parentheses. When you read a math expression,
type of value, a float variable can hold a floating type of value, you can use a stack to track what to do with numbers and
the results generated by the algorithm after processing the and a character variable can hold a character type of value 12.
inputs1. symbols, making sure the last thing you put in is the first to be
used2
Non-Primitive Data Structures: These are more complex data
structures that can store multiple data types in a single
Finiteness: Algorithms must have a well-defined termination
entity12. They are further classified into linear (like arrays, 4. What do you understand by Stack Overflow & Stack Underflow?
condition, meaning they eventually reach an endpoint or
linked lists, stacks, queues) and non-linear data structures (like
change after a finite number of steps1.
trees and graphs)12. For example, an array can contain several
Stack Overflow and Stack Underflow are two conditions that 3. Convert the modified expression to Postfix Expression Evaluation2:
can occur in a stack data structure1234: postfix: ED/BC*+A-
Stack Overflow: This occurs when an attempt is made to add 4. Reverse the postfix expression to get the prefix
(push) an element onto a stack that is already full124. In other expression: -+A*BC/ED 1. Read the postfix expression from left to right.
words, when there is no more space in the stack to hold new 2. If the scanned character is an operand, push it to
elements, a stack overflow error occurs124. the stack.
Stack Underflow: This occurs when an attempt is made to So, the prefix form of the expression (A+B*C) – (D/E) is - 3. If the scanned character is an operator, pop two
remove (pop) an element from a stack that is already +A*BC/ED. operands from the stack, perform the operation,
empty1234. In other words, when there are no more elements in and push the result back to the stack.
the stack to be removed, a stack underflow error occurs12 4. Repeat steps 2-3 until all characters are scanned.
5. Explain different types of expression with example. 9. Write postfix form for the above expression 5. The final result will be at the top of the stack.

Expressions can be of various types, each with its own The postfix form of an expression is a notation where the
characteristics and examples: 12.Evaluate the postfix expression : 4 2 $ 3 * 3 – 8 4 / 1 1 + / +
operator is placed after its operands. This is also known as
Constant Expressions: These consist of only constant Reverse Polish notation1.
values. A constant value is one that doesn’t change1. To evaluate the postfix expression 4 2 $ 3 * 3 – 8 4 /
To convert the infix expression (A+B*C) – (D/E) to postfix
Example: 5, 10 + 5 / 6.0. 1 1 + / +, you can use a stack and follow these steps1234:
notation, we follow these steps12:
Integral Expressions: These produce integer results after
implementing all the automatic and explicit type conversions1.
Example: x, x * y, x + int (5.0) where x and y are 1. Scan the infix expression from left to right. 1. Read the postfix expression from left to right.
integer variables. 2. If the scanned character is an operand, output it. 2. If the scanned character is an operand (number),
Floating Expressions: These produce floating point results 3. Else, push it onto the stack.
after implementing all the automatic and explicit type 3. If the scanned character is an operator, pop two
conversions1. Example: x + y, 10.75 where x and y are o If the precedence of the scanned operands from the stack, perform the operation,
operator is greater than the and push the result back onto the stack.
floating point variables.
precedence of the operator in the 4. Repeat steps 2 and 3 until all characters are
Relational Expressions: These yield results of type bool which
stack (or the stack is empty), push scanned.
takes a value true or false1. Example: x <= y, x + y > 2.
it. 5. The final result will be at the top of the stack.
Logical Expressions: These combine two or more relational
expressions and produces bool type results1. Example: x > y o Else, Pop the operator from the
&& x == 10, x == 10 || y == 5. stack until the precedence of the
Pointer Expressions: These produce address values1. scanned operator is less-equal to 13.Consider the following arithmetic infix expression Q. Q : A + ( B
Example: &x, ptr, ptr++ where x is a variable and ptr is a the precedence of the operator * C – (D / E ^ F ) * G ) * H Transform a into its equivalent postfix
pointer. residing on the top of the stack. expression.
Bitwise Expressions: These are used to manipulate data at bit Push the scanned operator to the
level1. Example: x << 3 shifts three bit position to left, y >> stack. To convert the infix expression A + ( B * C – (D / E ^
1 shifts one bit position to right. 4. If the scanned character is an ‘(’, push it to the F ) * G ) * H to postfix notation, we follow these steps123:
stack.
5. If the scanned character is an ‘)’, pop and output
from the stack until an ‘(’ is encountered. 1. Scan the infix expression from left to right.
6. What is the difference between Push & Pop?
Remove both ‘(’ and ‘)’. 2. If the scanned character is an operand, output it.
6. Repeat steps 2-6 until the expression is scanned. 3. If the scanned character is an operator, then:
Push and Pop are two fundamental operations performed on 7. Pop and output from the stack until it is not
the stack data structure1234: empty. o If the precedence of the scanned
Push: This operation adds a new item to the top of the operator is greater than the
stack1234. If the stack is empty, the push operation initializes precedence of the operator in the
it1234. The item being added (pushed) becomes the new top of So, the postfix form of the expression (A+B*C) – stack (or the stack is empty or the
the stack1234. (D/E) is ABC*+DE/-. stack contains a ‘(’), then push it in
Pop: This operation removes an item from the top of the the stack.
stack1234. The item being removed (popped) is always the one o Else, Pop all the operators from the
that was most recently added to the stack, following the Last stack which are greater than or
10.Infix to prefix conversion by using a Stack. Infix to Postfix
In First Out (LIFO) principle1234. After a pop operation, the item equal to in precedence than that of
that was previously second from the top becomes the new top conversion by using a Stack.
the scanned operator. After doing
of the stack1 that Push the scanned operator to
7. Write down algorithm for PUSH & POP operation with related to Infix to Prefix Conversion using a Stack123:
the stack.
the stack. (M-5) 4. If the scanned character is an ‘(’, push it to the
stack.
1. Reverse the infix expression. Note that while
PUSH Operation Algorithm1: 5. If the scanned character is an ‘)’, pop the stack
reversing, each ‘(’ will become ‘)’ and each ‘)’
if TOP = MAX-1 and output it until a ‘(’ is encountered, and
becomes ‘(’.
return "Overflow" discard both the parenthesis.
2. Convert the reversed infix expression to “nearly”
6. Repeat steps 2-6 until the expression is scanned.
else postfix expression. While converting to postfix
7. Pop and output from the stack until it is not
expression, instead of using pop operation to
top = top + 1 empty.
pop operators with greater than or equal
stack[top] = item precedence, here we will only pop the operators
end from stack that have greater precedence.
So, the postfix form of the expression A + ( B * C – (D /
3. Reverse the postfix expression to get the prefix
The steps for the PUSH operation are as follows1: E ^ F ) * G ) * H is ABC*DE^F/G*-H*+.
expression.

1. Check if the stack has some space or if the stack Infix to Postfix Conversion using a Stack456: 14.Evaluate the following postfix expression : P : 5, 6, 2, +, *, 12, 4,
is full. /, -
2. If the stack has no space then display “overflow”
and exit. 1. Scan the infix expression from left to right. To evaluate the postfix expression 5, 6, 2, +, *, 12,
3. If the stack has space then increase top by 1 to 2. If the scanned character is an operand, output it. 4, /, -, you can use a stack and follow these steps1234:
point to the next empty space. 3. If the scanned character is an operator, then:
4. Add the item to the newly stack location, where
top is pointing. 1. Read the postfix expression from left to right.
If the precedence of the scanned operator is greater than the 2. If the scanned character is an operand (number),
precedence of the operator in the stack (or the stack is empty push it onto the stack.
POP Operation Algorithm1: or the stack contains a ‘(’), then push it in the stack. 3. If the scanned character is an operator, pop two
if TOP = -1
operands from the stack, perform the operation,
return "Underflow" and push the result back onto the stack.
Else, Pop all the operators from the stack which are greater 4. Repeat steps 2 and 3 until all characters are
else
than or equal to in precedence than that of the scanned scanned.
item = stack[top] operator. After doing that Push the scanned operator to the 5. The final result will be at the top of the stack.
top = top - 1 stack.
return item
Following these steps, the given postfix expression evaluates
end 4. If the scanned character is an ‘(’, push it to the to 37
1 stack.
The steps for the POP operation are as follows :
5. If the scanned character is an ‘)’, pop the stack 15.What is Polish Notation?
and output it until a ‘(’ is encountered, and Polish Notation1234:
1. Check if the stack has some element or if the discard both the parenthesis. Polish notation, also known as prefix notation, is a
stack is empty. 6. Repeat steps 2-6 until the expression is scanned. mathematical notation in which operators precede their
2. If the stack has no element (i.e., it is empty) then 7. Pop and output from the stack until it is not operands1234. This is in contrast to the more common infix
display “underflow”. empty. notation, where operators are placed between operands, and
3. If the stack has some element, access the data reverse Polish notation (RPN), where operators follow their
element at which top is pointing. operands1234. It was invented by the logician Jan Łukasiewicz
4. Decrease the value of top by 1. in 19241234.

11.Evaluation of prefix expression or Postfix expression. 16.What is the difference between Array & Stack.
8. Write prefix form for the expression : (A+B*C) – (D/E). Difference between Array and Stack567:
Evaluation of Prefix and Postfix Expressions:
The prefix form of an expression is a notation where the Prefix Expression Evaluation1:
operator is placed before its operands. This is also known as Definition: A stack is a linear data structure represented by a
Polish notation1. sequential collection of elements in a fixed order5. An array is
To convert the infix expression (A+B*C) – (D/E) to prefix 1. Read the prefix expression from right to left. a collection of related data values called elements each
notation, we follow these steps12: 2. If the scanned character is an operand, push it to identified by an indexed array5.
the stack.
3. If the scanned character is an operator, pop two
1. Reverse the infix expression: E/D)-(C*B+A( operands from the stack, perform the operation, Principle: Stacks are based on the LIFO principle, i.e., the
2. Replace each ‘(’ with ‘)’ and each ‘)’ with ‘(’ and push the result back to the stack. element inserted at the last, is the first element to come out of
: E/D))-(C*B+A)) 4. Repeat steps 2-3 until all characters are scanned. the list5. In the array the elements belong to indexes5.
5. The final result will be at the top of the stack.
Operations: Insertion and deletion in stacks take place only 5. Write two application of Queue. The operations that can be performed on a deque include 12:
from one end of the list called the top5. Insertion and deletion
in the array can be done at any index in the array5. A queue is a linear data structure that follows the First-In-First-
Out (FIFO) principle. It is used in various applications where
things don’t have to be processed immediately, but have to be
• Insertion at the front
Storage: The stack has a dynamic size5. The array has a fixed processed in the order they arrived. Here are two applications • Insertion at the rear
size5. of Queue:
Task Scheduling: Queues can be used to schedule tasks
• Deletion at the front

Data Types: The stack can contain elements of different data


based on priority or the order in which they were received. For • Deletion at the rear
example, operating systems often use queues to manage
types5. The array contains elements of the same data type5. processes and resources1.
Resource Allocation: Queues can be used to manage and
allocate resources, such as printers or CPU processing
Methods: There are limited number of operations can be time. For instance, in printing systems, queues are used to
performed on a stack: push, pop, peek, etc5. It is rich in RECURSION
manage the order in which print jobs are processed1.
methods or operations that can be perform on it like sorting, 6. Write an algorithm for implement the circular Queue using Array
traversing, reverse, push, pop, etc5. 1. What is Recursion? Explain the types of Recursion

A circular queue is a variant of the queue data structure that


Recursion is a method of solving a computational problem
Pointers: It has only one pointer- the top5. This pointer uses a circular increment process to optimize memory
where the solution depends on solutions to smaller instances
indicates the address of the topmost element or the last utilization. When the rear of the queue reaches the end, it
of the same problem1. It involves a function calling itself
inserted one of the stack5. In arrays, memory can be allocated wraps around to the start of the queue. Here’s a basic
directly or indirectly21. The primary property of recursion is the
in compile-time and is also known as static memory algorithm to implement a circular queue using an array:
ability to solve a problem by breaking it down into smaller sub-
allocation5. problems2. A recursive function must have a base case or
Initialization: stopping criteria to avoid infinite recursion2.
There are two primary types of recursion2345:
Complexity Analysis: Operation Stack Operation Array push O Direct Recursion: When a function calls itself directly, it is
(1) Insert O (n) pop O (1) Delete O (n) peek O (1) Access O (1) Initialize an array queue of size n, where n is the maximum called direct recursion24. This can be further categorized into
isEmpty O (1) Traversal O (n)5. number of elements that the queue can hold. four types: Tail recursion, Head recursion, Tree recursion, and
Nested recursion2.
Indirect Recursion: Indirect recursion occurs when a function
QUEUE Initialize two variables front and rear to -1. calls another function that eventually calls the original
function, forming a cycle24. This results in a more complex
1. Define Queue? Explain the array representation of Queue. structure
A Queue is a linear data structure that is open at both ends Enqueue Operation (Insertion):
and the operations are performed in First In First Out (FIFO)
order1. This means that the element which is first pushed into
1. Increment rear by 1. If rear is equal to n, 2. Write down the recursive algorithm for factorial of a number.
the queue is the first one to be removed1. It is like a line
waiting to purchase tickets, where the first person in line is the set rear to 0.
first person served1. recursive algorithm for calculating the factorial of a number:
Array Representation of Queue2134: PythonAI-generated code. Review and use carefully. More info on
Queues can be represented using arrays2134. In this If front is -1 (indicating the queue is empty), set front to 0. FAQ.
def factorial(n):
representation, the queue is implemented using an
array2134. There are two variables, front and rear, that are # Base case: factorial of 0 or 1 is 1
implemented in the case of every Add the new element at the position pointed to by rear. if n == 0 or n == 1:
queue2134Front and rear variables point to the position from
return 1
where insertions and deletions are performed in a
queue2134. Initially, the value of front and rear is -1 which Dequeue Operation (Deletion): # Recursive case: n! = n * (n-1)!
represents an empty queue2134. After inserting an element into else:
the queue, the value of rear will increase while the value
Check if the queue is empty by checking if front is -1. If it is, return n * factorial(n - 1)
of front remains the same2134. After deleting an element, the
value of front will increase2134. return an error message indicating that the queue is empty.

2. What is Priorty Queue? 3. Write down the recursive algorithm for finding GCD of a number
If the queue is not empty, then get the element at the position
A Priority Queue is an abstract data type that behaves
pointed by front and remove it from the queue.
similarly to a regular queue or stack data structure, but with a The Greatest Common Divisor (GCD) of two or more integers
key difference: each element in a priority queue has an is the largest positive integer that divides both numbers
associated priority123. Elements with higher priority values are without leaving any remainder1. A recursive algorithm for
Increment front by 1 and if front is equal to n, set front to
typically retrieved before elements with lower priority finding the GCD of two numbers, often referred to as Euclid’s
0.
values123. algorithm, can be implemented as follows231:
In a priority queue, elements with high priority are served PythonAI-generated code. Review and use carefully. More info on
before elements with low priority123. If two elements have the FAQ.
same priority, they are served according to their order in the def gcd(a, b):
queue1.
7. What is Circular Queue? Write down the advantage of Circular # Base case: if b is 0, a is the GCD
3. Difference between Input restricted Queue & Output restricted Queue over Linear Queue if b == 0:
Queue. return a
A Circular Queue is a variant of the linear queue data structure
where the front and rear ends are connected. This forms a # Recursive case: gcd of b and a%b

Input Restricted Queue and Output Restricted Queue are two circular structure, hence the name “Circular Queue”. The else:
special cases of a double-ended queue12: primary advantage of a Circular Queue over a Linear Queue lies return gcd(b, a % b)
Input Restricted Queue: In this type of queue, data can be in its efficient utilization of memory and its flexibility in
inserted from one end (rear) but can be removed from both performing operations12345.
ends (front and rear)1. This kind of queue does not follow the
First In First Out (FIFO) principle1. The main operations Advantages of Circular Queue over Linear Queue: 4. Write down the recursion algorithm for Fibonacci Series.
performed on an input restricted queue Efficient Space Utilization: In a Circular Queue, once an item is
are insertRear(), deleteFront(), and deleteRear()1. removed, that spot can be filled again, so no space is The Fibonacci Series is a sequence of numbers where each
Output Restricted Queue: In this type of queue, data can be wasted15. This is not the case in a Linear Queue, where number is the sum of the two preceding ones, usually starting
removed from one end (front) but can be inserted from both insertion is not possible once the rear reaches the last index with 0 and 1123. The recursive algorithm for generating the
ends (front and rear)2. This kind of queue also does not follow even if there are empty locations present in the queue1. Fibonacci series can be implemented as follows:
the FIFO principle2. The main operations performed on an Ease of Performing Operations: In a Linear Queue, FIFO (First PythonAI-generated code. Review and use carefully. More info on
output restricted queue In, First Out) is followed, so the element inserted first is the FAQ.
are insertRear(), insertFront(), element to be deleted first. This is not the scenario in the case def fibonacci(n):
and deleteFront()2. of the Circular Queue as the rear and front are not fixed so the # Base case: Fibonacci of 0 or 1 is the
4. Write down the algorithm for QUEUE INSERTION & DELETION. order of insertion-deletion can be changed, which is very same number
useful1.
if n <= 1:
the algorithm for insertion (enqueue) and deletion (dequeue) Support for Cyclic Operations: The circular nature of this
queue allows for continuous enqueue and dequeue operations, return n
operations in a queue data structure.
Queue Insertion (Enqueue) Algorithm123: making it especially useful for cyclic processes in computer # Recursive case: Fibonacci of n is the
systems4. sum of the two preceding numbers
Minimizing Memory Wastage: Compared to a linear queue, a
else:
1. Check if the queue is full. If it is, return an circular queue minimizes memory wastage by wrapping
overflow error. around at the end of the queue4. return fibonacci(n - 1) + fibonacci(n
- 2)
2. If the queue is not full, insert the new element at
the rear of the queue. 5. Write down the advantages & disadvantages of Recursion
3. If the front of the queue is -1 (indicating the 8. What is De-Queue
queue was empty), set the front of the queue Recursion is a powerful tool in computer science and
to 0. A Deque, also known as a Double-Ended Queue, is a type of programming, allowing problems to be solved by breaking
4. Increment the rear of the queue. queue in which insertion and removal of elements can be them down into smaller sub-problems12. However, like any
performed from both the front and the rear1234. This means it tool, it has its advantages and disadvantages12345.
does not strictly follow the First-In-First-Out (FIFO) principle
Queue Deletion (Dequeue) Algorithm2: that a regular queue does4. Advantages of Recursion:
There are two types of deques12:

1. Check if the queue is empty. If it is, return an Simplicity: Recursive solutions are often more straightforward
underflow error.
2. If the queue is not empty, access the data • Input Restricted Deque: In this type of deque, and easier to understand than their iterative counterparts 12.
element at the front of the queue. insertion can only be performed at one end, but
3. Increment the front of the queue. allows deletion from both the ends12.
Problem Solving: Recursion is excellent for solving problems
4. If the front of the queue is greater than the rear • Output Restricted Deque: In this type of deque, that can be broken down into smaller, similar problems 12.
after the increment, reset the front and the rear deletion can only be performed at one end, but
to -1 to show that the queue is empty. allows insertion at both the ends12.
Code Readability: Recursive code can be more readable and Here is a recursive algorithm to solve the Tower of Hanoi 8. Problems on Binary Search Tree(BST).
easier to understand than iterative code2. problem:
PythonAI-generated code. Review and use carefully. More info on Problems on Binary Search Trees (BSTs) often involve various
FAQ. operations such as insertion, deletion, and searching12. Here
Disadvantages of Recursion: def TowerOfHanoi(n , source, destination, are some common problems:
auxiliary): 1. Insertion in a BST12: Given a BST and a key, the
if n==1: task is to insert the key in the BST at the correct
Memory Usage: Recursive functions can consume more position to maintain the BST property12.
memory as each function call needs to be stored on the stack print("Move disk 1 from
rod",source,"to rod",destination) 2. Search a given key in BST12: Given a BST and a
until the base case is reached1234. key, the task is to find the node with the given
return key in the BST12.
TowerOfHanoi(n-1, source, auxiliary, 3. Deletion from BST12: Given a BST and a key, the
Efficiency: Recursive functions may be less efficient than destination) task is to delete the node with the given key from
iterative solutions in terms of memory and performance 1234. the BST12.
print("Move disk",n,"from rod",source,"to
rod",destination) 4. Determine whether a given binary tree is a BST
or not12: Given a binary tree, the task is to
Debugging Difficulty: Recursive functions can be more TowerOfHanoi(n-1, auxiliary, destination, determine if it is a BST12.
challenging to debug and understand than iterative solutions2. source)
5. Find k’th smallest and k’th largest element in a
BST12: Given a BST and an integer k, the task is
Unit – 4 to find the k’th smallest and k’th largest element
6. Which Data Structure can be used to implement Recursion
in the BST12.
Find the Lowest Common Ancestor (LCA) of two nodes in a
The data structure used to implement recursion is 1. What is non-linear data structure? 12
BST : Given a BST and two nodes, the task is to find the LCA
the Stack1234. The Stack’s Last-In-First-Out (LIFO) property of the two nodes12.
makes it ideal for implementing recursion Non-linear Data Structure1234: Non-linear data structures are
those where data elements are not arranged in a sequential
When a recursive function is called, the current function’s manner. In these data structures, elements are stored in a
hierarchical or a network-based structure that does not follow 9. What is B Tree? How to insert and delete node in a B Tree?
state, including its variables and the location of any return
point, is pushed onto the Stack1234. When the recursive call is a sequential order. They do not follow a linear progression or a
finished, the function’s state is popped from the Stack, and simple order, unlike linear data structures such as arrays or B-Tree1234: A B-Tree is a self-balancing tree data structure that
execution continues at the return point1234. This allows the linked lists1234. maintains sorted data and allows for efficient searches,
function to be suspended and resumed, enabling recursion sequential access, insertions, and deletions 1234. Each node in a
2. What is Tree? Write down the properties of a Tree. B-Tree can contain multiple keys, which allows the tree to have
a larger branching factor and thus a shallower height1. This
7. The Ackerman Function Problems Tree and its Properties56: A Tree is a connected acyclic shallow height leads to less disk I/O, which results in faster
undirected graph5. There is a unique path between every pair search and insertion operations1. B-Trees are particularly well
of vertices in the tree5. A tree with N number of vertices suited for storage systems that have slow, bulky data access
The Ackermann function, named after Wilhelm Ackermann, is
contains (N-1) number of edges5. The vertex which is of 0 such as hard drives, flash memory, and CD-ROMs1.
one of the simplest and earliest-discovered examples of a total
degree is called the root of the tree5. The vertex which is of 1
computable function that is not primitive recursive12. This
degree is called a leaf node of the tree and the degree of an
means that while the function can be computed by a Turing
internal node is at least 25. Trees represent hierarchical
machine or a general-purpose computer language, it cannot be Insertion in a B-Tree5: Insertion in a B-Tree is performed at the
relationships between individual elements or nodes56.
computed by a primitive recursive function12. leaf node level. The algorithm first traverses the B-Tree to find
The Ackermann function is defined for nonnegative integers m the appropriate leaf node where the key can be inserted. If the
and n and is usually given in the following recursive form1: 5. How to represent Binary Tree using Array & Linked List
leaf node has space for the key, it is inserted there. If the leaf
PythonAI-generated code. Review and use carefully. More info on node is full, it is split into two nodes, and the median key is
FAQ. A Binary Tree can be represented in memory using either an moved up to its parent node5.
def Ackermann(m, n): Array or a Linked List12345.
Array Representation24: In an array representation of a binary Deletion in a B-Tree67: Deletion in a B-Tree involves searching
if m == 0:
tree, if a node is stored at index i, its left child is stored at for the node where the key to be deleted exists, deleting the
return n + 1 index 2*i+1 and its right child is stored at index 2*i+224. This key, and balancing the tree if required6. If the key to be deleted
elif m > 0 and n == 0: representation is efficient for complete binary trees, but for is in an internal node, it is replaced by its inorder predecessor
sparse trees, it may lead to wastage of memory space 24. or successor6. If deleting a key causes a node to have fewer
return Ackermann(m - 1, 1)
Linked List Representation135: In a linked list representation of than the minimum number of keys, the tree is rebalanced by
else: a binary tree, each node is a separate object with three fields: borrowing a key from a sibling node or merging nodes6.
return Ackermann(m - 1, Ackermann(m, n one for storing the data and two for storing the references to 1.
- 1)) the left and right child nodes135. This representation is more 10.Problems on B-Tree?
memory-efficient for sparse trees
The Ackermann function grows very rapidly. For
example, Ackermann(3,3) results in 29, an integer of two Problems on B-Trees often involve various operations such as
decimal digits insertion, deletion, and searching1. Here are some common
6. Explain the different types of Binary Tree. (Definition, Diagram, problems:
Example) [Rooted Binary Tree, Full, Complete/Perfect Binary Tree, Insertion in a B-Tree1: Given a B-Tree and a key, the task is to
9. Difference between recursive function & iterative function. AI most Binary Tree, Skewed Binary Tree) insert the key in the B-Tree at the correct position to maintain
the B-Tree property1.
Rooted Binary Tree12: A rooted binary tree is a binary tree in Search a given key in B-Tree1: Given a B-Tree and a key, the
A recursive function is one that calls itself within its code to
which a special node, known as the root, represents the task is to find the node with the given key in the B-Tree1.
solve a problem, while an iterative function uses loops to
starting point. Each node in a rooted binary tree has at most Deletion from B-Tree1: Given a B-Tree and a key, the task is to
repeat some part of the code1234. Here are some key
two children12. delete the node with the given key from the B-Tree1.
differences between them:
Determine whether a given binary tree is a B-Tree or not1:
Full Binary Tree34: A full binary tree, also known as a proper Given a binary tree, the task is to determine if it is a B-Tree1.
Code Size: Recursive functions usually have a smaller code binary tree, is a binary tree in which every node has either zero
size compared to iterative functions. However, iterative or two children34. This means that no node in a full binary tree 11.What is AVL Tree? Explain the different rotation of a AVL Tree?
functions can have a larger code size but are generally more has exactly one child34.
efficient in terms of time complexity12. Complete/Perfect Binary Tree567: A complete binary tree is a AVL Tree1234: An AVL tree, named after its inventors Georgy
binary tree in which every level, except possibly the last, is Adelson-Velsky and Evgenii Landis, is a self-balancing binary
completely filled, and all nodes in the last level are as far left search tree1234. In an AVL tree, the heights of the two child
Memory Usage: Recursive functions require more memory due as possible567. A perfect binary tree is a type of binary tree in subtrees of any node differ by at most one 1234. If at any time
to the overhead of repeated function calls. Each recursive call which every internal node has exactly two child nodes and all they differ by more than one, rebalancing is done to restore
is added to the call stack and takes up memory space. On the the leaf nodes are at the same level567. this property1234.
other hand, iterative functions require less memory as they do AI most Binary Tree89: The term “AI most Binary Tree” seems Rotations in AVL Tree15678: An AVL tree may perform four
not involve such overhead12. to be a typo or a misunderstanding. It could be referring to the kinds of rotations to keep itself balanced15678:
use of binary trees in Artificial Intelligence (AI). Binary trees are
used in AI for various purposes, including decision trees89.
Speed: Recursive functions are generally slower than iterative Skewed Binary Tree101112: A skewed binary tree is a binary tree Left Rotation (LL Rotation): Performed when a node is added
functions due to the overhead of repeated function calls and in which all the nodes have only either one child or no into the right subtree of the right subtree, and the tree gets out
return operations12. child. There are two types of skewed binary trees: left-skewed of balance15.
binary tree and right-skewed binary tree101112.

Usage: If time complexity is the point of focus, and the number Right Rotation (RR Rotation): Performed when a node is added
of recursive calls would be large, it is better to use 7. Define Binary Search Tree with example to the left subtree of the left subtree, and the tree gets out of
iteration. However, if time complexity is not an issue and balance15.
shortness of code is, recursion would be the way to go 1 A Binary Search Tree (BST) is a node-based binary tree data
structure with the following properties123:
Left-Right Rotation (LR Rotation): A combination in which first
10.Write down the recursive algorithm for Tower of Hanoi. left rotation takes place, followed by a right rotation 15.
The left subtree of a node contains only nodes with keys lesser
The Tower of Hanoi is a mathematical puzzle that consists of than the node’s key123.
Right-Left Rotation (RL Rotation): A combination in which first
three rods and a number of disks of different sizes which can
right rotation takes place, followed by a left rotation 15. These
slide onto any rod12345. The puzzle starts with the disks in a
rotations help the AVL tree maintain its balance, ensuring
neat stack in ascending order of size on one rod, the smallest The right subtree of a node contains only nodes with keys
efficient operations15678.
at the top, thus making a conical shape12345. greater than the node’s key123.
The objective of the puzzle is to move the entire stack to
another rod, obeying the following simple rules12345:
12.Problems on AVL Tree
The left and right subtree each must also be a binary search
tree123.
Only one disk can be moved at a time. Problems on AVL Trees often involve various operations such
as insertion, deletion, and searching1. Here are some common
For example, consider a BST with 8 as the root. The left child problems:
Each move consists of taking the upper disk from one of the of 8 is 3 and the right child is 10. The tree further branches out Insertion in an AVL Tree1: Given an AVL Tree and a key, the
stacks and placing it on top of another stack. with 3 having 1 as its left child and 6 as its right child, and so task is to insert the key in the AVL Tree at the correct position
on. This tree follows the BST property where all nodes in the to maintain the AVL Tree property1.
left subtree of a node are less than the node, and all nodes in Search a given key in AVL Tree1: Given an AVL Tree and a key,
No disk may be placed on top of a smaller disk12345. the right subtree are greater the task is to find the node with the given key in the AVL Tree 1.
Deletion from AVL Tree1: Given an AVL Tree and a key, the Database Applications: AVL trees are used in database
task is to delete the node with the given key from the AVL applications where insertions and deletions are less frequent,
Tree1. but there are frequent data lookups3.
Determine whether a given binary tree is an AVL Tree or not1:
Given a binary tree, the task is to determine if it is an AVL
Tree1. Optimized Search: AVL trees are used in applications that
require optimized search3.
13.What is Tree traversal? Write down the concept of preorder,
inorder, postorder traversal.

Tree Traversal12345: Tree traversal is the process of


systematically visiting each node in a tree data structure. It
involves exploring the nodes in a specific order, such as
inorder, preorder, or postorder, to perform operations or
retrieve information12345.

Preorder Traversal6718: In preorder traversal, the nodes are


visited in the following sequence: Root->Left->Right6718. The
algorithm starts by visiting or printing the root node, and then
traversing the left subtree. Once the left subtree has been fully
traversed, the algorithm then moves on to traverse the right
subtree6718.

Inorder Traversal971011: In inorder traversal, the nodes are


visited in the following sequence: Left->Root->Right971011. The
algorithm starts by traversing the left subtree, then visiting or
printing the root node, and finally traversing the right
subtree971011.
Postorder Traversal1213114: In postorder traversal, the nodes
are visited in the following sequence: Left->Right->Root1213114.
The algorithm starts by traversing the left subtree, then moving
on to traverse the right subtree. Once the right subtree has
been fully traversed, the algorithm then visits the root
node1213114.

15.Write a recursive algorithm for Binary Tree traversal with


example

Binary Tree traversal is a process of visiting each node in a


tree data structure exactly once in a systematic way1234. Here
are the recursive algorithms for three common types of binary
tree traversals:

Preorder Traversal1: In a preorder traversal, the root node is


visited first, then the left subtree, and finally the right subtree 1.
Here is a recursive algorithm for preorder traversal:

PythonAI-generated code. Review and use carefully. More info on


FAQ.
def Preorder(node):
if node is not null:
print(node.value)
Preorder(node.left)
Preorder(node.right)

Inorder Traversal1234: In an inorder traversal, the left subtree is


visited first, then the root node, and finally the right subtree 1234.
Here is a recursive algorithm for inorder traversal:

PythonAI-generated code. Review and use carefully. More info on


FAQ.
def Inorder(node):
if node is not null:
Inorder(node.left)
print(node.value)
Inorder(node.right)

Postorder Traversal1: In a postorder traversal, the left subtree


is visited first, then the right subtree, and finally the root node 1.
Here is a recursive algorithm for postorder traversal:

PythonAI-generated code. Review and use carefully. More info on


FAQ.
def Postorder(node):
if node is not null:
Postorder(node.left)
Postorder(node.right)
print(node.value)

16.What are the purpose of AVL tree.

An AVL tree, named after its inventors Georgy Adelson-Velsky


and Evgenii Landis, is a self-balancing Binary Search Tree
(BST) where the difference between heights of left and right
subtrees for any node cannot be more than one 12. The purpose
of an AVL tree is to maintain this balance in order to provide
efficient operations12.
Here are some of the main purposes of AVL trees13:

Efficient Searching: AVL trees are used to index large records


in a database and to efficiently search within that data3.

In-Memory Collections: AVL trees are used for all types of in-
memory collections, including sets and dictionaries3.

You might also like