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

Show that in a binary tree of N nodes. A binary tree of height 0 has one node. except the root node.1 H = N . 7. Every node has 2 outgoing pointers. Therefore true for h = 0.1 8. The total number of nodes in a binary tree equals N = F + H + L.1 nodes each by the induction hypothesis. 2 N = F + N . 2h+1 . These can have at most 2k+1 . It is also true that the total number of edges in a tree equals N .2 + 1 2(k+1)+1 . 2.1 pointer. Each node.1. Now consider a tree T of height k + 1.1 equals one for h = 0.6. and H = the number of nodes with one child (or half nodes). has one incoming pointer from its parent. Solution: Proof by induction. Thus. The root of T has a left sub tree and a right sub tree each of which has height at most k. This accounts for N . F = number of full nodes.1 . and each leaf is incident on no outgoing edge it follows that the total number of edges in a binary tree equals 2F +H.1 -2F Subbing this value of H into N = F + H + L gives. L = the number of leaves. Solution: Let N = the number of nodes. Therefore there are 2N pointers. for h = 1. Prove that the number of full nodes plus one is equal to the number of leaves in a nonempty binary tree.1. 2F + H = N . Inductive Hypothesis: Assume that the maximum number of nodes in a binary tree of height h is 2h+1 . Base Case: h = 0. :::. A full node is a node with two children. Adding the root node gives the maximum number of nodes in a binary tree of height k + 1. Show that the maximum number of nodes in a binary tree of height h is 2h+1 . The remaining N + 1 pointer are NULL pointers. Because each full node is incident on two outgoing edges. k. there are N + 1 NULL pointer. each half node is incident on one outgoing edge.2F + L N-N+1=-F+L F+1=L IT2201 | Data Structures and Algorithms .1. 2(2k+1 .1) + 1 2 * 2k+1 .

9. 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. Define internal path length. a link) to its INORDER successor. 12. IT2201 | Data Structures and Algorithms . Define Threaded Binary tree. 14. which makes use of stacks and consumes a lot of memory and time.  Listing a directory in an hierarchal file system (preorder)  Calculating the size of a directory (post order) 10. Traverse the left subtree Traverse the right subtree 16. 15. List the disadvantages of Binary search tree. It is the sum of the depths of all nodes in a tree. What are the tasks performed during inorder traversal? Traverse the left subtree Process the root node. The average depth is not O (log N) in trees which are not equally likely. 13. Absence of balanced search tree. Traverse the right subtree 17. What are the tasks performed during preorder traversal?         3  Process the root node. 11. What are the tasks performed during postorder traversal? Traverse the left subtree Traverse the right subtree Process the root node. By doing this threading we avoid the recursive method of traversing a Tree.    Deletions in a binary search tree leads to trees which are not equally likely. List the tree traversal applications. 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. 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).

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

--------------.. Show that for the perfect binary tree of height h containing 2h+1 ..1-(h+1) Proof The tree consists of 1 node at height h. 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) +…………….(2) 5 IT2201 | Data Structures and Algorithms .4h + 8 – 8h + 24 – 16h + 48 + …2h-1 (1) S = (2h+1 – 1) – (h+1) Which proves the theorem.24.+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 .(1) --------------.1 22 nodes at height h – 2 In general.1 nodes. the sum of heights of the nodes is 2h+1 . 2 nodes at height h .