Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. c prgram to display a binary tree but it is not showing any output, what is the error in this program can you help me

c prgram to display a binary tree but it is not showing any output, what is the error in this program can you help me

Scheduled Pinned Locked Moved C / C++ / MFC
helpdata-structuresperformancequestion
4 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • U Offline
    U Offline
    User 12502505
    wrote on last edited by
    #1

    #include
    #include
    struct tree
    {
    char info;
    struct tree *left;
    struct tree *right;
    };
    struct tree *root;
    struct tree *stree(struct tree *root,struct tree *r,char info);
    void print_tree(struct tree *root,int l);
    int main(void)
    {
    char s[80];
    int l=3;
    root=NULL;
    do
    {
    printf("enter a letter:");
    gets(s);
    root=stree(root,root, *s);

    }
    while(\*s);
    print\_tree(root,0);
    return 0;
    

    }
    struct tree *stree(struct tree *root,struct tree *r,char info)
    {
    if(!r)
    {
    r=(struct tree *) malloc(sizeof(struct tree));
    if(!r)
    {
    printf("out of memory \n");
    exit(0);

    	}
    	r->left=NULL;
    	r->right=NULL;
    	r->info=info;
    	if(!root)
    	return r;
    	if(infoinfo)
    	root->left=r;
    	else
    	root->right=r;
    	return r;
    	
    }
    if(infoinfo)
    stree(r,r->left,info);
    else
    stree(r,r->right,info);
    return root;
    

    }
    void print_tree(struct tree *r,int l)
    {
    int i;
    if(!r) return ;
    print_tree(r->right,l+1);
    for(i=0;iinfo);
    print_tree(r->left,l+1);

    }

    L D P 3 Replies Last reply
    0
    • U User 12502505

      #include
      #include
      struct tree
      {
      char info;
      struct tree *left;
      struct tree *right;
      };
      struct tree *root;
      struct tree *stree(struct tree *root,struct tree *r,char info);
      void print_tree(struct tree *root,int l);
      int main(void)
      {
      char s[80];
      int l=3;
      root=NULL;
      do
      {
      printf("enter a letter:");
      gets(s);
      root=stree(root,root, *s);

      }
      while(\*s);
      print\_tree(root,0);
      return 0;
      

      }
      struct tree *stree(struct tree *root,struct tree *r,char info)
      {
      if(!r)
      {
      r=(struct tree *) malloc(sizeof(struct tree));
      if(!r)
      {
      printf("out of memory \n");
      exit(0);

      	}
      	r->left=NULL;
      	r->right=NULL;
      	r->info=info;
      	if(!root)
      	return r;
      	if(infoinfo)
      	root->left=r;
      	else
      	root->right=r;
      	return r;
      	
      }
      if(infoinfo)
      stree(r,r->left,info);
      else
      stree(r,r->right,info);
      return root;
      

      }
      void print_tree(struct tree *r,int l)
      {
      int i;
      if(!r) return ;
      print_tree(r->right,l+1);
      for(i=0;iinfo);
      print_tree(r->left,l+1);

      }

      L Offline
      L Offline
      Lost User
      wrote on last edited by
      #2

      You need to use your debugger to gather more information about what happens when you run the code.

      1 Reply Last reply
      0
      • U User 12502505

        #include
        #include
        struct tree
        {
        char info;
        struct tree *left;
        struct tree *right;
        };
        struct tree *root;
        struct tree *stree(struct tree *root,struct tree *r,char info);
        void print_tree(struct tree *root,int l);
        int main(void)
        {
        char s[80];
        int l=3;
        root=NULL;
        do
        {
        printf("enter a letter:");
        gets(s);
        root=stree(root,root, *s);

        }
        while(\*s);
        print\_tree(root,0);
        return 0;
        

        }
        struct tree *stree(struct tree *root,struct tree *r,char info)
        {
        if(!r)
        {
        r=(struct tree *) malloc(sizeof(struct tree));
        if(!r)
        {
        printf("out of memory \n");
        exit(0);

        	}
        	r->left=NULL;
        	r->right=NULL;
        	r->info=info;
        	if(!root)
        	return r;
        	if(infoinfo)
        	root->left=r;
        	else
        	root->right=r;
        	return r;
        	
        }
        if(infoinfo)
        stree(r,r->left,info);
        else
        stree(r,r->right,info);
        return root;
        

        }
        void print_tree(struct tree *r,int l)
        {
        int i;
        if(!r) return ;
        print_tree(r->right,l+1);
        for(i=0;iinfo);
        print_tree(r->left,l+1);

        }

        D Offline
        D Offline
        David Crow
        wrote on last edited by
        #3

        Ditto what Richard suggested. Printing aside, you need to first verify that the tree is being built correctly. The only way to do that is to single step through each line of code (the stree() function) using the debugger. Note the values of info, left, and right along the way. As you build the tree on paper, what you see in the debugger should match. Are you trying to print the tree contents pre-order, in-order, or post-order?

        "One man's wage rise is another man's price increase." - Harold Wilson

        "Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons

        "You can easily judge the character of a man by how he treats those who can do nothing for him." - James D. Miles

        1 Reply Last reply
        0
        • U User 12502505

          #include
          #include
          struct tree
          {
          char info;
          struct tree *left;
          struct tree *right;
          };
          struct tree *root;
          struct tree *stree(struct tree *root,struct tree *r,char info);
          void print_tree(struct tree *root,int l);
          int main(void)
          {
          char s[80];
          int l=3;
          root=NULL;
          do
          {
          printf("enter a letter:");
          gets(s);
          root=stree(root,root, *s);

          }
          while(\*s);
          print\_tree(root,0);
          return 0;
          

          }
          struct tree *stree(struct tree *root,struct tree *r,char info)
          {
          if(!r)
          {
          r=(struct tree *) malloc(sizeof(struct tree));
          if(!r)
          {
          printf("out of memory \n");
          exit(0);

          	}
          	r->left=NULL;
          	r->right=NULL;
          	r->info=info;
          	if(!root)
          	return r;
          	if(infoinfo)
          	root->left=r;
          	else
          	root->right=r;
          	return r;
          	
          }
          if(infoinfo)
          stree(r,r->left,info);
          else
          stree(r,r->right,info);
          return root;
          

          }
          void print_tree(struct tree *r,int l)
          {
          int i;
          if(!r) return ;
          print_tree(r->right,l+1);
          for(i=0;iinfo);
          print_tree(r->left,l+1);

          }

          P Offline
          P Offline
          Patrice T
          wrote on last edited by
          #4

          You should learn to use the debugger as soon as possible. Rather than guessing what your code is doing, It is time to see your code executing and ensuring that it does what you expect. The debugger allow you to follow the execution line by line, inspect variables and you will see that there is a point where it stop doing what you expect. Debugger - Wikipedia, the free encyclopedia[^] Mastering Debugging in Visual Studio 2010 - A Beginner's Guide[^]

          Patrice “Everything should be made as simple as possible, but no simpler.” Albert Einstein

          1 Reply Last reply
          0
          Reply
          • Reply as topic
          Log in to reply
          • Oldest to Newest
          • Newest to Oldest
          • Most Votes


          • Login

          • Don't have an account? Register

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • World
          • Users
          • Groups