Recursive and iterative handling of a binary search tree, Data Structure & Algorithms

Assignment Help:

This section prescribes additional exercise with the recursive and iterative handling of a binary search tree.

Adding to the Binary Search Tree Recursively

Add implementations to recursively add to the binary search tree. The public addRecursively method should examine the tree's root, placing the new addition there if the root is zero; otherwise,addRecursively will call the private method addRecursively, passing it the non-zero root of the tree. The recursive method will compare the new addition's data to that located at the current root. If the data is less and the subsequent left pointer is zero, the new addition can be stored there; otherwise, the function calls itself recursively, passing the non-zero left pointer to itself. Data in the new addition greater than that in the current root is handled similarly with the right pointer.

Displaying the Binary Search Tree Iteratively

Add the implementation to iteratively write the binary search tree. The iteration will move down the tree, following successive left pointers. The first node with zero for a left pointer may be written, as there can be no data which comes before it. The iteration which moves down the tree will need to stack each current pointer so that it may unwind the downward traversal of the tree. Use theTemplateNode, TemplateList and TemplateStack implementations from the previous homework for this purpose. Starting with a current pointer initialized to the root of the tree, replace the current pointer with each non-zero left pointer encountered; push current on the stack before each replacement. When a left pointer of zero is encountered, display that node's data and move to the right. When a right pointer of zero is assigned to current, pop to move back up the tree, write a node, then move right again. Become familiar with this behavior in a diagram before coding.

Destroying the Binary Search Tree

Implement a recursive erase method. Add the following prototypes to the tree's class definition.

public:

voideraseRecursively

                  (void);

private:

voideraseRecursively

                  (node* currentRoot);

The bodies of these methods will be identical in form to those for writing recursively.

Add the following code to the else clause in the main function. Note the use of the recursive erase and add methods and the iterative write.

cout<< "Press to continue...\n";

cin.get();

customerTree.eraseRecursively();

cout<< "Recursive Tree Listing After Erase:" <

infile.clear();  // restore stream state so I/O may proceed

infile.seekg (0);  // seek "get" to file start (byte #0)

while (!infile.eof())

customerTree.addRecursively (new node(infile));  // recursive add

cout<< "Iterative Listing of Recursive Additions\n";

customerTree.writeIteratively (cout);

infile.close();

Note that one of the erase methods could be called by the destructor to perform its function as well.

Test the algorithms thoroughly by modifying the data file several times.


Related Discussions:- Recursive and iterative handling of a binary search tree

Asymptotic analysis, Asymptotic Analysis Asymptotic analysis is dependi...

Asymptotic Analysis Asymptotic analysis is depending on the idea that as the problem size grows, the complexity can be defined as a simple proportionality to some known functio

C++, #What is the pointer

#What is the pointer

Explain how the shuttle sort algorithm works, Question 1 Explain how th...

Question 1 Explain how the shuttle sort algorithm works by making use of the following list of integers:11, 4, 2, 8, 5, 33, 7, 3, 1, 6. Show all the steps. Question 2

Algorithm for sorting a deck of cards, What is wrong with the following alg...

What is wrong with the following algorithm for sorting a deck of cards (considering the basic properties of algorithms)? I. Put the cards together into a pile II. For each ca

Algorithm, what algorithms can i use for the above title in my project desi...

what algorithms can i use for the above title in my project desing and implmentation of road transport booking system

Lists, In the previous unit, we have discussed arrays. Arrays are data stru...

In the previous unit, we have discussed arrays. Arrays are data structures of fixed size. Insertion and deletion involves reshuffling of array elements. Thus, array manipulation

How will you represent a max-heap sequentially, How will you represent a ma...

How will you represent a max-heap sequentially? Max heap, also known as the descending heap, of size n is an almost complete binary tree of n nodes such that the content of eve

Functions for inserting and deleting at either end of deque, Q. Devise a re...

Q. Devise a representation for a given list where insertions and deletions can be made at both the ends. Such a structure is called Deque (which means Double ended queue). Write fu

Graph traversal schemes, Various graph traversal schemes Graph Traversa...

Various graph traversal schemes Graph Traversal Scheme. In many problems we wish to investigate all the vertices in a graph in some systematic order. In graph we often do no

Write Your Message!

Captcha
Free Assignment Quote

Assured A++ Grade

Get guaranteed satisfaction & time on delivery in every assignment order you paid with us! We ensure premium quality solution document along with free turntin report!

All rights reserved! Copyrights ©2019-2020 ExpertsMind IT Educational Pvt Ltd