Docsity

Implementing Binary Search Tree Program: A Lab Assignment in Data Structures, Assignments of Data Structures and Algorithms

Toggle sidebar
bg1
1
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:
pf3
pf4
pf5

Partial preview of the text

Download Implementing Binary Search Tree Program: A Lab Assignment in Data Structures and more Assignments Data Structures and Algorithms in PDF only on Docsity!

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:

  • the item is contained in a leaf node (i.e., it has no children),
  • the item is contained in a node that has one child or
  • the item is contained in a node that has two children. If the item to be deleted is contained in a leaf node, the node is deleted and the pointer in the parent node is set to null. If the item to be deleted is contained in a node with one child, the pointer in the parent node is set to point to the child node and the node containing the data item is deleted. This causes the child node to take the place of the deleted node in the tree. The last case is the most difficult. When a node with two children is deleted, another node in the tree must take its place. However, the pointer in the parent node cannot be assigned to point to one of the children
  • Set the pointer in the parent of the replacement node to point to the left child of the replacement node.
  • Set the pointer to the right subtree in the replacement node to point to the right subtree of the node to be deleted.
  • Delete the node to which the temporary pointer variable points. Write member function deleteNode, which takes as its arguments a pointer to the root node of the tree object and the value to be deleted. The function should locate in the tree the node containing the value to be deleted and use the algorithms discussed here to delete the node. The function should print a message that indicates whether the value is deleted. Modify the program of part (a) to use this function. After deleting an item, call the inorder, preorder and postorder traversal functions to confirm that the delete operation was performed correctly. (c) Write member function binaryTreeSearch, which attempts to locate a specified value in a binary search tree object. The function should take as arguments a pointer to the root node of the binary tree and a search key to be located. If the node containing the search key is found, the function should return a pointer to that node; otherwise, the function should return a null pointer. Modify the program of part (a) to use this function (d) In part (a) the program used three recursive methods (inorder, preorder and postorder) of traversing a binary tree of integers and of doubles. Here you are to create level-order traversal of the binary trees in part (a), in which the node values are printed level by level, starting at the root node level. The nodes on each level are printed from left to right. The level order traversal is not a recursive algorithm. It uses a queue object to control the output of the nodes. The algorithm is as follows: Insert the root node in the queue While there are nodes left in the queue, Get the next node in the queue Print the node's value If the pointer to the left child of the node is not null Insert the left child node in the queue If the pointer to the right child of the node is not null

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 :

  • Appropriate and adequate pseudo code/algorithm
  • Appropriate top-down design and style
  • Proper indentation and commenting
  • Proper naming of classes, functions, and variables etc
  • Correct function implementations (traversals, insertion, search etc)
  • Correct usage of binary search tree
  • Correct usage of class templates definitions
  • Overall presentation (well organized, clear, neat etc) 10 pts are reserved for demonstration:
  • Answers to questions
  • You get points is the program does exactly what it is supposed to do
  • ease of use,
  • type of user interface,
  • ability to handle various user input errors,
  • or any extra features that your program might have
  • You MUST demonstrate your working code (else your lab assignment will be given a zero) the end