Pointers and Binary Tree Inorder Probelm

Yes this is a HW assignment. I will take any hints offered. \

I have implemented 2 binary trees. One using new.. I didn't use struct.. Just as a class. How do I pass a pointer to the method inorder()? There is an error with the ->left members being private.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include <iostream>
#include "BinaryTree.h" // this brings in the declaration for BinaryTree()

int main()
{
    using namespace std;
	BinaryTree ATree(3);  // Constructs a BinaryTree object
	ATree.addNode(5); // Adds a node to ATree whose value is 5

	BinaryTree *root = new BinaryTree(7);
	root->addNode(2); // adds a node
	
	// Impliment inorder.  How?
	
	cin.get(); // A pause for debuging..  Remove when done.
	return 0;
}


1
2
3
4
5
6
7
8
9
10
11
class BinaryTree {
public:
    BinaryTree(int data);
    void addNode(int data);              
	int inorder() ;

private:	
	int data;
	BinaryTree *left;
	BinaryTree *right;
};  // End Class BinaryTree 


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
#include <iostream>
#include "BinaryTree.h"

using namespace std;

BinaryTree::BinaryTree(int data)  // Constructs a tree with 'data' and 2 NULL children
{
 this->data = data;
 this->left=NULL;
 this->right=NULL;
 BinaryTree *p;
 };

void BinaryTree::addNode(int nodeval) {	
	if (nodeval <= data) { // If goes to left child.
			left = new BinaryTree(nodeval); // Creates a new sub Tree on left
		}  // End if
	else { // If goes to right child.  
			right = new BinaryTree(nodeval); // Creates a new sub Tree on right
		}  // End else	
};  // End addNode

int inorder(BinaryTree *p) {  // how do I call this fcn, w a ptr root?
    if (p != NULL) {
        inorder(p->left);  // print left subtree
        cout << p->data << endl; // print this node
        inorder(p->right); // print right subtree
	return 0;
    }
} 
Topic archived. No new replies allowed.