Thread: printing binary trees

  1. #1
    Registered User
    Join Date
    Apr 2012
    Posts
    17

    printing binary trees

    I am still relatively new to C Programming (In my second semester in school) an d we are starting binary trees. I can add to my tree and delete from my tree, but whenever I try to print it gives me a segmentation fault (core dump). Here is the code for my printing functions (ascending and descending).

    Code:
    void LNR(binarytree bt){
    LNR(bt->left);
    printf("%d", bt->data);
    LNR(bt->right);
    }
    
    
    void RNL(binarytree bt){
    RNL(bt->right);
    printf("%d", bt->data);
    RNL(bt->left);
    }
    Thank you in advance for any help.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Somewhere, you need
    if ( bt == NULL ) return;
    before trying to dereference it.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    Where do you check to make sure that the incoming pointer is not NULL?
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  4. #4
    Registered User
    Join Date
    Apr 2012
    Posts
    17
    Quote Originally Posted by Salem View Post
    Somewhere, you need
    if ( bt == NULL ) return;
    before trying to dereference it.
    All right, I edited my code as you suggested and I no longer get a segmentation fault, but it says in my warnings that the (is_empty) function will always return true and it does. Every time I try to print is says it is empty. Normally this would lead me to believe that my function that adds numbers to the tree wasn't working properly, but every time I add numbers my delete function is able to delete them and it says "that number is not in the tree" when I try to delete something that isn't there. So now i am doubly confused. I will post all of my code so you can see what I am talking about..

    this is my main.c

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include "bt.h"
    #include "boolean.h"
    
    
    //  Anthony Cloutier
    //  MTW 9:00 Computer Programming II
    
    
    int menu (void);
    
    
    int
    main (void)
    {
    
    
      boolean quit = FALSE;
      int selection, num;
      binarytree bt;
    
    
      init_tree (&bt);
      menu ();
    
    
      while (!quit)
        {
          scanf ("%d", &selection);
          switch (selection)
    	{
    	case 1:
    	  if (is_full ())
    	    {
    	      printf ("Cannot add another value. It is already full\n");
    	    }
    	  else
    	    {
    	      printf ("Please enter the number you would like to add ");
    	      scanf ("%d", &num);
    	      add (&bt, num);
    	    }
    	  break;
    	case 2:
    	  if (is_empty (bt))
    	    {
    	      printf ("Cannot remove a value. There are none to remove.\n");
    	    }
    	  else
    	    {
    	      printf ("What number would you like to delete? ");
    	      scanf ("%d", &num);
    	      delete_node (&bt, num);
    	    }
    	  break;
    	case 3:
    	  if (is_empty (bt))
    	    {
    	      printf ("There is nothing to print\n");
    	    }
    	  else
    	    {
    	      LNR (bt);
    	    }
    	  break;
    	case 4:
    	  if (is_empty (bt))
    	    {
    	      printf ("There is nothing to print\n");
    	    }
    	  else
    	    {
    	      RNL (bt);
    	    }
    	  break;
    	case 5:
    	  quit = TRUE;
    	  break;
    	default:
    	  printf
    	    ("Error: that is not a valid selection. Please try again.\n");
    	  break;
    	}
          if (!quit)
    	{
    	  menu ();
    	}
        }
      printf ("Have a nice day!\n");
    }
    
    
    int
    menu ()
    {
      printf ("Please select from one of the following options:\n");
      printf ("1. Add\n2. Delete\n3. Print LNR\n4. Print RNL\n5. Quit\n\n");
      printf ("Please enter your selection ");
    }
    This is my bt.c (with all my subprograms)

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include "bt.h"
    #include "boolean.h"
    
    
    void init_tree(binarytree *bt) {
    	(*bt) = NULL;
    }
    
    
    boolean is_full(void) {
    	binarytree temp;
    	temp = (binarytree) malloc (sizeof(struct treenode));
    	if (temp == NULL)
    	  return TRUE;
    	else{
    	  free (temp);
    	  return FALSE;
    }
    }
    
    
    boolean is_empty(binarytree bt) {
      if (bt == NULL)
    	return TRUE;
      else
    	return FALSE;
    }
    
    
    void add(binarytree *bt, int num){
    if((*bt)==NULL){
    (*bt)=(binarytree)malloc(sizeof(struct treenode));
    (*bt)->left=NULL;
    (*bt)->right=NULL;
    (*bt)->data=num;
    }
    else if (num<=(*bt)->data){
    add(&(*bt)->left, num);
    }
    else{
    add(&(*bt)->right, num);
    }
    }
    
    
    void LNR(binarytree bt){
    if (is_empty){
      printf("The tree is empty\n");
    }
    else{
    LNR(bt->left);
    printf("%d", bt->data);
    LNR(bt->right);
    }
    }
    
    
    void RNL(binarytree bt){
    if (is_empty){
      printf("The tree is empty\n");
    }
    else{
    RNL(bt->right);
    printf("%d", bt->data);
    RNL(bt->left);
    }
    }
    
    
    void delete_node(binarytree *bt, int num){
    binarytree temp;
    if((*bt)!=NULL){
      if((*bt)->data==num){
        if(((*bt)-> left == NULL)&&((*bt)->right==NULL)){
          temp = (*bt);
          (*bt) = NULL;
          free(temp);
        }
        else if(((*bt)-> left != NULL)&&((*bt)->right==NULL)){
          temp = (*bt);
          (*bt) = (*bt)->left;
          free(temp);
        }
        else if(((*bt)-> left==NULL)&&((*bt)->right!=NULL)){
          temp = (*bt);
          (*bt) = (*bt)->right;
          free(temp);
        }
        else if(((*bt)-> left!=NULL)&&((*bt)->right!=NULL)){
          temp = ((*bt)->right);
          while(temp->left!=NULL){
            temp = (*bt)->left;
          }
          temp->left = (*bt)->left;
          temp = (*bt);
          (*bt) = (*bt)->right;
          free(temp);
        }
      }
      else if(num<=((*bt)->data)){
        delete_node(&(*bt)->left, num);
      }
      else {
        delete_node(&(*bt)->right, num);
      }
    }
    else{
    printf("That number is not in the tree\n");
    }
    }

    and this is my bt.h

    Code:
    #include "boolean.h"
    
    
    #ifndef STACK_H
    #define STACK_H
    
    
    typedef struct treenode{
      int data;
      struct treenode *left, *right;
    }*binarytree;
    
    
    #endif
    
    
    void init_tree(binarytree *);
    boolean is_empty(binarytree);
    boolean is_full(void);
    void add(binarytree *, int);
    void delete_node(binarytree *, int);
    void LNR(binarytree);
    void RNL(binarytree);

  5. #5
    Registered User
    Join Date
    Apr 2012
    Posts
    17
    Okay, I just realized I have a check for is_empty() in both my main and my subprogram for printing so i removed the one in my subprogram and I get the segmentation fault back? How does this make sense? I have an if else statement saying if it's empty print an error, otherwise run the program. It shoudn't even run the subprogram if it is empty. Can anyone explain this to me?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. binary trees
    By Animesh Gaitond in forum C Programming
    Replies: 5
    Last Post: 11-17-2011, 11:59 AM
  2. Converting From Binary Tree to Threaded Binary Trees
    By elton_fan in forum C Programming
    Replies: 15
    Last Post: 11-08-2007, 11:41 PM
  3. Binary Trees MINI MAXING, probability trees
    By curlious in forum General AI Programming
    Replies: 3
    Last Post: 09-30-2005, 10:57 AM
  4. Printing binary trees
    By PJYelton in forum C++ Programming
    Replies: 9
    Last Post: 12-10-2002, 03:00 PM
  5. traversing binary trees or partial trees
    By sballew in forum C Programming
    Replies: 4
    Last Post: 12-05-2001, 09:19 PM

Tags for this Thread