C/C++ Why Am I Getting Errors in My C++ Binary Tree Implementation?

  • Thread starter Thread starter Trista
  • Start date Start date
  • Tags Tags
    Binary C++ Trees
AI Thread Summary
The discussion centers around the development of a binary search tree in Visual C++ for an address book application. The initial implementation defines a `treeNode` structure with string data and pointers to left and right child nodes. The `binarySearchTree` class includes methods to check if the tree is empty and to determine if a specific item exists within the tree. The main issue arises in the `treeContains` function, where the code attempts to use pointers of type `struct node` instead of `struct treeNode`, leading to compilation errors. A key suggestion is made to correct the pointer types in the `treeNode` structure to ensure they point to `treeNode` instead of `node`. This adjustment is crucial for resolving the type conversion errors and allowing the implementation to function correctly. The discussion highlights the importance of proper type definitions in C++ programming, especially when dealing with data structures like binary trees.
Trista
Messages
32
Reaction score
0
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), starting with the basics:

Code:
#ifndef binarySearchTree_h
#define binarySearchTree_h

#include <string>
#include <iostream>

using namespace std;

//Define the node
struct treeNode
//An object of type TreeNode 
{
	string data;
	struct node* left;
	struct node* right;

treeNode(string str)
//constructor: Make a node containing str.
{
	data = str;
	left = NULL;
	right = NULL;
}

};

//define the class
class binarySearchTree
{
public:
	binarySearchTree(); 
		//constructor
	bool isEmpty() const;
	             //
	bool treeContains(treeNode *root, string data);
		//return true if item is one of the items in the 
		//binary sort tree to which root points. Return
		//false if not


protected:
	treeNode *root;
	
};
#endif

my problem is in the implementation file in the treecontains() function:

Code:
#include "binarySearchTree.h"

#include <string>
#include <iostream>

using namespace std;

binarySearchTree::binarySearchTree() :root(NULL)
{
}
bool binarySearchTree::isEmpty() const
		//return true if tree is empty
{
	return (root == NULL);
}
bool binarySearchTree::treeContains(treeNode *root, string data)     
		//return true if item is one of the items in the 
		//binary sort tree to which root points. Return
		//false if not
	{
		if(root==NULL)
			//tree is empty
		{
				return false;
		}
		if(data==root->data)
			//item has been found
		{
			return true;
		}

		
		if(data < root->data)
			//item is in left subtree
		{
			return treeContains(root->left, data);  //error
		}
		else
			//item is in right subtree
		{
			return treeContains(root->right, data); //error
		} 

	} //end treeContains()

I get an error that says:

Compiling...
test.cpp
binarySearchTree.cpp
c:\program files\microsoft visual studio\myprojects\binaryaddressbook\binarysearchtree.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\binarysearchtree.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:

Code:
#include "binarySearchTree.h"

#include <iostream>

using namespace std;

int main()
{

	treeNode *root;  //pointer to the root noode in the tree
	root = NULL;  //start with an empty tree



	return 0;
}

as you can see it doesn't do anything, but get the constructors working.

please help so I can get this thing going?

thank you
 
Last edited:
Technology news on Phys.org
Trista said:
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), starting with the basics:

Code:
#ifndef binarySearchTree_h
#define binarySearchTree_h

#include <string>
#include <iostream>

using namespace std;

//Define the node
struct treeNode
//An object of type TreeNode 
{
	string data;
	struct node* left;
	struct node* right;

treeNode(string str)
//constructor: Make a node containing str.
{
	data = str;
	left = NULL;
	right = NULL;
}

};
It's been years since I've used C++ but I think you should have struct treeNode* left; and struct treeNode* right;.
 
Yep: TreeNode needs pointers to TreeNode, not pointers to struct node.

Cheers,
Tim
 
You are so right! Thank you!
 
Thread 'Is this public key encryption?'
I've tried to intuit public key encryption but never quite managed. But this seems to wrap it up in a bow. This seems to be a very elegant way of transmitting a message publicly that only the sender and receiver can decipher. Is this how PKE works? No, it cant be. In the above case, the requester knows the target's "secret" key - because they have his ID, and therefore knows his birthdate.
I tried a web search "the loss of programming ", and found an article saying that all aspects of writing, developing, and testing software programs will one day all be handled through artificial intelligence. One must wonder then, who is responsible. WHO is responsible for any problems, bugs, deficiencies, or whatever malfunctions which the programs make their users endure? Things may work wrong however the "wrong" happens. AI needs to fix the problems for the users. Any way to...

Similar threads

Replies
3
Views
4K
Replies
22
Views
3K
Replies
5
Views
3K
Replies
40
Views
3K
Replies
39
Views
4K
Replies
7
Views
75K
Back
Top