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");}}}

Database System Concepts
7th Edition
ISBN:9780078022159
Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Chapter1: Introduction
Section: Chapter Questions
Problem 1PE
icon
Related questions
Question

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");
}
}
}

Expert Solution
trending now

Trending now

This is a popular solution!

steps

Step by step

Solved in 3 steps with 2 images

Blurred answer
Knowledge Booster
Linked List Representation
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Similar questions
  • SEE MORE QUESTIONS
Recommended textbooks for you
Database System Concepts
Database System Concepts
Computer Science
ISBN:
9780078022159
Author:
Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:
McGraw-Hill Education
Starting Out with Python (4th Edition)
Starting Out with Python (4th Edition)
Computer Science
ISBN:
9780134444321
Author:
Tony Gaddis
Publisher:
PEARSON
Digital Fundamentals (11th Edition)
Digital Fundamentals (11th Edition)
Computer Science
ISBN:
9780132737968
Author:
Thomas L. Floyd
Publisher:
PEARSON
C How to Program (8th Edition)
C How to Program (8th Edition)
Computer Science
ISBN:
9780133976892
Author:
Paul J. Deitel, Harvey Deitel
Publisher:
PEARSON
Database Systems: Design, Implementation, & Manag…
Database Systems: Design, Implementation, & Manag…
Computer Science
ISBN:
9781337627900
Author:
Carlos Coronel, Steven Morris
Publisher:
Cengage Learning
Programmable Logic Controllers
Programmable Logic Controllers
Computer Science
ISBN:
9780073373843
Author:
Frank D. Petruzella
Publisher:
McGraw-Hill Education