Download as pdf
Download as pdf
You are on page 1of 14
{7:7 BINARY SEARCH TRE Binary tree T is said to be binary search tree if each node N of T has the following properties : 1. The value of N is greater than every value in the left subtree of N. 2, The value of Nis less than every value in the right subtree of N. In other words, a binary tree in which the left child, if any, of each contains a smaller value and the right child, if any, contains a large value than the parent node.. For example, Fig. 7.24 shows a binary search tree. : Fig. 7.24: A binary search tree with 10 nodes. Notice that, for any given node, the node to its left contains smaller value and its right contains larger value. nodet? rees ADVANTAGES cs Thegreat advantage of this Search tree athe root will either find the element grin ure ay ne oranelement,acomparison icate whi pROPERTIES OF BINARY SEARCH tee Which one subtree to search. Following are the properties of bin, s lary search tree ‘he inorder tr: i Bee creadingerae oat Search tree shows that values stored in this tree inFig 724% For example, the inorder traversal of binary search tree 1, 2, 3, 4, 5, 6, 7,8 9, 10 ie, element comes in an increasing order. 2. Smallest element in binary search tree is the left most element and largest element of binary search tree is the right most element in the binary search tree. 18: OPERATION ON BINARY SEARCH TREE _ —~Cenarally follouine nw Generally, following operations are performed on binary search tree. 1 Insertion 2. Deletion 3. Searching LINSERTION To insert a node in binary search tree, we must first check whether the tree already ‘ntains any nodes. If tree is empty, the node is placed in the root node. If the tree is not ‘npty, then the proper location is found and new node becomes either a left or a right child ofan existing node. . Consider the following binary search tree T and suppose 33 is added to the tree T. 5 pinary Search Tree Before Insertion, 25: Fig. 7 306 Eagle’s Data Structure Using ¢ First, compare the 33 with root ie. 60. Since 33 < 60, move to the left chilg of oo, which is 30., Compare 33 with 30. Since 33 > 30, move to the right child of 30 ie. s¢ Compare 33 with 55, Since 33 <55, move tothe left child of 55, whichis 35, Now, compar 33 and 35, Since, 33 <35 and 35 has no left child, so we add 33 as left child of node 35 So, new binary search tree is : Fig. 7.26 : Binary Search Tree after insertion. -- ALGORITHM : INS_BST (ROOT, ITEM) Here, ROOT is a pointer variable that contains address of root node and ITEMis value to be inserted in binary search tree. This is a recursive algorithm. Step! If(ROOT== Step2 Set PTR:= ROOT Step3 -If(TEM Data Step 4. Preorder (PTR —> child) Step5. Preorder (PTR —> Rchild) Step6. End. Q.5, Construct a binary tree whose nodes are as under : Preorder: ABCD FHJMKEGILN Postorder: AD JMHKFCINLGEB Ans, Recheck Result : Preorder: AB CD FHJMKEGILN Ostorder : AD JMHKF CINLGEB 316 Eagle’s Data Structure Using Q. 6. Construct a binary tree whose nodes are as under : Preorder: ABDGHCEFIKJ Inorder: BGHDAECIKFJ Ans. os \ io Age NS De Recheck Result : Preorder: ABD GHCEFIKJ Inorder: BGHDAECIKFJ ======m OBJECTIVE TYPE QUESTIONS Be=———= data structure. tree can have at the most two children. 3. ‘Two nodes are known as .. ... if they have the same parents. ou Ancemunctancde mparenenedegea node. 5. Terminal node is the node that has .. degree. 6. Binary tree is.............. in nature. ... is the maximum level of any node in a tree. 8. Siblings are the children of .. parent. 9. A tree is composed of a collection of .. 10. BST stands for ... 1. Ab . Tefers to the process of visiting each node in a tree data structure. 12. , the contents of a root node are printed first. 13. The maximum leyel of any leaf in the tree is known as the .......... of the tree. 14, Ina complete binary tree, the out degree of every node is exactly equal to 2 or 0-(" crf) 15. Two nodes are .. . if they are left and Tight children of the same node. 16. The height of a iar tree is the maximum level of its tree. cy 17. A binary tree consists of nodes which have exactly two nodes. ee | 18. ee number of nodes at each level in complete binary tree is 1 less than som? oa) ol 19. The preorder traversal of binary tree begins with processing of the ... ac / grees 317 Answers 1. ae 2. Binary 3. siblings 4. Depth 5. zero 6. hierarchical eee 8. same 9. nodes 10. st earch Tree 11. Traverse 12. preorder 13. dept! 14. True 15. siblings 16. False 17. False 18. True 19. root SHORT ANSWER TYPE QUESTIONS Why trees structure is known as non-linear data structure ? What do you mean by similar binary tree and copies of binary tree ? Define the following terms : ere (i) Root (ii) Siblings (ii) Ancestor (iy) Descendents (») Level (vi) Path (vii) Branch (viii) Height (ix) Leaf node. Write short notes on the following : (a) Full binary tree (b) Complete binary tree (¢) Extended binary tree. = LONG ANSWER TYPE QUESTIONS g==——=—= 1. What do you mean by a binary tree ? How they are represented and implemented in memory ? Explain with suitable examples. 2. Explain the three standard ways of traversing a binary tree in detail with suitable examples. 4. Write and explain the preorder tree traversal algorithm. 4 Write and explain the inorder tree traversal algorithm. © White and explain postorder tree traversal algorithm. i ‘ f do you mean by binary search tree ? Explain the operations associated with binary >, tarch tree, | What distinguish a binary tree from other dynamic data structure such as linked stack, 4, iked queue and linked list ? ; ; i 4 . do you mean by sequential representation and linked representation of binary , “Sesin memory ? Explain. =

You might also like