473,387 Members | 1,512 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

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

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 2731
Banfa
9,065 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

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

Similar topics

4
by: abhrajit | last post by:
I'm looking for a C/C++/Java library to create a balanced binary tree data structure given a set of leaf nodes as input. A leaf node should never become an interior node. So if I wish to create...
4
by: Rasmus | last post by:
Hi. As partly novice in python I would like a piece of advise of how to implement (binary) trees the best way? Thanks in advance, Rasmus PS: Due to heavy spam reception (20.000+/week), I...
3
by: Will Oram | last post by:
Hi, My assignment is to create a non-binary tree of arbitrary form, and then print out the data in an orderly fashion. The handout contains a tree to be inputted: 2 / | \ 3 7 5 / \ |
1
by: Jerry Khoo | last post by:
hello, everybody, i am kinda new here, nice to meet u all. Now, i am > cs students and are now facing difficult problems in understanding > what a binary tree is, how it works, and the algorithm...
3
by: ptrSriram | last post by:
Can someone help me with an algorithm to merge two binary search trees. One method I thought of was to flatten both the trees into sorted lists(inorder traversal),merge those two sorted lists,...
8
by: sudharsan | last post by:
please gimme the logic to merge two binary search trees?I mean which node has to be the root node of the new binary tree?? Thanks in advance
10
by: free2cric | last post by:
Hi, I have a single link list which is sorted. structure of which is like typedef struct mylist { int num; struct mylist *next;
2
by: pyguy | last post by:
Hi all, I am running into a conceptual glitch in implementing a simple binary tree class. My insertion and printing (sorting) seems to be ok, but when I search the tree, my find method isn't doing...
7
by: Vinodh | last post by:
Started reading about Binary Trees and got the following questions in mind. Please help. Definition of a Binary Tree from "Data Structures using C and C++ by Tanenbaum" goes like this, "A...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.