472,781 Members | 1,278 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,781 software developers and data experts.

Binary Search Tree Input Problem

I have everything pretty much done but I need to fix something in my
coding. I want to be able to enter strings such as "love", "hate",
"the", etc. but am unable to figure how to do this. I have put my .cpp
and my .h code below. Please help and thank you very much.

// include files
#ifndef BINTREE_H
#define BINTREE_H
#include <iostream>
#include <iomanip>
#include <cstdlib>
#include <ctime>
using namespace std;

// data types
typedef char DataType;

// TreeNode struct (includes constructor)
struct TreeNode
{
DataType data;
TreeNode *left, *right;

// constructor
TreeNode( DataType d = 0, TreeNode * l = NULL, TreeNode * r = NULL
) :
data( d ), left( l ), right( r ) { }
};

// ------------------------------------------------------------------
// BinarySearchTree class
// ------------------------------------------------------------------
class BinarySearchTree
{
private:

// private data members
TreeNode* root;
int level; // class variable to help format printing

// private member functions
void FreeTree( TreeNode* & );
TreeNode* Search( DataType, TreeNode* ) const;
void Insert( DataType, TreeNode* & );
void Delete( DataType, TreeNode* & );
void Print( TreeNode* );
void PrintInorder( TreeNode* ) const;

public:

// public member functions

// constructor
BinarySearchTree( ) : root( NULL ) { }

// call recursive insert method
void Insert( DataType item ) { Insert( item, root ); }

// print the tree on its side, showing structure
void Print( ) { level = 0; Print( root ); }

// call recursive print method
void PrintInorder( ) const { PrintInorder( root ); }
};

#endif
// include files
#include <iostream>
#include <iomanip>
#include <cstdlib>
#include <ctime>
#include "bst.h"
using namespace std;

// Recursively insert an item in the tree, if not already present.
// Note that we pass root by reference.
void BinarySearchTree::Insert( DataType item, TreeNode* & root )
{
if ( root == NULL ) // hit NULL pointer, insert here
{
root = new TreeNode( item );
if ( root == NULL )
cerr << "Error: cannot allocate memory for TreeNode in
Insert()!\n";
}

else if ( item < root->data ) // insert in left subtree
Insert( item, root->left );

else if ( item > root->data ) // insert in right subtree
Insert( item, root->right );
}

// Recursive inorder tree traversal (called by Print function).
// Print the tree on its side, rotated counterclockwise to show
structure.
void BinarySearchTree::Print( TreeNode* nodeptr )
{
int i;

if ( nodeptr )
{
level++;

for ( i = 0; i < level; i++ )
cout << setw( 3 ) << ' ';
Print( nodeptr->right );

cout << '\r';
for ( i = 0; i < level - 1; i++ )
cout << setw( 3 ) << ' ';
cout << nodeptr->data << endl;

for ( i = 0; i < level; i++ )
cout << setw( 3 ) << ' ';
Print( nodeptr->left );

level--;
}
}

// Recursive tree traversals (called by Print routines).
// Print tree items inorder.
void BinarySearchTree::PrintInorder( TreeNode* nodeptr ) const
{
if ( nodeptr != NULL )
{
PrintInorder( nodeptr->left ); // print left subtree
cout << nodeptr->data << ' '; // print root
PrintInorder( nodeptr->right ); // print right subtree
}
}

// Program to test BinarySearchTree class: main() function
int main( )
{
BinarySearchTree tree;
DataType item;
int n = 20;
char answer;
cout<<"Would you like to enter the numbers or have the system
generate";
cout<<"them randomly. R for random and U for user input."<<endl;
cin>>answer;
if(answer == 'R' || answer =='r')
{
// generate a BST of random int values
srand( time( NULL ) ); // init random number generator
cout << "Inserting " << n << " items in the BST:";
for ( int i = 0; i < n; i++ )
{
item = rand() % n; // generate a random int between 0 and n-1
tree.Insert( item ); // insert it in the BST
if ( i % 10 == 0 ) cout << endl;
cout << setw( 7 ) << item;
}
}
else if(answer == 'U' || answer =='u')
{
cout<<"Enter data with spaces in between. # terminates the
input"<<endl;
do
{
cin>>item;
if(item != '#')
tree.Insert(item);
} while(item != '#');
}
else
cout<<"Wrong input."<<endl;

// print the tree
cout <<endl;
cout <<"Duplicates will not be shown"<<endl;
cout << "\n\nBST contents:\n";
tree.PrintInorder( );

cout << "\n\nBST structure:\n";
tree.Print( );
cout<<endl;

cout<<"Goodbye"<<endl;

return EXIT_SUCCESS;
}
again thanks for the help
Jul 22 '05 #1
4 2771
On 26 Jul 2004 18:08:19 -0700, Henry Jordon <bu*******@hotmail.com> wrote:
I have everything pretty much done but I need to fix something in my
coding. I want to be able to enter strings such as "love", "hate",
"the", etc. but am unable to figure how to do this. I have put my .cpp
and my .h code below. Please help and thank you very much.


It seems very unlikely that you could write all that code and yet not do
this seemingly simple task. What exactly is confusing you? What did you
attempt?

Here's a start

Add this.

#include <string>

Replace this

typedef char DataType;

with this.

typedef std::string DataType;

I think you'll have one or two more changes to make, but if you have any
understanding at all of the code you are working with I don't think you'll
find it too difficult.

john
Jul 22 '05 #2
In message <opsbr7wzkp212331@andronicus>, John Harrison
<jo*************@hotmail.com> writes
On 26 Jul 2004 18:08:19 -0700, Henry Jordon <bu*******@hotmail.com> wrote:
I have everything pretty much done but I need to fix something in my
coding. I want to be able to enter strings such as "love", "hate",
"the", etc. but am unable to figure how to do this. I have put my .cpp
and my .h code below. Please help and thank you very much.


It seems very unlikely that you could write all that code and yet not
do this seemingly simple task.


A possible explanation of your confusion may be found here:

http://www.hpcnet.org/upload/directo...31027093651.cc

;-)

--
Richard Herring
Jul 22 '05 #3

On 26 Jul 2004 18:08:19 -0700, bu*******@hotmail.com (Henry Jordon)
wrote:
I have everything pretty much done but I need to fix something in my
coding. I want to be able to enter strings such as "love", "hate",
"the", etc. but am unable to figure how to do this. I have put my .cpp
and my .h code below. Please help and thank you very much.

// include files
#ifndef BINTREE_H
#define BINTREE_H
#include <iostream>
#include <iomanip>
#include <cstdlib>
#include <ctime>
using namespace std;

// data types
typedef char DataType;

// TreeNode struct (includes constructor)
struct TreeNode
{
DataType data;
TreeNode *left, *right;

// constructor
TreeNode( DataType d = 0, TreeNode * l = NULL, TreeNode * r = NULL You have a semi-colon here. It should be a colon or whatever you call
it, I always get them mixed up but you know what I mean, you have the
wrong line terminator right below my comments.
***********) : right here. should be ); *********** data( d ), left( l ), right( r ) { }
};

// ------------------------------------------------------------------
// BinarySearchTree class
// ------------------------------------------------------------------
class BinarySearchTree
{
private:

// private data members
TreeNode* root;
int level; // class variable to help format printing

// private member functions
void FreeTree( TreeNode* & );
TreeNode* Search( DataType, TreeNode* ) const;
void Insert( DataType, TreeNode* & );
void Delete( DataType, TreeNode* & );
void Print( TreeNode* );
void PrintInorder( TreeNode* ) const;

public:

// public member functions

// constructor
BinarySearchTree( ) : root( NULL ) { }

// call recursive insert method
void Insert( DataType item ) { Insert( item, root ); }

// print the tree on its side, showing structure
void Print( ) { level = 0; Print( root ); }

// call recursive print method
void PrintInorder( ) const { PrintInorder( root ); }
};

#endif
// include files
#include <iostream>
#include <iomanip>
#include <cstdlib>
#include <ctime>
#include "bst.h"
using namespace std;

// Recursively insert an item in the tree, if not already present.
// Note that we pass root by reference.
void BinarySearchTree::Insert( DataType item, TreeNode* & root )
{
if ( root == NULL ) // hit NULL pointer, insert here
{
root = new TreeNode( item );
if ( root == NULL )
cerr << "Error: cannot allocate memory for TreeNode in
Insert()!\n";
}

else if ( item < root->data ) // insert in left subtree
Insert( item, root->left );

else if ( item > root->data ) // insert in right subtree
Insert( item, root->right );
}

// Recursive inorder tree traversal (called by Print function).
// Print the tree on its side, rotated counterclockwise to show
structure.
void BinarySearchTree::Print( TreeNode* nodeptr )
{
int i;

if ( nodeptr )
{
level++;

for ( i = 0; i < level; i++ )
cout << setw( 3 ) << ' ';
Print( nodeptr->right );

cout << '\r';
for ( i = 0; i < level - 1; i++ )
cout << setw( 3 ) << ' ';
cout << nodeptr->data << endl;

for ( i = 0; i < level; i++ )
cout << setw( 3 ) << ' ';
Print( nodeptr->left );

level--;
}
}

// Recursive tree traversals (called by Print routines).
// Print tree items inorder.
void BinarySearchTree::PrintInorder( TreeNode* nodeptr ) const
{
if ( nodeptr != NULL )
{
PrintInorder( nodeptr->left ); // print left subtree
cout << nodeptr->data << ' '; // print root
PrintInorder( nodeptr->right ); // print right subtree
}
}

// Program to test BinarySearchTree class: main() function
int main( )
{
BinarySearchTree tree;
DataType item;
int n = 20;
char answer;
cout<<"Would you like to enter the numbers or have the system
generate";
cout<<"them randomly. R for random and U for user input."<<endl;
cin>>answer;
if(answer == 'R' || answer =='r')
{
// generate a BST of random int values
srand( time( NULL ) ); // init random number generator
cout << "Inserting " << n << " items in the BST:";
for ( int i = 0; i < n; i++ )
{
item = rand() % n; // generate a random int between 0 and n-1
tree.Insert( item ); // insert it in the BST
if ( i % 10 == 0 ) cout << endl;
cout << setw( 7 ) << item;
}
}
else if(answer == 'U' || answer =='u')
{
cout<<"Enter data with spaces in between. # terminates the
input"<<endl;
do
{
cin>>item;
if(item != '#')
tree.Insert(item);
} while(item != '#');
}
else
cout<<"Wrong input."<<endl;

// print the tree
cout <<endl;
cout <<"Duplicates will not be shown"<<endl;
cout << "\n\nBST contents:\n";
tree.PrintInorder( );

cout << "\n\nBST structure:\n";
tree.Print( );
cout<<endl;

cout<<"Goodbye"<<endl;

return EXIT_SUCCESS;
}
again thanks for the help


Jul 22 '05 #4
In message <aj********************************@4ax.com>, Mark R Rivet
<ma********@verizon.net> writes

On 26 Jul 2004 18:08:19 -0700, bu*******@hotmail.com (Henry Jordon)
wrote:
I have everything pretty much done but I need to fix something in my
coding. I want to be able to enter strings such as "love", "hate",
"the", etc. but am unable to figure how to do this. I have put my .cpp
and my .h code below. Please help and thank you very much.

// include files
#ifndef BINTREE_H
#define BINTREE_H
#include <iostream>
#include <iomanip>
#include <cstdlib>
#include <ctime>
using namespace std;

// data types
typedef char DataType;

// TreeNode struct (includes constructor)
struct TreeNode
{
DataType data;
TreeNode *left, *right;

// constructor
TreeNode( DataType d = 0, TreeNode * l = NULL, TreeNode * r = NULL

You have a semi-colon here. It should be a colon or whatever you call
it, I always get them mixed up but you know what I mean, you have the
wrong line terminator right below my comments.
***********
) : right here. should be );

***********
data( d ), left( l ), right( r ) { }


Wrong. That's a constructor definition and the colon introduces an
initializer list.

John M Weiss's code
(http://www.hpcnet.org/upload/directo...31027093651.cc)
is fine, even if Mr Jordan did fail to acknowledge his authorship.
Somehow I don't think he's going to get full marks for this piece of
homework.

--
Richard Herring
Jul 22 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: tsunami | last post by:
hi all; I have an array and want to insert all the elements from this array to a binary search tree.That array is an object of the class of a stringtype which includes overloaded "< > = =="...
4
by: Tarique Jawed | last post by:
Alright I needed some help regarding a removal of a binary search tree. Yes its for a class, and yes I have tried working on it on my own, so no patronizing please. I have most of the code working,...
15
by: Foodbank | last post by:
Hi all, I'm trying to do a binary search and collect some stats from a text file in order to compare the processing times of this program (binary searching) versus an old program using linked...
1
by: mathon | last post by:
hi, now i facing a problem which i do not know how to solve it...:( My binary search tree structures stores a double number in every node, whereby a higher number is appended as right child...
1
by: hn.ft.pris | last post by:
I have the following code: Tree.h defines a simple binary search tree node structure ########## FILE Tree.h ################ #ifndef TREE_H #define TREE_H //using namespace std; template...
6
by: fdmfdmfdm | last post by:
This might not be the best place to post this topic, but I assume most of the experts in C shall know this. This is an interview question. My answer is: hash table gives you O(1) searching but...
4
by: whitehatmiracle | last post by:
Hello I have written a program for a binary tree. On compiling one has to first chose option 1 and then delete or search. Im having some trouble with the balancing function. It seems to be going...
11
by: Defected | last post by:
Hi, How i can create a Binary Search Tree with a class ? thanks
1
by: sarajan82 | last post by:
Hi all, Given a binary search tree and a number n, how can we find the smallest node of the tree greater than n and the largest node smaller than n? For example for the following binary search...
0
by: Rina0 | last post by:
Cybersecurity engineering is a specialized field that focuses on the design, development, and implementation of systems, processes, and technologies that protect against cyber threats and...
0
linyimin
by: linyimin | last post by:
Spring Startup Analyzer generates an interactive Spring application startup report that lets you understand what contributes to the application startup time and helps to optimize it. Support for...
0
by: kcodez | last post by:
As a H5 game development enthusiast, I recently wrote a very interesting little game - Toy Claw ((http://claw.kjeek.com/))。Here I will summarize and share the development experience here, and hope it...
0
by: Taofi | last post by:
I try to insert a new record but the error message says the number of query names and destination fields are not the same This are my field names ID, Budgeted, Actual, Status and Differences ...
14
DJRhino1175
by: DJRhino1175 | last post by:
When I run this code I get an error, its Run-time error# 424 Object required...This is my first attempt at doing something like this. I test the entire code and it worked until I added this - If...
0
by: Rina0 | last post by:
I am looking for a Python code to find the longest common subsequence of two strings. I found this blog post that describes the length of longest common subsequence problem and provides a solution in...
5
by: DJRhino | last post by:
Private Sub CboDrawingID_BeforeUpdate(Cancel As Integer) If = 310029923 Or 310030138 Or 310030152 Or 310030346 Or 310030348 Or _ 310030356 Or 310030359 Or 310030362 Or...
0
by: lllomh | last post by:
How does React native implement an English player?
2
by: DJRhino | last post by:
Was curious if anyone else was having this same issue or not.... I was just Up/Down graded to windows 11 and now my access combo boxes are not acting right. With win 10 I could start typing...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.