Its all about C++ Binary Trees

In summary, the conversation is about a person trying to write an address book using a binary tree structure in Visual C++. They are having trouble with the implementation file and getting an error regarding incompatible pointer types. The expert suggests that the issue may be due to using pointers to a struct node instead of pointers to struct treeNode.
  • #1
Trista
33
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
  • #2
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;.
 
  • #3
Yep: TreeNode needs pointers to TreeNode, not pointers to struct node.

Cheers,
Tim
 
  • #4
You are so right! Thank you!
 

Related to Its all about C++ Binary Trees

What is a binary tree?

A binary tree is a data structure in computer science that is used to store data in a hierarchical way. It consists of nodes, which can have up to two child nodes, a left child and a right child. Each node can also have data associated with it.

What is the difference between a binary tree and a binary search tree?

A binary search tree is a type of binary tree that follows a specific rule: for any given node, all the nodes in its left subtree have values smaller than the node's value, while all the nodes in its right subtree have values greater than the node's value. This rule makes searching for a specific value in a binary search tree more efficient compared to a regular binary tree.

How are binary trees useful in computer science?

Binary trees are useful for storing and organizing data in a hierarchical manner. They are commonly used in algorithms for sorting and searching data, as well as in data compression and encoding methods.

What is the time complexity for searching a value in a binary tree?

The time complexity for searching a value in a binary tree is O(log n) in the average case and O(n) in the worst case. This is because in a binary tree, the search starts from the root node and the number of nodes to search decreases by half at each level, resulting in a logarithmic time complexity.

What are some common operations performed on binary trees?

Some common operations on binary trees include insertion and deletion of nodes, searching for a specific value, and traversing the tree to access or modify data. Other operations include balancing the tree, which ensures that the tree remains efficient and organized, and tree traversal algorithms, which allow for processing all the nodes in a specific order.

Similar threads

  • Programming and Computer Science
Replies
3
Views
3K
  • Programming and Computer Science
Replies
3
Views
2K
  • Programming and Computer Science
2
Replies
40
Views
2K
  • Programming and Computer Science
Replies
5
Views
2K
  • Programming and Computer Science
Replies
13
Views
4K
  • Engineering and Comp Sci Homework Help
Replies
2
Views
1K
  • Programming and Computer Science
Replies
1
Views
1K
  • Programming and Computer Science
Replies
8
Views
3K
  • Programming and Computer Science
Replies
2
Views
1K
  • Programming and Computer Science
Replies
6
Views
1K
Back
Top