Sei sulla pagina 1di 6

DSA ASSIGNMENT

Sethu Janaki VM 1. Define binary tree. A binary tree is a tree in which no nodes can have more than two subtrees. These subtrees are called left subtree and right subtree respectively. Each subtree are binary trees themselves. Each element of a binary tree is called node of tree. 2. Applications of binary tree. 1. Binary tree is used to represent expressions are called expression trees. 2. Binary tree is used to find duplicate numbers. 3. Binary tree is used for sorting elements. 4. Binary tree is used in binary search. 3. How to construct expression tree.
Read one Symbol at a time from postfix expression. Check whether the symbol is operand or operator . If the symbol is operand ,create one node tree and push pointer to the stack. If it is operator pop two pointers from the stack and form a new tree with root as operator and pointer to new node is pushed on to the stack. 4. Representation of binary tree. i. Array #define NUMNODES 500 struct nodetype { int info; int left; int right; int father; }; struct nodetype node [NUMNODES]; ii. Linked list struct nodetype { int info; struct nodetype *left; struct nodetype *right; sruct nodetype *father; };

5. Function to insert element in a binary tree .

insert_node (struct node * tmp, struct node * tree) { if (tmp-> value > tree-> value) { if ( tree->right==NULL) tree->right=tmp; else insert_node(tmp, tree->right) } else { if (tree->left==NULL) tree->left=tmp; else insert_node(tmp,tree->left) } } 6. Define tree traversal. Name the 3 types of tree traversals. Traversing means visiting each node only once. Tree traversal is a method of visiting all the nodes in the tree exactly once. 1. In Order traversal. 2. Pre Order traversal. 3. Post Order traversal. 7. What is complete binary tree . A binary tree of depth d is a complete binary tree if : 1. If all nodes at levels less than d-1 has two sons. 2. All the nodes at the last level as far left as possible. 8. Give steps of preorder traverse. 1. Visit the root. 2. Traverse the left subtree in preorder. 3. Traverse the right subtree in preorder. 9. Give steps of postorder traverse. 1. Traverse the left subtree in postorder. 2. Traverse the right subtree in postorder. 3. Visit the root. 10. Give steps of inorder or symmetric order traverse.

1. Traverse the left subtree in postorder. 2. Visit the root. 3. Traverse the right subtree in postorder. 11. How many different trees are possible with 10 nodes.

Answer: 1014 Explanation: For example, consider a tree with 3 nodes(n=3), it will have the maximum combination of 5 different (ie, 23 - 3 = 5) trees.

ii

iii

iv

In general: If there are n nodes, there exist 2n-n different trees. 12. In tree construction which is the suitable efficient data structure? (a ) Array (b) Linked list (c) Stack (d) Queue Answer: (b) Linked list

13. Function for preorder traverse. Void pretrav ( struct node * tree) { if (tree!=NULL){ printf("%d\n",tree->info); pretrav(tree->left); pretrav(tree->right); } } 14. Function for inorder traverse.

Void intrav ( struct node * tree) { if (tree!=NULL){ intrav(tree->left); printf("%d\n",tree->info); intrav(tree->right); } } 15. Function for posttorder traverse. Void posttrav ( struct node * tree) { if (tree!=NULL){ posttrav(tree->left); posttrav(tree->right); printf("%d\n",tree->info); } } 16. Huffman tree for following data. symbol Frequency

A B C D E

17 9 8 5 3

A B C D E

17 9 8 5 3

17 (A) 9 (B) 8 (C) 8(D,E)

17 (A) 16 (C,D,E) 9 (B)

25(B,C,D,E) 17 (A)

42(A,B,C,D,E)

42 (A,B,C,D, E) 17(A) 8(C) 17(C,D, 25(B,C,D ,E) E) 8(D,E) 5(D) 9(B) 3(E)

17. Traverse the following binary tree in different order.

A B C F G D H

E I

Answer: (a) preorder (b) Inorder (c) postorder

: ABCEIFJDGHKL : EICFJBGDKHLA : IEJFCGKLHDBA

18. Draw binary tree for the expression 5+3*(2-7)/9+4

+ + 3 * 4 6 7 / 5 3

19. Number of nodes for tree with n leaves. Answer: 2n-1 20. Of the following tree structure, which is efficient considering space and time complexities? (a) Incomplete Binary Tree (b) Complete Binary Tree (c) Full Binary Tree Answer: (b) Complete Binary Tree. Explanation: Full binary tree loses its nature when operations of insertions and deletions are done. For incomplete binary trees, extra storage is required and overhead of NULL node checking takes place. So complete binary tree is the better one since the property of complete binary tree is maintained even after operations like additions and deletions are done on it.

Potrebbero piacerti anche