Originally Posted by
Salem
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);