What's wrong with this code because I'm getting errors but everything looks fine though.

Attachments
#include "BinaryTree.h"
#include <iostream>
using namespace std;

BinaryTree::BinaryTree()
{
	root = NULL;
}

bool BinaryTree::isEmpty()
{
	return (root == NULL);
}

void BinaryTree::insert(int& data)
{
	nodeType *current;
	nodeType *trailCurrent;
	nodeType *newNode;

	newNode = new nodeType;

	newNode->data = data;
	newNode->lLink = NULL;
	newNode->rLink = NULL;

	if (isEmpty())
		root = newNode;
	else
	{
		current = root;

		while (current != NULL)
		{
			trailCurrent = current;

			if (current->data == data)
				cout << "Cannot have duplicate items" << endl;
			else if (current->data > data)
				current = current->lLink;
			else
				current = current->rLink;
		}

		if (trailCurrent->data > data)
			trailCurrent = trailCurrent->lLink;
		else
			trailCurrent = trailCurrent->rLink;
	} 
}

void BinaryTree::remove()
{
}

void BinaryTree::inorder(nodeType *p) const
{	

	if (p != NULL)
	{
		inorder(p->lLink);
		cout << p->data << " ";
		inorder(p->rLink);
	}
}

void BinaryTree::preorder(nodeType *p) const
{

	if (p != NULL)
	{
		cout << p->data << " ";
		preorder(p->lLink);
		preorder(p->rLink);
	}
}

void BinaryTree::postorder(nodeType *p) const
{

	if (p != NULL)
	{
		postorder(p->lLink);
		postorder(p->rLink);
		cout << p->data << " ";
	}
}
#ifndef BINARY_TREE_H
#define BINARY_TREE_H

struct nodeType
{
	int data;
	nodeType *lLink;
	nodeType *rLink;
};

class BinaryTree
{
	private:
		nodeType *root;
	public:
		BinaryTree();
		bool isEmpty();
		void insert(int& data);
		void remove();
		void inorder(nodeType *p) const;
		void preorder(nodeType *p) const;
		void postorder(nodeType *p) const;
};

#endif
#include "BinaryTree.h"
#include <iostream>
using namespace std;

int main()
{
	BinaryTree b;

	b.insert(60);
	b.insert(50);
	b.insert(70);

	cout << "Inorder traversal: " << endl;
	b.inorder();

	cout << "Preorder traversal: " << endl;
	b.preorder();

	cout << "Postorder traversal: " << endl;
	b.postorder();
	
	return 0;
}

And what are your errors?

I would rather see the code posted here instead of download your files...

#include "BinaryTree.h"
#include <iostream>
using namespace std;

BinaryTree::BinaryTree()
{
	root = NULL;
}

bool BinaryTree::isEmpty()
{
	return (root == NULL);
}

void BinaryTree::insert(int& data)
{
	nodeType *current;
	nodeType *trailCurrent;
	nodeType *newNode;

	newNode = new nodeType;

	newNode->data = data;
	newNode->lLink = NULL;
	newNode->rLink = NULL;

	if (isEmpty())
		root = newNode;
	else
	{
		current = root;

		while (current != NULL)
		{
			trailCurrent = current;

			if (current->data == data)
				cout << "Cannot have duplicate items" << endl;
			else if (current->data > data)
				current = current->lLink;
			else
				current = current->rLink;
		}

		if (trailCurrent->data > data)
			trailCurrent = trailCurrent->lLink;
		else
			trailCurrent = trailCurrent->rLink;
	} 
}

void BinaryTree::remove()
{
}

void BinaryTree::inorder(nodeType *p) const
{	

	if (p != NULL)
	{
		inorder(p->lLink);
		cout << p->data << " ";
		inorder(p->rLink);
	}
}

void BinaryTree::preorder(nodeType *p) const
{

	if (p != NULL)
	{
		cout << p->data << " ";
		preorder(p->lLink);
		preorder(p->rLink);
	}
}

void BinaryTree::postorder(nodeType *p) const
{

	if (p != NULL)
	{
		postorder(p->lLink);
		postorder(p->rLink);
		cout << p->data << " ";
	}
}
#include "BinaryTree.h"
#include <iostream>
using namespace std;

int main()
{
	BinaryTree b;

	b.insert(60);
	b.insert(50);
	b.insert(70);

	cout << "Inorder traversal: " << endl;
	b.inorder();

	cout << "Preorder traversal: " << endl;
	b.preorder();

	cout << "Postorder traversal: " << endl;
	b.postorder();
	
	return 0;
}

If I remember correctly about pointer, line 14 in your BinaryTree

void BinaryTree::insert(int& data)

Are you expecting a pointer for it (with the & symbol), or just a plain integer value? Because you are calling it as

b.insert(60);

I don't know your node structure here...

Edited 5 Years Ago by Taywin: n/a

Here's the header file:

#ifndef BINARY_TREE_H
#define BINARY_TREE_H

struct nodeType
{
	int data;
	nodeType *lLink;
	nodeType *rLink;
};

class BinaryTree
{
	private:
		nodeType *root;
	public:
		BinaryTree();
		bool isEmpty();
		void insert(int& data);
		void remove();
		void inorder(nodeType *p) const;
		void preorder(nodeType *p) const;
		void postorder(nodeType *p) const;
};

#endif

When you use "&" in your function declaration, it expects you to pass by reference. As a result, the argument passed in should be variable.

int x=60;
b.insert(x);

But I do not believe that the insert() function should take an integer argument that way. Because if the incoming argument variable value is changed, it could break the tree. I believe you should pass it by value instead -- insert(int).

It is a value, but it is a copied value. Why would you want to take an object? If you look at your nodeType struct, it takes only a value (data) which can be copied over instead of a variable pointer.

Edited 5 Years Ago by Taywin: n/a

See the difference between the two:

//in .h
void inorder(nodeType *p) const;
void preorder(nodeType *p) const;
void postorder(nodeType *p) const;
//in main
b.inorder();
b.preorder();
b.postorder();

Edited 5 Years Ago by firstPerson: n/a

See the difference between the two:

//in .h
void inorder(nodeType *p) const;
void preorder(nodeType *p) const;
void postorder(nodeType *p) const;
//in main
b.inorder();
b.preorder();
b.postorder();

But this is what I already have

This article has been dead for over six months. Start a new discussion instead.