0% found this document useful (0 votes)
16 views9 pages

DATA STRUCTURE UNIT III

Uploaded by

clof883
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)
16 views9 pages

DATA STRUCTURE UNIT III

Uploaded by

clof883
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/ 9

UNIT III

1.QUEUE AND METHODS OF QUEUE


ANS: A Queue Data Structure is a fundamental concept in computer science
used for storing and managing data in a specific order. It follows the principle of
“First in, First out” (FIFO), where the first element added to the queue is the
first one to be removed. Queues are commonly used in various algorithms and
applications for their simplicity and efficiency in managing data flow.
Operation Description
enqueue() Process of adding or storing an element to the end of the queue
dequeue() Process of removing or accessing an element from the front of the
queue
peek() Used to get the element at the front of the queue without
removing it
*code
queue = []
queue.append('a')
queue.append('b')
queue.append('c')
print("Initial queue")
print(queue)
print("\nElements dequeued from queue")
print(queue.pop(0))
print(queue.pop(0))
print(queue.pop(0))
print("\nQueue after removing elements")
print(queue)
#output
Initial queue
['a', 'b', 'c']
Elements dequeued from queue
a
b
c
Queue after removing elements
[]

2.TREE AND TERMINOLOGY OF TREE


ANS: Tree data structure is a specialized data structure to store data in
hierarchical manner. It is used to organize and store data in the computer to be
used more effectively. It consists of a central node, structural nodes, and sub-
nodes, which are connected via edges. We can also say that tree data structure
has roots, branches, and leaves connected.
TERMINOLOGY OF TREE
Tree is a hierarchical data structure where data is organized in a parent-child
relationship. It has a root node with child nodes connected by edges. Key terms
include root, edge, parent, child, siblings, degree, internal node, leaf node,
level, height, depth, subtree, path, ancestors, and forest.

3.BFS
ANS: Breadth First Search (BFS) is a fundamental graph traversal algorithm. It
begins with a node, then first traverses all its adjacent. Once all adjacent are
visited, then their adjacent are traversed. This is different from DFS in a way
that closest vertices are visited before others.
from collections import deque
def bfs(graph, root):
visited = set()
queue = deque([root])
while queue:
vertex = queue.popleft()
print(vertex, end=" ")

for neighbour in graph[vertex]:


if neighbour not in visited:
visited.add(neighbour)
queue.append(neighbour)

# Example graph represented as an adjacency list


graph = {
'A' : ['B','C'],
'B' : ['D', 'E'],
'C' : ['F'],
'D' : [],
'E' : ['F'],
'F' : []
}

bfs(graph, 'A') # Output: A B C D E F

4.INORDER AND INFIX


ANS: Inorder Traversal: Inorder traversal is a technique used to traverse a
binary tree. It visits the nodes in the following order:
1. Left subtree
2. Root node
3. Right subtree
In other words, it visits the left subtree, then the root node, and finally the
right subtree. This traversal method is commonly used in binary search trees
(BSTs) to retrieve the nodes in ascending order.
Infix Notation: Infix notation is a way of representing mathematical expressions
where operators are placed between operands. For example:
A+B
In this expression, A and B are operands, and + is the operator. Infix notation is
the most common way of writing mathematical expressions.
Now, let's connect the two concepts. When we traverse a binary tree using
inorder traversal, we can represent the tree as an infix expression. For example,
consider the following binary tree:
VerifyOpen In EditorEditCopy code
+
/\
A B
This is because we visit the left subtree (A), then the root node (+), and finally
the right subtree (B).

5.preorder and prefix


ANS:Preorder Traversal: Preorder traversal is a tree traversal algorithm where
we visit the root node first, then recursively traverse the left subtree, and
finally the right subtree. The order of visitation is: Root -> Left -> Right.
Prefix Notation: Prefix notation is a notation for expressing arithmetic and
logical operations where the operator is written before its arguments. For
example, the infix expression A + B would be written in prefix notation as + A B.
Here's a small code example in Python to demonstrate preorder traversal and
prefix notation:
python
class Node:
def __init__(self, value):
self.value = value
self.left = None
self.right = None

def preorder_traversal(root):
if root:
print(root.value, end=' ') # Visit the root node
preorder_traversal(root.left) # Traverse the left subtree
preorder_traversal(root.right) # Traverse the right subtree

def prefix_notation(root):
if root:
print(root.value, end=' ') # Print the operator
prefix_notation(root.left) # Print the left operand
prefix_notation(root.right) # Print the right operand

# Create a sample tree


root = Node('+')
root.left = Node('A')
root.right = Node('*')
root.right.left = Node('B')
root.right.right = Node('C')

print("Preorder Traversal: ", end='')


preorder_traversal(root) # Output: + A * B C
print("\nPrefix Notation: ", end='')
prefix_notation(root) # Output: + A * B C

6.QUEUE AND DEQUEUE


ANS A Queue Data Structure is a fundamental concept in computer science
used for storing and managing data in a specific order. It follows the principle of
“First in, First out” (FIFO), where the first element added to the queue is the
first one to be removed. Queues are commonly used in various algorithms and
applications for their simplicity and efficiency in managing data flow.

dequeue() Process of removing or accessing an element from the front of the


queue
7.ternary search
ANS: Ternary search is a divide-and-conquer algorithm that can be used to
find an element in an array. It is similar to binary search, but instead of
dividing the array into two parts, it divides the array into three parts.
Here is a small code snippet in Python that demonstrates ternary search:

8.recursive function
Ans: Recursive functions are a fundamental concept in programming, and
they can be very powerful when used correctly.
A recursive function is a function that calls itself in its own definition. This
allows the function to repeat its behavior until it reaches a base case that
stops the recursion.
Here is an example of a recursive function in Python that calculates the
factorial of a given integer:
1. The function takes an integer n as input.
2. The base case is when n is 0, in which case the function returns 1 (since
the factorial of 0 is 1).
3. If n is not 0, the function calls itself with n-1 as input, and multiplies
the result by n. This is the recursive step.
4. The function will continue to call itself until it reaches the base case, at
which point it will start returning values back up the call stack.
5. The final result is the product of all the integers from n down to 1,
which is the definition of the factorial.

9.Inorder search
Ans: Inorder search is a tree traversal algorithm that visits the nodes of a
binary search tree in ascending order. It is commonly used to search for a
specific value in a binary search tree.

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy