Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
12 views

Subhajit Das Data Structures and Algorithms

Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views

Subhajit Das Data Structures and Algorithms

Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

Name: Subhajit Das College ID: O23MSD160112

Subject: Data Structures and Algorithm

1. What are the steps to convert a general tree into binary tree?
Converting a general tree into a binary tree involves a specific set of rules to rearrange the
nodes. Here are the steps to perform the conversion:
Root Node: The root of the binary tree is the same as the root of the general tree.
Left Child: The left child of a node in the binary tree is the leftmost child of that node in the
general tree.
Right Child: The right sibling of any node in the general tree becomes the right child of that
node in the binary tree.
Let’s consider a general tree with root node A, and its children B, C, and D, where B is the
leftmost child. In the binary tree:

A remains the root.


B becomes the left child of A.
C and D, being the right siblings of B, become the right children of B and C, respectively.
Here’s a simple code example in Python that demonstrates the conversion process:

class Node:
def __init__(self, value):
self.value = value
self.left = None
self.right = None
self.children = []

def convert_to_binary_tree(root):
if not root:
return None

# Convert the leftmost child


if root.children:
root.left = convert_to_binary_tree(root.children[0])

# Convert the right siblings


current = root.left
for child in root.children[1:]:
current.right = convert_to_binary_tree(child)
current = current.right

return root
def print_binary_tree(root, depth=0):
if root is not None:
print_binary_tree(root.left, depth + 1)
print(' ' * 4 * depth + '->', root.value)
print_binary_tree(root.right, depth + 1)

# Example usage:
root = Node('A')
root.children.append(Node('B'))
root.children.append(Node('C'))
root.children.append(Node('D'))

# Convert the general tree to a binary tree


binary_root = convert_to_binary_tree(root)
print_binary_tree(binary_root)
This code snippet creates a TreeNode class to represent nodes in the tree, defines a convert
function to transform a general tree to a binary tree, and includes a printTree function to
display the tree structure. Remember, this is a simplified example to help you understand the
concept. In practice, you might need to adapt the code to fit the specific details of your tree
structure.

2. What are the operations of the stack?


The stack is a fundamental data structure that operates on the Last In, First Out (LIFO)
principle. Here are the basic operations associated with a stack:

Push: Adds an element to the top of the stack. If the stack is full, it results in an overflow
condition.
Pop: Removes the top element from the stack. If the stack is empty when attempting to pop, it
results in an underflow condition.
Top or Peek: Returns the top element of the stack without removing it from the stack.
isEmpty: Checks if the stack is empty. It returns true if the stack is empty, otherwise false.
isFull: Checks if the stack has reached its maximum capacity. It returns true if the stack is full,
otherwise false.
Size: Returns the number of elements present in the stack.
These operations allow you to manipulate the data within the stack, adhering to its LIFO
nature. For example, in programming, stacks are used for function call management,
expression evaluation, and more.

3. Algorithm to find the largest and smallest element of an array?


To find the largest and smallest elements in an array, you can use the following algorithm:
1. Initialize two variables, largest and smallest, with the first element of the array.
2. Iterate through the array starting from the second element.
3. Compare each element with largest and smallest.
4. Update largest if the current element is greater than largest.
5. Update smallest if the current element is smaller than smallest.
6. Continue until the end of the array.
7. Return the values of largest and smallest.

Here’s a simple Python code snippet that implements this algorithm:


def find_largest_smallest(arr):
if not arr:
return None, None # Return a tuple of None if the array is empty

largest = smallest = arr[0] # Initialize with the first element

for i in range(1, len(arr)):


if arr[i] > largest:
largest = arr[i]
elif arr[i] < smallest:
smallest = arr[i]

return largest, smallest

# Example usage:
array = [3, 1, 4, 1, 5, 9, 2, 6, 5, 3, 5]
largest, smallest = find_largest_smallest(array)
print("Largest:", largest)
print("Smallest:", smallest)

This code will output the largest and smallest numbers in the given array. The time complexity
of this algorithm is O(n), where n is the number of elements in the array, because it requires a
single pass through the array.

You might also like