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

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.






                  (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";



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);


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.

Posted Date: 4/2/2013 3:50:13 AM | Location : United States

Related Discussions:- Recursive and iterative handling of a binary search tree, Assignment Help, Ask Question on Recursive and iterative handling of a binary search tree, Get Answer, Expert's Help, Recursive and iterative handling of a binary search tree Discussions

Write discussion on Recursive and iterative handling of a binary search tree
Your posts are moderated
Related Questions
Merge sort: Merge sort is a sorting algorithm that uses the idea of split and conquers. This algorithm splits the array into two halves, sorts them separately and then merges t

Following are some of the drawback of sequential file organisation: Updates are not simply accommodated. By definition, random access is impossible. All records should be

A freight train from Melbourne is approaching Sydney, carrying n cars of cargos. The cargos are to be delivered to n different cities in the metropolitan area of Sydney - one car f

A shop sells books, magazines and maps. Every item is identified by a unique 4 - digit code. All books have a code which starts with 1, all maps have a code starting with 2 and all

Stacks are often used in evaluation of arithmetic expressions. An arithmetic expression contains operands & operators. Polish notations are evaluated through stacks. Conversions of

Explain the theory of computational complexity A  problem's  intractability  remains  the  similar  for  all  principal  models  of   computations    and   all reasonable inpu

Preorder traversal of a binary tree struct NODE { struct NODE *left; int value;     /* can take any data type */ struct NODE *right; };   preorder(struct N

Efficiency of Linear Search How much number of comparisons is there in this search in searching for a particular element? The number of comparisons based upon where the reco

Decision Tree A decision tree is a diagram that shows conditions and actions sequentially and therefore shows which condition is to be considered first, second and so on. It is

File organisation might be described as a method of storing records in file. Also, the subsequent implications approaching these records can be accessed. Given are the factors invo