My first very simple Binary Tree :)

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;
    
    Current = Root;
    
                  while(Current != NULL){
                        cout << Current->data << endl;
                        Current = Current->Left;
                        }
                        
    
    
    system("PAUSE");
}


Hello everyone :), so been looking online for a way to print binary trees, and every site ive been to has been way to complex for my current level :) so I was hoping that you very smart guys and gals would be able to assist me!
I have it setup to print the left side of the tree but I can't figure out how to print both the right and left :(, and I would love for someone to push me in the right direction that way I learn faster :).

Thanks for all the help :)
you guys rock!
Rawr
Simplest way is a recursive function.

1
2
3
4
5
6
7
void Print( Tree* node ) {
    if( node ) {
        Print( node->Left );
        cout << node->data << endl;
        Print( node->Right );
    }
}


Topic archived. No new replies allowed.