I'm looking over my code for my tree and I keep receiving segmentation faults when I try to add or search for value in the tree. Is there something that I am missing in my addNode or newNode function? #include <stdio.h>#include <stdlib.h> // Austin Chong// The core concept of this assignment is to use and create a BST in a C Program.// 7 April 2020 struct node{int data;struct node *left;struct node *right;}; struct node *newNode(int data){struct node *temp = (struct node*)malloc(sizeof(struct node));temp -> data = data;temp -> left = NULL;temp -> right = NULL;return temp;} struct node *addNode(struct node* node, int data){if(node == NULL){return newNode(data);} if(data < node -> data){node -> left = addNode(node -> left, data);} else {node -> right = addNode(node -> right, data);}return node;} void findData(struct node *node, int value){if(node -> left == NULL && node -> right == NULL && node -> data != value) {printf("Value was not found.\n");} else if(node -> data == value) {printf("Value was found.\n");} else if(value < node -> data && node -> left != NULL) {findData(node -> left, value);} else if(value > node -> data && node -> right != NULL) {findData(node -> right, value);}} void preorderTraverse(struct node *node){if(node == NULL){return;}printf("%d -> ", node -> data);preorderTraverse(node -> left);preorderTraverse(node -> right);} int main(){int option;int value;struct node *tree = (struct node*)malloc(sizeof(struct node));while(1){printf("\nPlease select an option below:\n");printf("1. Add an integer to the tree.\n");printf("2. Display the entire tree using preorder traversal.\n");printf("3. Search for a value in the tree.\n");printf("4. Quit.\n");scanf("%d", &option);switch(option){case 1:printf("\nPlease enter an integer value to add to the tree: ");scanf("%d", &value);addNode(tree, value);break;case 2:printf("The preorder traversal result:\n");preorderTraverse(tree);break;case 3:printf("Please enter an integer value: ");scanf("%d", &value);findData(tree, value);break;case 4:printf("Good bye!");exit(0);default:printf("That is not a valid option. Please enter a number 1-4.\n");}}}
I'm looking over my code for my tree and I keep receiving segmentation faults when I try to add or search for value in the tree. Is there something that I am missing in my addNode or newNode function?
#include <stdio.h>
#include <stdlib.h>
// Austin Chong
// The core concept of this assignment is to use and create a BST in a C Program.
// 7 April 2020
struct node
{
int data;
struct node *left;
struct node *right;
};
struct node *newNode(int data)
{
struct node *temp = (struct node*)malloc(sizeof(struct node));
temp -> data = data;
temp -> left = NULL;
temp -> right = NULL;
return temp;
}
struct node *addNode(struct node* node, int data)
{
if(node == NULL)
{
return newNode(data);
}
if(data < node -> data)
{
node -> left = addNode(node -> left, data);
} else {
node -> right = addNode(node -> right, data);
}
return node;
}
void findData(struct node *node, int value)
{
if(node -> left == NULL && node -> right == NULL && node -> data != value) {
printf("Value was not found.\n");
} else if(node -> data == value) {
printf("Value was found.\n");
} else if(value < node -> data && node -> left != NULL) {
findData(node -> left, value);
} else if(value > node -> data && node -> right != NULL) {
findData(node -> right, value);
}
}
void preorderTraverse(struct node *node)
{
if(node == NULL)
{
return;
}
printf("%d -> ", node -> data);
preorderTraverse(node -> left);
preorderTraverse(node -> right);
}
int main()
{
int option;
int value;
struct node *tree = (struct node*)malloc(sizeof(struct node));
while(1)
{
printf("\nPlease select an option below:\n");
printf("1. Add an integer to the tree.\n");
printf("2. Display the entire tree using preorder traversal.\n");
printf("3. Search for a value in the tree.\n");
printf("4. Quit.\n");
scanf("%d", &option);
switch(option)
{
case 1:
printf("\nPlease enter an integer value to add to the tree: ");
scanf("%d", &value);
addNode(tree, value);
break;
case 2:
printf("The preorder traversal result:\n");
preorderTraverse(tree);
break;
case 3:
printf("Please enter an integer value: ");
scanf("%d", &value);
findData(tree, value);
break;
case 4:
printf("Good bye!");
exit(0);
default:
printf("That is not a valid option. Please enter a number 1-4.\n");
}
}
}
Trending now
This is a popular solution!
Step by step
Solved in 3 steps with 2 images