Professional Documents
Culture Documents
mcs-021 Assignment 21
mcs-021 Assignment 21
PDFelement
Inorder traversal
Preorder traversal
Postorder 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
Inorder(root)
Preorder(root)
Postorder(root)
Program for binary tree traversals in inorder, preorder, and postorder traversals is given below.
#include <iostream>
#include <stdlib.h>
struct node
int data;
};
3
Remove Watermark Wondershare
PDFelement
return temp;
};
void insert_node(struct node *root, int n1, int n2, char lr)
if(root == NULL)
return;
switch(lr)
break;
break;
4
Remove Watermark Wondershare
PDFelement
else
if(root == NULL)
return;
if(root == NULL)
return;
5
Remove Watermark Wondershare
PDFelement
if(root == NULL)
return;
/* Main Function */
int main()
int n;
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 >>lr;
if(root == NULL)
root = newNode(n1);
switch(lr)
break;
break;
else
insert_node(root,n1,n2,lr);
inorder(root);
7
Remove Watermark Wondershare
PDFelement
preorder(root);
postorder(root);
return 0;
Output:
(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
function Enqueue(element) {
Stack1.push(element);
9
Remove Watermark Wondershare
PDFelement
function Dequeue() {
if (Stack2.length === 0) {
var p = Stack1.pop();
Stack2.push(p);
return Stack2.pop();
Enqueue('a');
Enqueue('b');
Enqueue('c');
Dequeue();
Complexity Analysis:
Time Complexity:
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)
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)
deQueue(q)
2) If stack2 is empty
https://youtube.com/channel/UCLt-DOQGHOpr_56-FdOENnw
https://t.me/iwuwn
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
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.
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.
Leonardo da Vinci: Made designs for inventions such as the flying machine, war machines and
more.
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.
Clock: Various inventors at different stages - from the sundial to atomic clocks.
13
Remove Watermark Wondershare
PDFelement
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.
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):
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