You are on page 1of 10

CSE-2050 Assignment-3

Submitted to: Mr. Munish Kumar Submitted By: Gaurav Sharma RB6803A24 10800582

Q1: Does the Sequence <23,17,14,6,13,10,1,5,7,12> represents a Max Heap? Ans: 17 6 5 7 12 13 10 23 14 1

The above representation is the sequence when it has been made as a heap. We can see that in the second last line of the nodes, 6 has two elements 5&7. 7 >6, which is not the permissible property of a max heap. In a max heap, the middle element is always largest. Hence, the given sequence is not a max heap.

Q2: Which of the Operations on BST are more complex and why? Give suitable Example. Ans: The DELETION operation on a bst is the most complex operation. It is because the node to be removed may have further child nodes. As such, if we have to remove the node, we have to link the child nodes to the new parent node. It becomes even more complex when the removed node has two child nodes. This is the most complex operation on a node in a BST. For example: Removing the value 17 from the following BST: 10 7 1 8 14 24 17 25 30

Here, now we will find the minimum element in the right sub tree of the node 17 It is 24 in this case. We will replace 17 with 24. Only the values need to be replaced, and the nodes remain as they are. Afterwards, we will remove the Lower 24 node and place it instead of 17 as:

10 7 1 8 14 17 24 25 30

Q3: When we insert a new node in a binary search tree, will it become an internal node or terminal node? Give suitable example. Ans: when we insert a new node into a binary search tree, it always goes to the terminal node. We have to take the element to be inserted, and start from the root node, going downwards. The element is compared with the reference root. If the element >node, proceed to the right child of the node. Else, go to the left child of the node. Keep on going downwards till a suitable empty place for the insertion of the element is found. When we get a vacant place below any node, we can insert our element there. Example: Insert 8 in the following bst: 22 11 4 2 16 17 9 4 7 9 1

Here, 8<22. So we move to the left child, 11. 8<11, so we move further to its left child. 8>4. So now we move to its right child 16. 8<16, so we move to left side. Here we will insert the new element 8 as:

22 11 4 2 8 16 17 9 4 7 9 1

Q4: For a binary tree T, the Pre-Order and In-Order traversal sequences are as
Follows:Pre-Order: A B L M K N P Q In-Order: L B M A N K Q P Draw the Binary Tree. A M L B Q5: For a binary tree T, the Pre-Order and In-Order traversal sequences are as Follows:Pre-Order: A B E H Q R C D K L M In-Order: B Q R H E A D L K M C (a) What is the height of the tree? (b) What are the internal nodes? (c) What is its Post-Order traversal sequence? Ans: The binary tree for the given question will be as: K Q N P

A C H E Q L Height of this tree: 6 R B K M D

Internal nodes: these are the nodes which have at least one child element, and are not leaf nodes. Here there are 8 internal nodes. They are: A,B,C,E,D,H,K,Q. Post order: R Q H E B D L M K C A Q6: The Sequence <70,15,50,5,12,35,10> represents a Max Heap. Show the Following operations: (a) Insertion of element 25 (b) Deletion from Root (c) Insertion of element 75 Also give the number of exchanges performed n each case. Sol: a. Insertion of 25 element: Number of exchanges: 2

b. Deletion from root: Number of exchanges: 3

c. Insertion of 75: Number of exchanges:3

PS.: Sorry to paste pictures in the document, but it was really getting hectic to draw so many heaps and show the exchange process. Instead, I made them on paper, clicked the pictures and pasted them here. Thanks.

7. Suppose a binary tree is in memory:INFO H R P B E C F Q S A K L D LEFT 4 0 17 0 18 3 1 6 0 15 0 0 0 5 2 0 13 0 RIGHT 11 0 0 7 0 10 16 12 0 9 0 0 0

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18

Draw the Binary Tree. if the root of the tree is 14. ANS: A B D H P Q S E R C F K L

You might also like