Printing a binary tree.

It compiled but it doesn't work.

Page 1 of 1

3 Replies - 2110 Views - Last Post: 10 November 2010 - 06:32 AM Rate Topic: -----

#1 Vixthefox  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 13
  • Joined: 08-October 08

Printing a binary tree.

Posted 10 November 2010 - 12:56 AM

Hello! I need a bit of help in this. I wrote this program and after compiling I was glad to see it running, but somehow it isn't working. I have to get this program to work in the way it was described by my teacher, but I have noticed that it doesn't print any numbers but instead just jams up. Please help me find what's wrong.

here is my code:

#include<cstdio>

class tree
{
      private:
              struct node
              {
                     int num;
                     tree *lft,*rgt;
                     }*root;
      public:
             tree();
             ~tree();
             void add(int, tree *,tree *);
             void inorder(void);
             void preorder(void);
             void postorder(void);       
             };//end of class tree
             
tree::tree()//constructor
{
            root=new node;
            root->num=0;
            root->lft=NULL;
            root->rgt=NULL;
            }
            
tree::~tree(){delete root;}//destructor

void tree::add(int v, tree *L, tree *R)
{
     root->num=v;
     root->lft=L;
     root->rgt=R;
     }//end of add()
     
void tree::inorder(void)
{
     if(root!=NULL){
                    (root->lft)->inorder();
                    printf("%i",root->num);
                    (root->rgt)->inorder();
                    }
     }//end of inorder()

void tree::preorder(void)
{
     if(root!=NULL){
                    printf("%i",root->num);
                    (root->lft)->preorder();
                    (root->rgt)->preorder();
                    }
     }//end of preorder()
     
void tree::postorder(void)
{
     if(root!=NULL){
                    (root->lft)->postorder();
                    (root->rgt)->postorder();
                    printf("%i",root->num);
                    }
     }//end of postorder()
     
int main()
{
    tree A,B,C,D,E,F,G,H,I;
    I.add(8,NULL,NULL);
    H.add(6,NULL,NULL);
    G.add(7,&H,&I);
    F.add(5,NULL,&G);
    C.add(6,&F,NULL);
    A.add(4,NULL,&C);
    D.add(1,NULL,NULL);
    E.add(3,NULL,NULL);
    B.add(2,&D,&E);
    A.add(4,&B,&C);   
    A.inorder();
    printf("\n\n");
    A.preorder();
    printf("\n\n");
    A.postorder();
    
    getchar();
    return 0;
}



Is This A Good Question/Topic? 0
  • +

Replies To: Printing a binary tree.

#2 Djabby  Icon User is offline

  • D.I.C Head

Reputation: 37
  • View blog
  • Posts: 131
  • Joined: 02-November 10

Re: Printing a binary tree.

Posted 10 November 2010 - 02:08 AM

You check if root is always NULL. But that is unnecessary, because any tree is defining his root in his constructor. But: e.g. (root->left)->preorder(). Here you dereference first root, what is ok, because root is never null. But then you dereference root->left without checking that it is null! And NULL->preorder() will NOT work! So, remove the root-check and add some branch-checks:

void tree::inorder(void)
{
    if(root->lft)
        (root->lft)->inorder();
    printf("%i",root->num);
    if(root->rgt)
        (root->rgt)->inorder();
}

This post has been edited by Djabby: 10 November 2010 - 02:09 AM

Was This Post Helpful? 1
  • +
  • -

#3 Vixthefox  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 13
  • Joined: 08-October 08

Re: Printing a binary tree.

Posted 10 November 2010 - 03:04 AM

It worked! I also understood what you meant by. Thank you so much!
Was This Post Helpful? 0
  • +
  • -

#4 baavgai  Icon User is offline

  • Dreaming Coder
  • member icon

Reputation: 5801
  • View blog
  • Posts: 12,636
  • Joined: 16-October 07

Re: Printing a binary tree.

Posted 10 November 2010 - 06:32 AM

I think you're missing the point of a tree. If you have to keep track of all the nodes it's hardly useful.

You probably want something like:
class tree {
private:
	struct node {
		int num;
		node *lft,*rgt;
	} *root;
public:
	tree();
	~tree();
	void add(int);
	void inorder();
	void preorder();
	void postorder();
};

//...

int main() {
	tree T;
	T.add(8);
	T.add(6);
	T.add(7);
	T.add(5);
	T.add(6);
	T.add(4);
	T.add(1);
	T.add(3);
	T.add(2);
	T.add(4);
	T.inorder();
	T.preorder();
	T.postorder();
	return 0;
}


Was This Post Helpful? 0
  • +
  • -

Page 1 of 1