Basic Tree Structure Issue

I have programmed in Java for quite a while, yet I'm new to C++ and the concept of structures as opposed to classes. Below is this code:
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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
#include <iostream>

using namespace std;

struct Tree{
       int data;
       Tree* Right;
       Tree* Left;
       };

int main(){
   Tree* Root = NULL;
   Tree* RightBranch = NULL;
   Tree* LeftBranch = NULL;
   Tree* Current = NULL;

    Root = new Tree;
    RightBranch = new Tree;
    LeftBranch = new Tree;
    Current = new Tree;


    Root->data = 1;
    Root->Left = LeftBranch;
    Root->Right = RightBranch;

    RightBranch->data = 3;
    RightBranch->Left = NULL;
    RightBranch->Right = NULL;

    LeftBranch->data = 2;
    LeftBranch->Left = NULL;
    LeftBranch->Right = NULL;

}

void print( Tree *root )
{
        if ( root != NULL ) 
{
           print( root->Left);
           cout << root->data << " "; 
           print( root->Right); 
        }
     } 


Now, how can I call the print method from inside this struct / class. Or please advise how I should be doing this.
I don't understand the question.

Forward declare print() at the top of the file, then in main() do

print( Root );

? Is that what you are asking?
think he wants something like this.
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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
#include <iostream>

typedef unsigned int uint;
using namespace std;

class Bnode
{
  public:
	int data;
	Bnode *left;
	Bnode *right;
	
	Bnode()
	{ data = 0; left = NULL; right = NULL; }
	
};
			
class Tree
{
  private:
    Bnode *root;
	uint size;
	
	void Add( Bnode *leaf , Bnode *d )
	{
		if (d->data <= leaf->data)
		{
			if (leaf->left == NULL) { leaf->left = d; }
			else { Add( leaf->left , d ); }
		}
		else
		{
			if (leaf->right == NULL) { leaf->right = d; }
			else { Add( leaf->right , d ); }
		}
	}
	
	void Output(Bnode *leaf)
	{
		if ( leaf != NULL )
		{
			Output( leaf->left );
			cout << leaf->data << " ";
			Output( leaf->right );
		}
	}
	
	void RemoveAll(Bnode *leaf)
	{
		if ( leaf != NULL )
		{
			RemoveAll( leaf->left );
			RemoveAll( leaf->right );
			cout << leaf->data << " ! ";
			delete leaf;
			
		}
	}
	
  public:
  
    Tree() { root = NULL; size = 0; }
    
    void Push( int d )
    {
    	Bnode *n_data;
    	n_data = new Bnode;
    	n_data->data = d;
    	++size;
    	
    	if (root == NULL) { root = n_data; }
    	else
    	{
    		Add( root , n_data );
	}
    }
	
	void Print() { Output(root); }
	
	void PopAll() { RemoveAll(root); size = 0; }
	
	uint GetSize() { return size; }
};

int main()
{
	Tree btree;
	btree.Push(10);
	btree.Push(12);
	btree.Push(5);
	btree.Push(16);
	btree.Push(9);
	
	cout << "size of tree :: " << btree.GetSize() << endl;
	
	btree.Print();
	cout << endl << " deleting ";
	btree.PopAll();

	return 0;
}
Last edited on
Forward declare print() at the top of the file
<-

We are required to declare all methods in the header, but that should be fine!

Thanks for the other code but I wanted to know the differences in use between a struct and a class? Because as far as I'm aware the referencing is slightly different.
then what i done you don't want.

Forward declaring is
void print( Tree *root );
with no brackets and semicolon after it.

class and struct are the same except
for struct are public to start , class is private to start
There are other differences though surely...

eg.
When passed to methods, structures, just like primitive data types(e.g. integer, boolean) are passed by value. Which means a copy of structure is passed and any changes made to the passed structure will not affect the original structure. Whereas a class is passed by reference and any changes made to the passed instance of class will change the original value.

http://www.cplusplus.com/forum/beginner/5980/
http://bytes.com/topic/c/answers/131240-struct-vs-class
http://www.senzee5.com/2006/05/c-class-vs-struct.html

basicly it saying classes are the OOP way .
classes are safer because they start out as private

only reasons struct is there is to work with C codes.

so matter of opinion.
classes are safer because they start out as private

only reasons struct is there is to work with C codes.


Ehhhhhh

It's more of a conceptual difference.

Classes are for making self contained, encapsulated objects. Whereas structs are for making groups of data that are related in some way, but don't need to be encapsulated. Generally stucts have public data members and little/no member functions, whereas classes have public member functions and little/no public data members.

Of course there's no rule that says you HAVE to use structs/classes that way, but that's generally how they're perceived.

structs with public members still have a place in C++ and OOP. Not everything needs to be encapsulated all the time.
Last edited on
Topic archived. No new replies allowed.