Sie sind auf Seite 1von 15

Advanced Data Structures and Algorithms

1. Explain the time complexity of selection sort. Also give its derivation ?
2. Differentiate between polynomial and exponential algorithms?

Ans:
3. Explain various operations on binary search trees.

Ans: Binary search tree Definition:

Binary Search Tree is a binary tree where each node contains only smaller
values in its left subtree and only larger values in its right subtree."

There are three types of operations .they are.

1. Insert operation
2. Search operation
3. Deletion operation.

Insertion operation:

 Insert operation is performed with O(log n) time complexity in a binary search


tree.
 Insert operation starts from the root node. It is used whenever an element is
to be inserted.
The following algorithm shows the insert operation in binary search
tree:

Step 1: Create a new node with a value and set its left and right to NULL.

Step 2: Check whether the tree is empty or not.

Step 3: If the tree is empty, set the root to a new node.

Step 4: If the tree is not empty, check whether a value of new node is smaller
or larger than the node (here it is a root node).
Step 5: If a new node is smaller than or equal to the node, move to its left
child.

Step 6: If a new node is larger than the node, move to its right child.

Step 7: Repeat the process until we reach to a leaf node.

Algorithm
If root is NULL
then create root node
return

If root exists then


compare the data with node.data

while until insertion position is located

If data is greater than node.data


goto right subtree
else
goto left subtree

endwhile

insert data

end If

The above tree is constructed a binary search tree by inserting the above
elements {50, 80, 30, 20, 100, 75, 25, 15}. The diagram represents how the
sequence of numbers or elements are inserted into a binary search tree.

2. Search Operation

 Search operation is performed with O(log n) time complexity in a binary search


tree.
 This operation starts from the root node. It is used whenever an element is to
be searched.
The following algorithm shows the search operation in binary search
tree:

Step 1: Read the element from the user .

Step 2: Compare this element with the value of root node in a tree.

Step 3: If element and value are matching, display "Node is Found" and
terminate the function.

Step 4: If element and value are not matching, check whether an element is
smaller or larger than a node value.

Step 5: If an element is smaller, continue the search operation in left subtree.

Step 6: If an element is larger, continue the search operation in right subtree.

Step 7: Repeat the same process until we found the exact element.

Step 8: If an element with search value is found, display "Element is found"


and terminate the function.

Step 9: If we reach to a leaf node and the search value is not match to a leaf
node, display "Element is not found" and terminate the function.

Algorithm
If root.data is equal to search.data
return root
else
while data not found

If data is greater than node.data


goto right subtree
else
goto left subtree

If data found
return node
endwhile

return data not found

end if

https://www.youtube.com/watch?v=oKf-SFhRI6c

4. Explain binary tree traversals.


Ans:

Binary Tree Traversal


Binary tree traversing is a process of accessing every node of the tree and exactly
once. A tree is defined in a recursive manner. Binary tree traversal also defined
recursively.

There are three techniques of traversal:

1. PreorderTraversal
2. PostorderTraversal
3. InorderTraversal

1. Preorder Traversal
Algorithm for preorder traversal

Step 1 : Start from the Root.

Step 2 : Then, go to the Left Subtree.

Step 3 : Then, go to the Right Subtree.


The above figure represents how preorder traversal actually works.

Following steps can be defined the flow of preorder traversal:

Step 1 : A + B (B + Preorder on D (D + Preorder on E and F)) + C (C + Preorder


on G and H)

Step 2 :A + B + D (E + F) + C (G + H)

Step 3 :A + B + D + E + F + C + G + H

Preorder Traversal : A B C D E F G H

2. Postorder Traversal
Algorithm for postorder traversal

Step 1 : Start from the Left Subtree (Last Leaf).

Step 2 : Then, go to the Right Subtree.

Step 3 : Then, go to the Root.


The above figure represents how postorder traversal actually works.

Following steps can be defined the flow of postorder traversal:

Step 1 : As we know, preorder traversal starts from left subtree (last leaf)
((Postorder on E + Postorder on F) + D + B )) + ((Postorder on G + Postorder on
H) + C) + (Root A)

Step 2 : (E + F) + D + B + (G + H) + C + A

Step 3 :E + F + D + B + G + H + C + A

Postorder Traversal : E F D B G H C A

3. Inorder Traversal
Algorithm for inorder traversal

Step 1 : Start from the Left Subtree.

Step 2 : Then, visit the Root.

Step 3 : Then, go to the Right Subtree.


The above figure represents how inorder traversal actually works.

Following steps can be defined the flow of inorder traversal:

Step 1 : B + (Inorder on E) + D + (Inorder on F) + (Root A ) + (Inorder on


G) + C (Inorder on H)

Step 2 :B + (E) + D + (F) + A + G + C + H

Step 3 :B + E + D + F + A + G + C + H

Inorder Traversal : B E D F A G C H

5.Write Pseudo code to insert and delete an element in


linked list.
Insertion Operation
Insertion is a three step process −

 Create a new Link with provided data.


 Point New Link to old First Link.
 Point First Link to this New Link.
//insert link at the first location
void insertFirst(int key, int data){
//create a link
struct node *link = (struct node*) malloc(sizeof(struct node));
link->key = key;
link->data = data;

//point it to old first node


link->next = head;

//point first to new first node


head = link;
}

Deletion Operation
Deletion is a two step process −

 Get the Link pointed by First Link as Temp Link.


 Point First Link to Temp Link's Next Link.
//delete first item
struct node* deleteFirst(){
//save reference to first link
struct node *tempLink = head;

//mark next to first link as first


head = head->next;

//return the deleted link


return tempLink;
}

5. Explain Threaded Binary Tress

Threaded Binary Trees

A binary tree can be represented using array representation or linked list representation. When a

binary tree is represented using linked list representation, the reference part of the node which doesn't

have a child is filled with a NULL pointer. In any binary tree linked list representation, there is a number

of NULL pointers than actual pointers. Generally, in any binary tree linked list representation, if there
are 2N number of reference fields, then N+1 number of reference fields are filled with NULL ( N+1 are

NULL out of 2N ). This NULL pointer does not play any role except indicating that there is no link (no

child).

A. J. Perlis and C. Thornton have proposed new binary tree called "Threaded Binary Tree", which

makes use of NULL pointers to improve its traversal process. In a threaded binary tree, NULL pointers

are replaced by references of other nodes in the tree. These extra references are called as threads.

Threaded Binary Tree is also a binary tree in which all left child pointers that are NULL (in

Linked list representation) points to its in-order predecessor, and all right child pointers that

are NULL (in Linked list representation) points to its in-order successor.

If there is no in-order predecessor or in-order successor, then it points to the root node.

Consider the following binary tree...


To convert the above example binary tree into a threaded binary tree, first find the in-order traversal

of that tree...

In-order traversal of above binary tree...

H-D-I-B-E-A-F-J-C-G

When we represent the above binary tree using linked list representation, nodes H, I, E, F, J and G left

child pointers are NULL. This NULL is replaced by address of its in-order predecessor respectively (I

to D, E to B, F to A, J to F and G to C), but here the node H does not have its in-order predecessor,

so it points to the root node A. And nodes H, I, E, J and G right child pointers are NULL. These NULL

pointers are replaced by address of its in-order successor respectively (H to D, I to B, E to A, and J to

C), but here the node G does not have its in-order successor, so it points to the root node A.

Above example binary tree is converted into threaded binary tree as follows.

A threaded binary tree may be defined as follows: "A binary tree is threaded by making all
right child pointers that would normally be null point to the inorder successor of the node,
and all left child pointers that would normally be null point to the inorder predecessor of the
node."
A threaded tree, with the special threading links shown by dashed arrows
DATA SCIENCE
1. How do you compare Linear Regression with K-Nearest Neighbors?
Ans: Page No-104 OR 118

2. Define Linear Regression. Explain the Linear Regression with example?

Linear regression quantifies the relationship between one or more predictor


variables and one outcome variable. Linear regression is used for predictive
analysis and modeling. For example, linear regression can be used to quantify
the relative impacts of age, gender, and diet (the predictor variables) on height
(the outcome variable). Linear regression is also known as multiple
regression, multivariate regression, ordinary least squares (OLS),
and regression. This post will show you examples of linear regression,
including an example of simple linear regression and an example of multiple
linear regression.
Try your own Linear Regression!

Example of simple linear regression

The table below shows some data from the early days of the Italian clothing
company Benetton. Each row in the table shows Benetton’s sales for a year
and the amount spent on advertising that year. In this case, our outcome of
interest is sales—it is what we want to predict. If we use advertising as the
predictor variable, linear regression estimates that Sales = 168 + 23
Advertising. That is, if advertising expenditure is increased by one Euro, then
sales will be expected to increase by 23 million Euro, and if there was no
advertising we would expect sales of 168 million Euro.
3. Describe the trade-off between Prediction Accuracy and Model
Interpretability
Ans: Pdf -38 OR 25

4. Explain Linear Discriminant Analysis with example?


Ans

5. Explain about LDA, QDA?


Ans: Pg.no-175 or 161
Advance operating System
1. Explain history and standards of UNIX?
Ans:

Das könnte Ihnen auch gefallen