Download as pdf or txt
Download as pdf or txt
You are on page 1of 21

Remove Watermark Wondershare

PDFelement

MCS-021 {DATA AND FILE STRUCTURES } BCA(3) SOLVED


ASSIGNMENT / 2021-2022
ARPITA SHILPI
Q.1 Write an algorithm that accepts a Binary Tree as inputs and outputs the traversals of
Inorder , Postorder and Preorder of it.

traversal can be done in the following ways.

Inorder traversal

Preorder traversal

Postorder traversal

Consider the given binary tree,

binary tree traversal

1
Remove Watermark Wondershare
PDFelement

Inorder Traversal: 7 9 4 2 5 1 3 6 8

Preorder Traversal: 1 2 4 7 9 5 3 6 8

Postorder Traversal: 9 7 4 5 2 8 6 3 1

Inorder Traversal: For binary search trees (BST), Inorder Traversal specifies the nodes in non-
descending order. In order to obtain nodes from BST in non-increasing order, a variation of
inorder traversal may be used where inorder traversal is reversed.

Preorder Traversal: Preorder traversal will create a copy of the tree. Preorder Traversal is also
used to get the prefix expression of an expression.

Postorder Traversal: Postorder traversal is used to get the postfix expression of an expression
given

2
Remove Watermark Wondershare
PDFelement

Algorithm for binary tree traversal

Inorder(root)

Traverse the left sub-tree, (recursively call inorder(root -> left).

Visit and print the root node.

Traverse the right sub-tree, (recursively call inorder(root -> right).

Preorder(root)

Visit and print the root node.

Traverse the left sub-tree, (recursively call inorder(root -> left).

Traverse the right sub-tree, (recursively call inorder(root -> right).

Postorder(root)

Traverse the left sub-tree, (recursively call inorder(root -> left).

Traverse the right sub-tree, (recursively call inorder(root -> right).

Visit and print the root node.

Program for binary tree traversals in inorder, preorder, and postorder traversals is given below.

#include <iostream>

#include <stdlib.h>

using namespace std;

/* Structure for a node */

struct node

int data;

struct node *left;

struct node *right;

};

3
Remove Watermark Wondershare
PDFelement

/* Function to create a new node */

struct node *newNode(int data)

struct node *temp = (struct node *) malloc(sizeof(struct node));

temp -> data = data;

temp -> left = NULL;

temp -> right = NULL;

return temp;

};

/* Function to insert a node in the tree */

void insert_node(struct node *root, int n1, int n2, char lr)

if(root == NULL)

return;

if(root -> data == n1)

switch(lr)

case 'l' :root -> left = newNode(n2);

break;

case 'r' : root -> right = newNode(n2);

break;

4
Remove Watermark Wondershare
PDFelement

else

insert_node(root -> left, n1, n2, lr);

insert_node(root -> right, n1, n2, lr);

/* Function to print the inorder traversal of the tree */

void inorder(struct node *root)

if(root == NULL)

return;

inorder(root -> left);

cout << root -> data << " ";

inorder(root -> right);

/* Function to print the preorder traversal of the tree */

void preorder(struct node *root)

if(root == NULL)

return;

5
Remove Watermark Wondershare
PDFelement

cout << root -> data << " ";

preorder(root -> left);

preorder(root -> right);

/* Function to print the postorder traversal of the tree */

void postorder(struct node *root)

if(root == NULL)

return;

postorder(root -> left);

postorder(root -> right);

cout << root -> data << " ";

/* Main Function */

int main()

struct node *root = NULL;

int n;

cout <<"\nEnter the number of edges : ";

cin >> n;

cout << "\nInput the nodes of the binary tree in order \n\nparent-child-left(or)right-\n\n";

while(n--)

6
Remove Watermark Wondershare
PDFelement

char lr;

int n1,n2;

cin >> n1 >> n2;

cin >>lr;

if(root == NULL)

root = newNode(n1);

switch(lr)

case 'l' :root -> left = newNode(n2);

break;

case 'r' : root -> right = newNode(n2);

break;

else

insert_node(root,n1,n2,lr);

cout <<"\nInorder Traversal : ";

inorder(root);

cout << endl;

7
Remove Watermark Wondershare
PDFelement

cout <<"\nPreorder Traversal : ";

preorder(root);

cout << endl;

cout <<"\nPostorder Traversal : ";

postorder(root);

cout << endl;

return 0;

Output:

Q2. Is it possible to implement multiple queues in a Stack. Justify your answer.

ANS- yes, it is possible to implement multiple stacks in a Queue.Implementing two Stacks in a


Queue.

(1) When calling the enqueue method, simply push the elements into the stack 1.

(2) If the dequeue method is called, push all the elements from stack 1 into stack 2, which
reverses the order of the elements. Now pop from stack 2.

8
Remove Watermark Wondershare
PDFelement

Code

// implement stacks using plain arrays with push and pop functions

var Stack1 = [];

var Stack2 = [];

// implement enqueue method by using only stacks

// and the push and pop functions

function Enqueue(element) {

Stack1.push(element);

// implement dequeue method by pushing all elements

// from stack 1 into stack 2, which reverses the order

// and then popping from stack 2

9
Remove Watermark Wondershare
PDFelement

function Dequeue() {

if (Stack2.length === 0) {

if (Stack1.length === 0) { return 'Cannot dequeue because queue is empty'; }

while (Stack1.length > 0) {

var p = Stack1.pop();

Stack2.push(p);

return Stack2.pop();

Enqueue('a');

Enqueue('b');

Enqueue('c');

Dequeue();

Complexity Analysis:

Time Complexity:

Push operation: O(N).

In the worst case we have empty whole of stack 1 into stack 2.

Pop operation: O(1).

Same as pop operation in stack.

Auxiliary Space: O(N).

Use of stack for storing values.

Method 2 (By making deQueue operation costly)In this method, in en-queue operation, the new
element is entered at the top of stack1. In de-queue operation, if stack2 is empty then all the
elements are moved to stack2 and finally top of stack2 is returned.

10
Remove Watermark Wondershare
PDFelement

enQueue(q, x)

1) Push x to stack1 (assuming size of stacks is unlimited).

Method 2 (By making deQueue operation costly)In this method, in en-queue operation, the new
element is entered at the top of stack1. In de-queue operation, if stack2 is empty then all the
elements are moved to stack2 and finally top of stack2 is returned.

enQueue(q, x)

1) Push x to stack1 (assuming size of stacks is unlimited).

Here time complexity will be O(1)

deQueue(q)

1) If both stacks are empty then error.

2) If stack2 is empty

While stack1 is not empty, push everything from stack1 to stack2.

3) Pop the element from stack2 and return it.

Here time complexity will be O(n)

youtuber channel name or links ||ARPITA SHILPI||

https://youtube.com/channel/UCLt-DOQGHOpr_56-FdOENnw

telegram group link or name ||arpita business classes||

https://t.me/iwuwn

telegram channel link or name ||arpita business classes||

https://t.me/arpitallll

please join for BCA OR MCA students .for all subjects notes .or solve notes, important
questions ,guess papers, and solve assignments are also avhilable in y group or

11
Remove Watermark Wondershare
PDFelement

channels..

thank you

- Arpita Shilpi
Q-3 . List the names of all Sorting Algorithms along with their Complexities (Best case,
Average case and Worst case). List as many names as possible along with their year of
Invention and Inventor. Make necessary assumptions. Best Average Worst

NAME BEST CASE AVERAGE CESE WORST CASE

Selection Sort Ω(n^2) θ(n^2) O(n^2)

Bubble Sort Ω(n) θ(n^2) O(n^2)

Insertion Sort Ω(n) θ(n^2) O(n^2)

Heap Sort Ω(n log(n)) θ(n log(n)) O(n log(n))

Quick Sort Ω(n log(n)) θ(n log(n)) O(n^2)

Merge Sort Ω(n log(n)) θ(n log(n)) O(n log(n))

Bucket Sort Ω(n+k) θ(n+k) O(n^2)

Famous inventors

Many inventors worked on a number of inventions. Others made discoveries which later were
developed into new or better inventions by other inventors or were part of a team that
combined their inventing. Here is a list of some of them.

Alexander Graham Bell: Invented a dehusking machine, telephone , photophone, and made
other technological discoveries which future inventors carried further.

Alfred Nobel: A detonator, Dynamite, Gelignite.

Benjamin Franklin: Lightning rod, bifocal glasses and many others.

Isaac Newton: Reflecting telescope and other discoveries.

12
Remove Watermark Wondershare
PDFelement

Johannes Gutenberg: Designed and built the first printing press using movable type and
mechanised ink. This helped print a large number of books at a lower cost for the first time.

Josephine Cochrane: Invented the dishwasher .

Leonardo da Vinci: Made designs for inventions such as the flying machine, war machines and
more.

Marion Donovan: Developed the first waterproof disposable diaper.

Margaret Knight: Invented the square-bottomed paper bag .

Thomas Edison: Worked with electricity , light bulbs , telephones, motion picture cameras and
more.

Famous inventions

Here is a list of some inventions and their inventors but there are many more. You will find
information about these on the websites we have chosen for this entry.

Chocolate chip cookies: Ruth Graves Wakefield.

Clock: Various inventors at different stages - from the sundial to atomic clocks.

Computer: Charles Babbage, Howard Aiken and others.

Flush toilet system: Sir John Harrington.

13
Remove Watermark Wondershare
PDFelement

Light bulb: Joseph Swan (UK) Thomas Edison (US).

Pencil: Prehistoric with various modifications through the centuries.

Plastic: Alexander Parkes.

Telegraph (wireless): Guglielmo Marconi.

Television: John Logie Baird.

Q-4. Show the effect of making the following insertions into a Binary Search Tree which is
already

having one node consisting of 91 (value): 50, 30, 40, 60, 10, 80, 90, 5, 100

ANS-A Red-Black Tree or RB Tree, for short, is a self-balancing Binary Search Tree wherein each
node is colored either Red or Black. Umm, confused? Let’s go over it bit-by-bit:

A Binary Search Tree, or BST for short, is a binary tree wherein each node follows these two
properties:

1) For a given node, value of each node in it’s left subtree is less than the given node.

2) For a given node, value of each node in it’s right subtree is greater than the given node.

Apply these two properties to each node in the BST.

14
Remove Watermark Wondershare
PDFelement

Self-Balancing BST is a BST that keeps its height as minimal as possible at all times including but
not limited to insertion and deletion.

So, a RB tree satisfies the following four properties (Also the above mentioned property of BST):

Every node is either colored red or black.

Root of tree is always black.

There are no two adjacent red nodes (A red node cannot have a red parent or red child) - refer
it to as RR property.

Every simple path from a given node, downward to the leaf contains the same number of black
nodes.

Now, let’s go over the solution in depth. I have tried to explain each insertion step-by-step, but
in case something is unclear, do let me know in comment.

For following up more on the four cases I mentioned, watch the Youtube video by Michael
Sambol [2].

15
Remove Watermark Wondershare
PDFelement

16
Remove Watermark Wondershare
PDFelement

17
Remove Watermark Wondershare
PDFelement

18
Remove Watermark Wondershare
PDFelement

19
Remove Watermark Wondershare
PDFelement

20
Remove Watermark Wondershare
PDFelement

21

You might also like