You are on page 1of 5

UNIT II 1. Define Tree.

TREE STRUCTURES

A tree is a data structure, which represents hierarchical relationship between individual data item. A tree is a finite set of one or more nodes such that o There is a specially designated node called root. o The remaining nodes are partitioned into n>= 0 disjoint sets a, b, c, d. n, where a, b, c, d are called the sub trees of the root

A tree is a non-linear; two dimensional data structure in which items are arranged in a sorted sequence and it represents hierarchical relationship between individual data items.

2. List the applications of trees. Binary search trees Expression trees Threaded binary trees

3. Explain why binary search cannot be performed on a linked list? Binary search tree is a non-linear data structure whereas linked list is a linear data structure, which permits only sequential search. 4. State the properties of a binary tree. The maximum number of nodes on level n of the binary tree is 2n-1 where n>=1 The maximum number of nodes in a binary tree of height n is 2n-1 where n>=1 For any non empty tree nl =nd+1 where nl is the number of leaf nodes and nd is the number of nodes of degree 2. 1 5. What are the different ways of representing a binary tree? Linear representation using arrays Linked representation using pointers

IT2201 | Data Structures and Algorithms

6. Show that in a binary tree of N nodes, there are N + 1 NULL pointer. Every node has 2 outgoing pointers. Therefore there are 2N pointers. Each node, except the root node, has one incoming pointer from its parent. This accounts for N - 1 pointer. The remaining N + 1 pointer are NULL pointers. 7. Show that the maximum number of nodes in a binary tree of height h is 2h+1 - 1. Solution: Proof by induction. Base Case: h = 0. A binary tree of height 0 has one node. 2h+1 - 1 equals one for h = 0. Therefore true for h = 0. Inductive Hypothesis: Assume that the maximum number of nodes in a binary tree of height h is 2h+1 - 1, for h = 1; 2; :::; k. Now consider a tree T of height k + 1. The root of T has a left sub tree and a right sub tree each of which has height at most k. These can have at most 2k+1 - 1 nodes each by the induction hypothesis. Adding the root node gives the maximum number of nodes in a binary tree of height k + 1,

2(2k+1 - 1) + 1 2 * 2k+1 - 2 + 1 2(k+1)+1 - 1


8. A full node is a node with two children. Prove that the number of full nodes plus one is equal to the number of leaves in a nonempty binary tree. Solution: Let N = the number of nodes, F = number of full nodes, L = the number of leaves, and H = the number of nodes with one child (or half nodes). The total number of nodes in a binary tree equals N = F + H + L. Because each full node is incident on two outgoing edges, each half node is incident on one outgoing edge, and each leaf is incident on no outgoing edge it follows that the total number of edges in a binary tree equals 2F +H. It is also true that the total number of edges in a tree equals N - 1. Thus, 2F + H = N - 1 H = N - 1 -2F Subbing this value of H into N = F + H + L gives, 2 N = F + N - 1 - 2F + L N-N+1=-F+L F+1=L
IT2201 | Data Structures and Algorithms

9. List the tree traversal applications. Listing a directory in an hierarchal file system (preorder) Calculating the size of a directory (post order)

10. Define Threaded Binary tree. A Threaded Binary Tree is a binary tree in which every node that does not have a right child has a THREAD (in actual sense, a link) to its INORDER successor. By doing this threading we avoid the recursive method of traversing a Tree, which makes use of stacks and consumes a lot of memory and time. 11. Define internal path length. It is the sum of the depths of all nodes in a tree. 12. What is the average depth of all nodes in an equally likely tree? The average depth of all nodes in an equally likely tree is O (log N). 13. List the disadvantages of Binary search tree. Deletions in a binary search tree leads to trees which are not equally likely. Absence of balanced search tree. The average depth is not O (log N) in trees which are not equally likely.

14. What is meant by binary tree traversal?


Traversing a binary tree means moving through all the nodes in the binary tree visiting each node in the tree only once.

15. What are the tasks performed during preorder traversal? 3 Process the root node. Traverse the left subtree Traverse the right subtree

16. What are the tasks performed during inorder traversal? Traverse the left subtree Process the root node. Traverse the right subtree

17. What are the tasks performed during postorder traversal? Traverse the left subtree Traverse the right subtree Process the root node.

IT2201 | Data Structures and Algorithms

18. Define expression tree? Expression tree is also a binary tree in which the leafs terminal nodes or operands and nonterminal intermediate nodes are operators used for traversal. 19. State the construction of expression trees? 1. Convert the given infix expression into postfix notation 2. Create a stack and read each character of the expression and push into the stack, if operands are encountered. 3. When an operator is encountered pop 2 values from the stack. 4. From a tree using the operator. 20. Define lazy deletion? When an element is to be deleted it is left in the tree itself and marked a s being deleted. This is called as lazy deletion and is an efficient procedure if duplicate keys are present in the binary search tree, because the field that keeps count of the frequency of appearance of the element can be decremented of the element can be decremented. 21. State the merits and demerits of linear representation of binary trees. The merits: Storage method is easy and can be easily implemented in arrays. When the location of a parent /child node is known other one can be determined easily. It requires static memory allocation so it is easily implemented in all programming language The demerits: Insertions and deletions in a node, taker an excessive amount of processing time due to data movement up and down the array. 22. State the merits and demerits of linked representation of a binary tree. The merits: Insertions and deletions in a node, involves no data movement except the re arrangement of pointers, hence less processing time. The demerits: Given a node structure, it is difficult to determine its parent node. Memory spaces are wasted for storing null pointers for the nodes, which have one or no subtrees. It requires dynamic memory allocation, which is not possible in some programming languages. 23. What is the purpose of a left child node and a right child node? 4 The left child node has a key that is less than the key of its parent node. The right child node has a key that is greater than the key of its parent node

IT2201 | Data Structures and Algorithms

24. Show that for the perfect binary tree of height h containing 2h+1 - 1 nodes, the sum of heights of the nodes is 2h+1 - 1-(h+1) Proof The tree consists of 1 node at height h, 2 nodes at height h - 1 22 nodes at height h 2 In general, 2i nodes at height h i The sum of the heights of all the nodes is S= S = h + 2(h-1) + 4(h-2) +..+2h 1 (1) 2S = 2h + 4(h-1) + 8(h-2) +..+2h(1) Subtract equation 1 from equation 2 2S S = 2h + 4h 4 + 8h 16 + 16h 48 + 2h (1) h 2h + 2 - 4h + 8 8h + 24 16h + 48 + 2h-1 (1) S = (2h+1 1) (h+1) Which proves the theorem. --------------- (1) --------------- (2)

IT2201 | Data Structures and Algorithms

You might also like