string BST

class StringTree{
private:
class TreeNode{
friend class StringTree;
string str;
TreeNode *left;
TreeNode *right;
TreeNode(string s, TreeNode *l=NULL, TreeNode *r=NULL){
str=s, left=l, right=r;}
};
TreeNode *root;
..
..
..
..
..

void displayInOrder(TreeNode *tree);
void displayPreOrder(TreeNode *tree);
void displayPostOrder(TreeNode *tree);
..
..
..
//prints using inorder method
void StringTree::displayInOrder(TreeNode *tree){
if(tree){
displayInOrder(tree->left);
cout<<tree->str<<" ";
displayInOrder(tree->right);
}
}
//prints using preorder method
void StringTree::displayPreOrder(TreeNode *tree){
if(tree){
cout<<tree->str<<" ";
displayPreOrder(tree->left);
displayPreOrder(tree->right);
}
}
//prints using post order method
void StringTree::displayPostOrder(TreeNode *tree){
if(tree){
displayPostOrder(tree->left);
displayPostOrder(tree->right);
cout<<tree->str<<" ";
}
}

This is some of the code for the tree I've written. The only problem I'm having is when I use the << operator with cout<<tree->value, it's not seeming to recognize the operator and I can't figure out why. Any thoughts?
I realize it's probably a silly problem. I've just been working on it for a couple hours and don't know why it's behaving that way. Sidenote: The file also has the proper header files included, I just didn't include them on the post.
closed account (zwA4jE8b)
did you #include <iostream>
did you std::cout
Yes and yes.
Did you see if it's a precedence issue?
Try cout<<(tree->str)<<" ";.
It'd be helpful to have the error itself.
'std::basic_ostream<_Elem,_Traits> &std::basic_ostream<_Elem,_Traits>::operator <<(unsigned int)'
1> with
1> [
1> _Elem=char,
1> _Traits=std::char_traits<char>
1> ]
1> c:\program files (x86)\microsoft visual studio 10.0\vc\include\ostream(325): or 'std::basic_ostream<_Elem,_Traits> &std::basic_ostream<_Elem,_Traits>::operator <<(long)'
1> with
1> [
1> _Elem=char,
1> _Traits=std::char_traits<char>
1> ]
1> c:\program files (x86)\microsoft visual studio 10.0\vc\include\ostream(345): or 'std::basic_ostream<_Elem,_Traits> &std::basic_ostream<_Elem,_Traits>::operator <<(unsigned long)'
1> with
1> [
1> _Elem=char,
1> _Traits=std::char_traits<char>
1> ]

That's the jist of the error.
Last edited on


#include <iostream>
#include <vector>
#include <fstream>

using namespace std;

//Class declaration for Binary seach tree
class StringTree{
private:
class TreeNode{
friend class StringTree;
string str;
TreeNode *left;
TreeNode *right;
TreeNode(string s, TreeNode *l=NULL, TreeNode *r=NULL){
str=s, left=l, right=r;}
};
TreeNode *root;

void insert(TreeNode *&tree, string word);
void remove(TreeNode *&tree, string word);
void makeDeletion(TreeNode *&tree);
void destroySubtree(TreeNode *tree);
void displayInOrder(TreeNode *tree);
void displayPreOrder(TreeNode *tree);
void displayPostOrder(TreeNode *tree);


public:
//inline recursive functions
StringTree()
{root=NULL;}
~StringTree()
{destroySubtree(root);}

bool search(string word);
bool isBalanced(TreeNode *tree);
int height(TreeNode *tree);

void insert(string word)
{insert(root, word);}

void remove(string word)
{remove(root, word);}

void showInOrder(void)
{displayInOrder(root);}

void showPreOrder(void)
{displayPreOrder(root);}

void showPostOrder(void)
{displayPostOrder(root);}
void getTreeSize (int &size);
};

//for inserting into the tree
void StringTree::insert(TreeNode *&tree, string word)
{
if(!tree)
{tree=new TreeNode(word);
return;}

if(tree->str.compare(word))
return;
if(tree->str.compare(word)<0)
insert(tree->left, word);
else
insert(tree->right, word);
}

//removing from the tree
void StringTree::remove(TreeNode *&tree, string word)
{
if(!tree) return;
if(tree->str.compare(word)<0)
remove(tree->left, word);
else if(tree->str.compare(word)>0)
remove(tree->right, word);
else
makeDeletion(tree);
}
//searching for data in the tree
bool StringTree::search(string word)
{
TreeNode *tree=root;
while(tree){
if(tree->str.compare(word))
return true;
else if(tree->str.compare(word)<0)
tree=tree->left;
else
tree=tree->right;
}
return false;
}
//destroys a subtree
void StringTree::destroySubtree(TreeNode *tree){
if(!tree) return;
destroySubtree(tree->left);
destroySubtree(tree->right);
delete tree;
}
//prints using inorder method
void StringTree::displayInOrder(TreeNode *tree){
if(tree){
displayInOrder(tree->left);
cout<<tree->str<<" ";
displayInOrder(tree->right);
}
}
//prints using preorder method
void StringTree::displayPreOrder(TreeNode *tree){
if(tree){
cout<<tree->str<<" ";
displayPreOrder(tree->left);
displayPreOrder(tree->right);
}
}
//prints using post order method
void StringTree::displayPostOrder(TreeNode *tree){
if(tree){
displayPostOrder(tree->left);
displayPostOrder(tree->right);
cout<<(tree->str)<<" ";
}
}
//for deleting
void StringTree::makeDeletion(TreeNode *&tree){
TreeNode *deleteNode;
TreeNode *attach;

if(!tree->right)
tree=tree->left;
else if(!tree->left)
tree=tree->right;
else
attach=tree->right;
while(attach->left)
{
attach=attach->left;
attach->left=tree->left;
tree=tree->right;
}
delete deleteNode;
}

void StringTree::getTreeSize (int &size)
{
TreeNode *tree=root;
while(tree){
if (tree->right)
{
size++;
tree=tree->right;
}
if (tree->left)
{
size++;
tree=tree->left;
}
}
}//end getTreeSize

bool StringTree::isBalanced(TreeNode *tree)
{
int lh; /* for height of left subtree */
int rh; /* for height of right subtree */

/* If tree is empty then return true */
if(!tree)
return 1;

/* Get the height of left and right sub trees */
lh = height(tree->left);
rh = height(tree->right);

if( abs(lh-rh) <= 1 &&
isBalanced(root->left) &&
isBalanced(root->right))
return 1;

/* If we reach here then tree is not balanced */
return 0;
}//end isBalanced

int StringTree::height(TreeNode *tree)
{
if (!tree)
return 0;
else
{
/* compute the depth of each subtree */
int lDepth = height(tree->left);
int rDepth = height(tree->right);

/* use the larger one */
if (lDepth > rDepth)
return (lDepth+1);
else return (rDepth+1);
}
} // end height


}

That's my complete code.






Last edited on
Wow. I forgot #include<string>...
Topic archived. No new replies allowed.