Quantcast
Channel: Programming Forums
Viewing all articles
Browse latest Browse all 51036

Binary Search Tree and Recursion Not Displaying Properly

$
0
0
Hi Everyone,

As I mentioned in a previously posted of mine, I am working/studying subjects for a class I will have to take soon. It's an Advanced Data Structure and Algorithm class.

Presently, I am working on a little project using Binary Tree and Recursion however I have some questions/issues that I am still not able to solve or figure it out.

My code follows, and thereafter my questions/issues:

#include <iostream>
#include <fstream>
#include <algorithm>
#include <string>

using namespace std;  

const string IN_FILENAME = "FILE.TXT";    // constant input file name 

struct node
{
  int    key_value;
  node*  left;
  node*  right;
};

// function prototypes 
void  ReadInFile(ifstream&, node*);       // read integers from input file into the tree
//void insert(int key, node** leaf);      // storing a list of integers into a binary tree
node* insert(int& key, node* leaf);       // storing a list of integers into a binary tree
void print_in_order(node*);               // display in traversal order the integers in the tree to the screen

//**********************************************************************
int main(int argc, char* argv[])
{
	ifstream inFile;       // to handle the input file
	int count = -1;        // for counting
	bool result = true;    // to control the loop	
	string file = " ";	   // to handle the filename
	node* root = new node; // create a tree       
	
	do 
	{	
		cout << "Please enter a filename: ";
		getline(cin, file);	
		transform( file.begin(), file.end(), file.begin(), toupper );	
		
		if ( file == IN_FILENAME.c_str() )
        { 
			inFile.open( IN_FILENAME.c_str() );
			ReadInFile( inFile, root );
			result = false;
		}
		else
		{
			cout << "The filename entered does not exist!" << endl;
			cout << "Please type it again." << endl << endl;
			result = true;
		}
 
	} while ( result );
	
    cout << endl << endl;
	system("PAUSE");  
    return 0;
}

//**************************************************************************
void ReadInFile(ifstream& inFile, node* node) 
{
	int number = 0;  
	int newNumber = 0;			
	
	node->key_value = 0;  
	node->left =      NULL; 
	node->right =     NULL; 
	cout << endl;
     
        // input file: 49 83 91 17 62
	
        while ( inFile )
	{
		inFile >> number;
		node->key_value = number;  
		insert( number, node );    
	}

	print_in_order( node );	
	
	inFile.close();		
}

//**************************************************************************
node* insert(int& key, node* leaf)
{
	if( key < leaf->key_value )
    {
		if( leaf->left != NULL )
			insert( key, leaf->left );
		
		else
		{
		  leaf->left = new node;
		  leaf->left->key_value = key;
		  leaf->left->left = NULL;       
		  leaf->left->right = NULL;      
		}  
    }
    
	else if( key >= leaf->key_value )
    {
		if( leaf->right != NULL )
			insert( key, leaf->right );
		
		else
		{
		  leaf->right = new node;
		  leaf->right->key_value = key;  
		  leaf->right->left = NULL;      
		  leaf->right->right = NULL;     
		}
    }
	
	return leaf;
}

//**************************************************************************
void print_in_order(node* p) 
{
    if( p != NULL )
    {
        if( p->left ) 
			print_in_order( p->left );
        
		cout<< " " << p->key_value << " ";
        
		if( p->right ) 
			print_in_order( p->right );
    }

	else 
		return;	
}



//*******************************************************************
Integer in the Input File:

49 83 91 17 62


Output So Far:

Please enter a filename: file.txt

62 17 49 62 62 83 91

Press any key to continue . . .


Questions:

1)Why my print_in_order function is printing extras 62?

2) It's the order being used in the code and then displaying using recursive "in-order" tree?

3) What number should be printed/displaying when using "in-order", 17 62 91 83 49 ?

4) Does my insert, and print_in_order function make sense?

5) Any mistakes/errors I am not seeing it.

My code compile but I am not sure if it's behaving the way I expected. I know that it should not display extras 62 but am not sure what is the order to be displayed when using "in order" based on the integers from my input file.

I attached the input file just in case.

Big Thanks in advance.

Marco

Viewing all articles
Browse latest Browse all 51036

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>