



Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!




Lab Assignment: Implementing the Binary Search Tree Program Lab Objectives In this lab assignment, you will: Design and implement a binary search tree Use class templates definitions Allocate and de-allocate memory at runtime Manipulate links in a binary search tree Insert items into a binary search tree Delete items from a binary search tree Traverse the binary search tree Compile and execute the program. Compare your output with the sample output provided. NB: USE CLASS-TEMPLATE DEFINITIONS submit your lab assignment for assessment on Moodle. submit as a zipped folder (CODE) (a) Write a program that creates a binary search tree and traverses it (walks through all its nodes) three ways using recursive inorder, preorder and postorder traversals. A node can only be inserted as a leaf node in a binary search tree. If the tree is empty, a new node is created, initialized and inserted in the tree. If the tree is not empty, the program compares the value to be inserted with the data value in the root node. If the insert value is smaller, the program recursively calls the appropriate function to insert the value in the left sub tree. If the insert value is larger, the program recursively calls the appropriate function to insert the value in the right sub tree. If the value to be inserted is identical to the data value in the root node, the program prints the message “duplicate" and returns without inserting the duplicate value into the tree. Each of the member functions inorder, preorder and postorder traverses the tree and prints the node values as shown below. Sample Output: Enter 10 integer values:
Preorder traversal 50 25 12 6 13 33 75 67 68 88 Inorder traversal 6 12 13 25 33 50 67 68 75 88 Postorder traversal 6 13 12 33 25 68 67 88 75 50 Enter 10 double values: 39.2 16.5 82.7 3.3 65.2 90.8 1.1 4.4 89.5 92. Preorder traversal 39.2 16.5 3.3 1.1 4.4 82.7 65.2 90.8 89.5 92. Inorder traversal 1.1 3.3 4.4 16.5 39.2 65.2 82.7 89.5 90.8 92. Postorder traversal 1.1 4.4 3.3 16.5 65.2 89.5 92.5 90.8 82.7 39. Each of the member functions calls its own separate recursive utility function to perform the appropriate operations on the internal representation of the tree. (b) This is a continuation of part (a) here you are to create a function for deleting items from binary search trees in part (a). The deletion algorithm is not as straightforward. There are three cases that are encountered when deleting an item:
Insert the right child node in the queue Write member function level order to perform a level-order traversal of a binary tree object. This function must use the program in part (a). Note: You will also need to incorporate the queue-processing functions in this program. (e)Write a function depth that receives a binary tree in part (a) and determines how many levels it has. NOTE: Combine all the functions and methods in this lab assignment (parts a, b, c, d and e) into a complete working program with appropriate menus. Experiment with several sample runs of the complete program. Assessment This lab assignment will be graded out of 2 0pts : 10 pts are reserved for :