468,765 Members | 1,499 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,765 developers. It's quick & easy.

c++ binary trees... compile trouble

TMS
119 100+
I'm trying to write an address book that is based on a binary tree. I'm devloping in Visual C++ (I blew up my Ubuntu with the new dist, so no EMACS), starting with the basics:

Expand|Select|Wrap|Line Numbers
  1. #ifndef binarySearchTree_h
  2. #define binarySearchTree_h
  3.  
  4. #include <string>
  5. #include <iostream>
  6.  
  7. using namespace std;
  8.  
  9. //Define the node
  10. struct treeNode
  11. //An object of type TreeNode 
  12. {
  13.     string data;
  14.     struct node* left;
  15.     struct node* right;
  16.  
  17. treeNode(string str)
  18. //constructor: Make a node containing str.
  19. {
  20.     data = str;
  21.     left = NULL;
  22.     right = NULL;
  23. }
  24.  
  25. };
  26.  
  27. //define the class
  28. class binarySearchTree
  29. {
  30. public:
  31.     binarySearchTree(); 
  32.         //constructor
  33.     bool isEmpty() const;
  34.                  //
  35.     bool treeContains(treeNode *root, string data);
  36.         //return true if item is one of the items in the 
  37.         //binary sort tree to which root points. Return
  38.         //false if not
  39.  
  40.  
  41. protected:
  42.     treeNode *root;
  43.  
  44. };
  45. #endif
  46.  
my problem is in the implementation file in the treecontains() function:

Expand|Select|Wrap|Line Numbers
  1. #include "binarySearchTree.h"
  2.  
  3. #include <string>
  4. #include <iostream>
  5.  
  6. using namespace std;
  7.  
  8. binarySearchTree::binarySearchTree() :root(NULL)
  9. {
  10. }
  11. bool binarySearchTree::isEmpty() const
  12.         //return true if tree is empty
  13. {
  14.     return (root == NULL);
  15. }
  16. bool binarySearchTree::treeContains(treeNode *root, string data)     
  17.         //return true if item is one of the items in the 
  18.         //binary sort tree to which root points. Return
  19.         //false if not
  20.     {
  21.         if(root==NULL)
  22.             //tree is empty
  23.         {
  24.                 return false;
  25.         }
  26.         if(data==root->data)
  27.             //item has been found
  28.         {
  29.             return true;
  30.         }
  31.  
  32.  
  33.         if(data < root->data)
  34.             //item is in left subtree
  35.         {
  36.             return treeContains(root->left, data);  //error
  37.         }
  38.         else
  39.             //item is in right subtree
  40.         {
  41.             return treeContains(root->right, data); //error
  42.         } 
  43.  
  44.     } //end treeContains()
  45.  
  46.  
I get an error that says:

Compiling...
test.cpp
binarySearchTree.cpp
c:\program files\microsoft visual studio\myprojects\binaryaddressbook\binarysearchtr ee.cpp(41) : error C2664: 'treeContains' : cannot convert parameter 1 from 'struct node *' to 'struct treeNode *'
Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast
c:\program files\microsoft visual studio\myprojects\binaryaddressbook\binarysearchtr ee.cpp(46) : error C2664: 'treeContains' : cannot convert parameter 1 from 'struct node *' to 'struct treeNode *'
Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast
Error executing cl.exe.

binaryAddressBook.exe - 2 error(s), 0 warning(s)

Here is my test file:

Expand|Select|Wrap|Line Numbers
  1. #include "binarySearchTree.h"
  2.  
  3. #include <iostream>
  4.  
  5. using namespace std;
  6.  
  7. int main()
  8. {
  9.  
  10.     treeNode *root;  //pointer to the root noode in the tree
  11.     root = NULL;  //start with an empty tree
  12.  
  13.  
  14.  
  15.     return 0;
  16. }
  17.  
as you can see main doesn't do anything yet, but get the constructors working.

please help so I can get this thing going?

thank you
Oct 31 '06 #1
1 2497
Banfa
9,057 Expert Mod 8TB
Is this structure correct?

Expand|Select|Wrap|Line Numbers
  1. //Define the node
  2. struct treeNode
  3. //An object of type TreeNode 
  4. {
  5.     string data;
  6.     struct node* left;
  7.     struct node* right;
  8.  
  9.     treeNode(string str)
  10.     //constructor: Make a node containing str.
  11.     {
  12.         data = str;
  13.         left = NULL;
  14.         right = NULL;
  15.     }
  16. };
  17.  
?

Particularly is struct node* left; right or should it be struct TreeNode * left; ?

If it is correct where is struct node defined?
Nov 1 '06 #2

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

4 posts views Thread by abhrajit | last post: by
4 posts views Thread by Rasmus | last post: by
3 posts views Thread by Will Oram | last post: by
1 post views Thread by Jerry Khoo | last post: by
3 posts views Thread by ptrSriram | last post: by
8 posts views Thread by sudharsan | last post: by
10 posts views Thread by free2cric | last post: by
2 posts views Thread by pyguy | last post: by
7 posts views Thread by Vinodh | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by zhoujie | last post: by
reply views Thread by Marin | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.