Programming Assignment 5: Implementing Traversals and Destructor in Binary Search Tree, Assignments of Data Structures and Algorithms

A programming assignment for a csci 270 course, focusing on adding inorder, preorder, and postorder traversals to a binary search tree class. Students are required to implement the traversals recursively and create a destructor that performs an inorder traversal while deleting nodes. The bst.h and bst.cpp files as a starting point and includes a test driver for the class.

Typology: Assignments

Pre 2010

Uploaded on 08/18/2009

koofers-user-p3s
koofers-user-p3s 🇺🇸

1

(1)

9 documents

1 / 1

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
CSCI 270 Programming Assignment #5 Summer 2005
Due: Thursday, July 28
Emphasis: Binary Trees and Searching
In this assignment you will add inorder, preorder and postorder traversals to a binary search tree class.
All three implementations of accessing the binary tree will be implemented in a similar manner, simply
the order of output of a node and recursively visiting the two child nodes will differ. You should
implement the actual traversal as a recursive function that visits the nodes of the tree. You have been
given an implementation of a binary search tree, in BST.h and BST.cpp, as well as a test driver for the
class. There are places in the test driver commented out for you which indicates the correct prototype
of the function you should create for your traversals. For example, to invoke an inorder traversal, the
call looks something like:
intBST.inorder(cout);
That is to say, the public function to do an inorder traversal takes a single parameter, an ostream
variable, and displays the contents of the tree to the indicated output stream. You will need to define a
different, private method, to do the actual recursion and traversal of the nodes.
In addition to implementing the inorder, preorder and postorder traversals, you should also fill in the
destructor for the BST. The implementation of the destructor was left blank. Basically this was
because the destructor amounts to doing an inorder traversal of the binary tree, but instead of
displaying the items as they are visited inorder, you need to delete them. Using your implementation
of inorder traversal, correctly create a destructor to do an inorder traversal of the nodes, deleting the
nodes as they are visited.
Requirements
1) Use the BST.h and BST.cpp classes provides as your starting point
2) Add inorder, postorder and preorder methods to the class, prototypes should match those as
required in the BSTtester.cpp test driver.
3) Implement your traversal methods using recursion. You will need to define a private method to
perform the actual recursive traversal which will be called by the public interface method.
4) Also implement the destructor for the BST as an inorder traversal that causes the nodes in the tree
to be deleted during the inorder traversal.

Partial preview of the text

Download Programming Assignment 5: Implementing Traversals and Destructor in Binary Search Tree and more Assignments Data Structures and Algorithms in PDF only on Docsity!

CSCI 270 Programming Assignment #5 Summer 2005

Due: Thursday, July 28 Emphasis: Binary Trees and Searching

In this assignment you will add inorder, preorder and postorder traversals to a binary search tree class. All three implementations of accessing the binary tree will be implemented in a similar manner, simply the order of output of a node and recursively visiting the two child nodes will differ. You should implement the actual traversal as a recursive function that visits the nodes of the tree. You have been given an implementation of a binary search tree, in BST.h and BST.cpp, as well as a test driver for the class. There are places in the test driver commented out for you which indicates the correct prototype of the function you should create for your traversals. For example, to invoke an inorder traversal, the call looks something like:

intBST.inorder(cout);

That is to say, the public function to do an inorder traversal takes a single parameter, an ostream variable, and displays the contents of the tree to the indicated output stream. You will need to define a different, private method, to do the actual recursion and traversal of the nodes.

In addition to implementing the inorder, preorder and postorder traversals, you should also fill in the destructor for the BST. The implementation of the destructor was left blank. Basically this was because the destructor amounts to doing an inorder traversal of the binary tree, but instead of displaying the items as they are visited inorder, you need to delete them. Using your implementation of inorder traversal, correctly create a destructor to do an inorder traversal of the nodes, deleting the nodes as they are visited.

Requirements

  1. Use the BST.h and BST.cpp classes provides as your starting point
  2. Add inorder, postorder and preorder methods to the class, prototypes should match those as required in the BSTtester.cpp test driver.
  3. Implement your traversal methods using recursion. You will need to define a private method to perform the actual recursive traversal which will be called by the public interface method.
  4. Also implement the destructor for the BST as an inorder traversal that causes the nodes in the tree to be deleted during the inorder traversal.