segmentation fault in implementing traverse for binary search tree
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ height:90px;width:728px;box-sizing:border-box;
}
I am writing a part of a program in which it traverses a binary tree until it finds the intended item in the tree (assuming that the item we are looking for always exists in the tree). The method I have adopted for searching in a tree is preorder tree walk.
Each node contains either a question or a statement. If the node is statement, it has no children. However, if the node is a question, it has exactly two children. In the follow you can see the header file for creating a node.
// two type of branch in the tree
enum response{
YES,
NO
};
// content of a node
union objInfoOrQInfo {
char * object;
char * question;
};
struct node{
union objInfoOrQInfo container;
// enum response existLeftChild;
// enum response existRightChild;
struct node * rightChild;
struct node * leftChild;
};
void nodePrint(char * string);
In the following code, I have populated a tree and implemented the pre-order tree walk
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include "node.h"
struct node * traverse(struct node * searchingNode, char * searchContent);
void nodePrint(char * string)
{
struct node root;
struct node left;
struct node right;
//second level
struct node leftLeft;
struct node leftRight;
struct node rightLeft;
struct node rightRight;
// Populating the tree
root.container.question = "Does it have a tail?";
root.leftChild = &left;
root.rightChild = &right;
left.container.question = "Does it like a chase mice?";
left.leftChild = &leftLeft;
left.leftChild = &leftRight;
right.container.question = "Is it flat, round and edible?";
right.leftChild = &rightLeft;
right.rightChild = &rightRight;
leftLeft.container.object = "A Cat";
leftLeft.leftChild = NULL;
leftLeft.rightChild = NULL;
leftRight.container.object = "A Pangolin";
leftRight.leftChild = NULL;
leftRight.rightChild = NULL;
rightLeft.container.object = "A Pizza";
rightLeft.leftChild = NULL;
rightLeft.rightChild = NULL;
rightRight.container.object = "Pete";
rightLeft.leftChild = NULL;
rightLeft.rightChild = NULL;
// It needs to traverse from the root until it finds the content you are asking for
struct node * result = traverse(&root, string);
if(result->leftChild != NULL || result->rightChild != NULL)
{
printf("Object: %sn","[NOTHING]");
printf("Question: %sn",result->container.question);
if(result->leftChild->container.object == NULL)
printf("Yes: %sn",result->leftChild->container.question);
else
printf("Yes: %sn",result->leftChild->container.object);
if(result->leftChild->container.object == NULL)
printf("Yes: %sn",result->rightChild->container.question);
else
printf("Yes: %sn",result->rightChild->container.object);
}
else
{
printf("Object: %sn","[NOTHING]");
printf("Question: %sn",result->container.question);
}
}
// This function traverse from the root until it finds the desired content (return 1), otherwise (return -1)
struct node * traverse(struct node * searchingNode, char * searchContent) {
// If the node is found, just return it.
if (strcmp(searchingNode->container.question, searchContent) == 0)
return searchingNode;
// If the node is not found and we are at a leaf, return NIL
if (searchingNode->leftChild == NULL && searchingNode->rightChild == NULL)
return NULL;
// Searching in the left and right subtrees, respectively.
struct node *leftSubTree = traverse(searchingNode->leftChild, searchContent);
struct node *rightSubTree;
if(leftSubTree == NULL)
rightSubTree = traverse(searchingNode->rightChild, searchContent);
// Calculating the final result
if (leftSubTree == NULL && rightSubTree == NULL)
return NULL;
else if (leftSubTree == NULL)
return rightSubTree;
return leftSubTree;
}
In order to search for a node, I need to call the nodePrint() with passing the appropriate argument to it in the main function as follow:
int main() {
nodePrint("A Pizza");
return 0;
}
If the node data is a question, the expected result should be:
Object: [NOTHING]
Question: Is it flat, round and edible?
Yes: A Pizza
Yes: Pete
and if the data is a statement, the output should be:
Object: [NOTHING]
Question: A Pizza
My program shows the appropriate result, but there is a SEGFAULT that I cannot figure out what causes it. I really appreciate if someone could help me to debug it.
c
add a comment |
I am writing a part of a program in which it traverses a binary tree until it finds the intended item in the tree (assuming that the item we are looking for always exists in the tree). The method I have adopted for searching in a tree is preorder tree walk.
Each node contains either a question or a statement. If the node is statement, it has no children. However, if the node is a question, it has exactly two children. In the follow you can see the header file for creating a node.
// two type of branch in the tree
enum response{
YES,
NO
};
// content of a node
union objInfoOrQInfo {
char * object;
char * question;
};
struct node{
union objInfoOrQInfo container;
// enum response existLeftChild;
// enum response existRightChild;
struct node * rightChild;
struct node * leftChild;
};
void nodePrint(char * string);
In the following code, I have populated a tree and implemented the pre-order tree walk
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include "node.h"
struct node * traverse(struct node * searchingNode, char * searchContent);
void nodePrint(char * string)
{
struct node root;
struct node left;
struct node right;
//second level
struct node leftLeft;
struct node leftRight;
struct node rightLeft;
struct node rightRight;
// Populating the tree
root.container.question = "Does it have a tail?";
root.leftChild = &left;
root.rightChild = &right;
left.container.question = "Does it like a chase mice?";
left.leftChild = &leftLeft;
left.leftChild = &leftRight;
right.container.question = "Is it flat, round and edible?";
right.leftChild = &rightLeft;
right.rightChild = &rightRight;
leftLeft.container.object = "A Cat";
leftLeft.leftChild = NULL;
leftLeft.rightChild = NULL;
leftRight.container.object = "A Pangolin";
leftRight.leftChild = NULL;
leftRight.rightChild = NULL;
rightLeft.container.object = "A Pizza";
rightLeft.leftChild = NULL;
rightLeft.rightChild = NULL;
rightRight.container.object = "Pete";
rightLeft.leftChild = NULL;
rightLeft.rightChild = NULL;
// It needs to traverse from the root until it finds the content you are asking for
struct node * result = traverse(&root, string);
if(result->leftChild != NULL || result->rightChild != NULL)
{
printf("Object: %sn","[NOTHING]");
printf("Question: %sn",result->container.question);
if(result->leftChild->container.object == NULL)
printf("Yes: %sn",result->leftChild->container.question);
else
printf("Yes: %sn",result->leftChild->container.object);
if(result->leftChild->container.object == NULL)
printf("Yes: %sn",result->rightChild->container.question);
else
printf("Yes: %sn",result->rightChild->container.object);
}
else
{
printf("Object: %sn","[NOTHING]");
printf("Question: %sn",result->container.question);
}
}
// This function traverse from the root until it finds the desired content (return 1), otherwise (return -1)
struct node * traverse(struct node * searchingNode, char * searchContent) {
// If the node is found, just return it.
if (strcmp(searchingNode->container.question, searchContent) == 0)
return searchingNode;
// If the node is not found and we are at a leaf, return NIL
if (searchingNode->leftChild == NULL && searchingNode->rightChild == NULL)
return NULL;
// Searching in the left and right subtrees, respectively.
struct node *leftSubTree = traverse(searchingNode->leftChild, searchContent);
struct node *rightSubTree;
if(leftSubTree == NULL)
rightSubTree = traverse(searchingNode->rightChild, searchContent);
// Calculating the final result
if (leftSubTree == NULL && rightSubTree == NULL)
return NULL;
else if (leftSubTree == NULL)
return rightSubTree;
return leftSubTree;
}
In order to search for a node, I need to call the nodePrint() with passing the appropriate argument to it in the main function as follow:
int main() {
nodePrint("A Pizza");
return 0;
}
If the node data is a question, the expected result should be:
Object: [NOTHING]
Question: Is it flat, round and edible?
Yes: A Pizza
Yes: Pete
and if the data is a statement, the output should be:
Object: [NOTHING]
Question: A Pizza
My program shows the appropriate result, but there is a SEGFAULT that I cannot figure out what causes it. I really appreciate if someone could help me to debug it.
c
add a comment |
I am writing a part of a program in which it traverses a binary tree until it finds the intended item in the tree (assuming that the item we are looking for always exists in the tree). The method I have adopted for searching in a tree is preorder tree walk.
Each node contains either a question or a statement. If the node is statement, it has no children. However, if the node is a question, it has exactly two children. In the follow you can see the header file for creating a node.
// two type of branch in the tree
enum response{
YES,
NO
};
// content of a node
union objInfoOrQInfo {
char * object;
char * question;
};
struct node{
union objInfoOrQInfo container;
// enum response existLeftChild;
// enum response existRightChild;
struct node * rightChild;
struct node * leftChild;
};
void nodePrint(char * string);
In the following code, I have populated a tree and implemented the pre-order tree walk
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include "node.h"
struct node * traverse(struct node * searchingNode, char * searchContent);
void nodePrint(char * string)
{
struct node root;
struct node left;
struct node right;
//second level
struct node leftLeft;
struct node leftRight;
struct node rightLeft;
struct node rightRight;
// Populating the tree
root.container.question = "Does it have a tail?";
root.leftChild = &left;
root.rightChild = &right;
left.container.question = "Does it like a chase mice?";
left.leftChild = &leftLeft;
left.leftChild = &leftRight;
right.container.question = "Is it flat, round and edible?";
right.leftChild = &rightLeft;
right.rightChild = &rightRight;
leftLeft.container.object = "A Cat";
leftLeft.leftChild = NULL;
leftLeft.rightChild = NULL;
leftRight.container.object = "A Pangolin";
leftRight.leftChild = NULL;
leftRight.rightChild = NULL;
rightLeft.container.object = "A Pizza";
rightLeft.leftChild = NULL;
rightLeft.rightChild = NULL;
rightRight.container.object = "Pete";
rightLeft.leftChild = NULL;
rightLeft.rightChild = NULL;
// It needs to traverse from the root until it finds the content you are asking for
struct node * result = traverse(&root, string);
if(result->leftChild != NULL || result->rightChild != NULL)
{
printf("Object: %sn","[NOTHING]");
printf("Question: %sn",result->container.question);
if(result->leftChild->container.object == NULL)
printf("Yes: %sn",result->leftChild->container.question);
else
printf("Yes: %sn",result->leftChild->container.object);
if(result->leftChild->container.object == NULL)
printf("Yes: %sn",result->rightChild->container.question);
else
printf("Yes: %sn",result->rightChild->container.object);
}
else
{
printf("Object: %sn","[NOTHING]");
printf("Question: %sn",result->container.question);
}
}
// This function traverse from the root until it finds the desired content (return 1), otherwise (return -1)
struct node * traverse(struct node * searchingNode, char * searchContent) {
// If the node is found, just return it.
if (strcmp(searchingNode->container.question, searchContent) == 0)
return searchingNode;
// If the node is not found and we are at a leaf, return NIL
if (searchingNode->leftChild == NULL && searchingNode->rightChild == NULL)
return NULL;
// Searching in the left and right subtrees, respectively.
struct node *leftSubTree = traverse(searchingNode->leftChild, searchContent);
struct node *rightSubTree;
if(leftSubTree == NULL)
rightSubTree = traverse(searchingNode->rightChild, searchContent);
// Calculating the final result
if (leftSubTree == NULL && rightSubTree == NULL)
return NULL;
else if (leftSubTree == NULL)
return rightSubTree;
return leftSubTree;
}
In order to search for a node, I need to call the nodePrint() with passing the appropriate argument to it in the main function as follow:
int main() {
nodePrint("A Pizza");
return 0;
}
If the node data is a question, the expected result should be:
Object: [NOTHING]
Question: Is it flat, round and edible?
Yes: A Pizza
Yes: Pete
and if the data is a statement, the output should be:
Object: [NOTHING]
Question: A Pizza
My program shows the appropriate result, but there is a SEGFAULT that I cannot figure out what causes it. I really appreciate if someone could help me to debug it.
c
I am writing a part of a program in which it traverses a binary tree until it finds the intended item in the tree (assuming that the item we are looking for always exists in the tree). The method I have adopted for searching in a tree is preorder tree walk.
Each node contains either a question or a statement. If the node is statement, it has no children. However, if the node is a question, it has exactly two children. In the follow you can see the header file for creating a node.
// two type of branch in the tree
enum response{
YES,
NO
};
// content of a node
union objInfoOrQInfo {
char * object;
char * question;
};
struct node{
union objInfoOrQInfo container;
// enum response existLeftChild;
// enum response existRightChild;
struct node * rightChild;
struct node * leftChild;
};
void nodePrint(char * string);
In the following code, I have populated a tree and implemented the pre-order tree walk
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include "node.h"
struct node * traverse(struct node * searchingNode, char * searchContent);
void nodePrint(char * string)
{
struct node root;
struct node left;
struct node right;
//second level
struct node leftLeft;
struct node leftRight;
struct node rightLeft;
struct node rightRight;
// Populating the tree
root.container.question = "Does it have a tail?";
root.leftChild = &left;
root.rightChild = &right;
left.container.question = "Does it like a chase mice?";
left.leftChild = &leftLeft;
left.leftChild = &leftRight;
right.container.question = "Is it flat, round and edible?";
right.leftChild = &rightLeft;
right.rightChild = &rightRight;
leftLeft.container.object = "A Cat";
leftLeft.leftChild = NULL;
leftLeft.rightChild = NULL;
leftRight.container.object = "A Pangolin";
leftRight.leftChild = NULL;
leftRight.rightChild = NULL;
rightLeft.container.object = "A Pizza";
rightLeft.leftChild = NULL;
rightLeft.rightChild = NULL;
rightRight.container.object = "Pete";
rightLeft.leftChild = NULL;
rightLeft.rightChild = NULL;
// It needs to traverse from the root until it finds the content you are asking for
struct node * result = traverse(&root, string);
if(result->leftChild != NULL || result->rightChild != NULL)
{
printf("Object: %sn","[NOTHING]");
printf("Question: %sn",result->container.question);
if(result->leftChild->container.object == NULL)
printf("Yes: %sn",result->leftChild->container.question);
else
printf("Yes: %sn",result->leftChild->container.object);
if(result->leftChild->container.object == NULL)
printf("Yes: %sn",result->rightChild->container.question);
else
printf("Yes: %sn",result->rightChild->container.object);
}
else
{
printf("Object: %sn","[NOTHING]");
printf("Question: %sn",result->container.question);
}
}
// This function traverse from the root until it finds the desired content (return 1), otherwise (return -1)
struct node * traverse(struct node * searchingNode, char * searchContent) {
// If the node is found, just return it.
if (strcmp(searchingNode->container.question, searchContent) == 0)
return searchingNode;
// If the node is not found and we are at a leaf, return NIL
if (searchingNode->leftChild == NULL && searchingNode->rightChild == NULL)
return NULL;
// Searching in the left and right subtrees, respectively.
struct node *leftSubTree = traverse(searchingNode->leftChild, searchContent);
struct node *rightSubTree;
if(leftSubTree == NULL)
rightSubTree = traverse(searchingNode->rightChild, searchContent);
// Calculating the final result
if (leftSubTree == NULL && rightSubTree == NULL)
return NULL;
else if (leftSubTree == NULL)
return rightSubTree;
return leftSubTree;
}
In order to search for a node, I need to call the nodePrint() with passing the appropriate argument to it in the main function as follow:
int main() {
nodePrint("A Pizza");
return 0;
}
If the node data is a question, the expected result should be:
Object: [NOTHING]
Question: Is it flat, round and edible?
Yes: A Pizza
Yes: Pete
and if the data is a statement, the output should be:
Object: [NOTHING]
Question: A Pizza
My program shows the appropriate result, but there is a SEGFAULT that I cannot figure out what causes it. I really appreciate if someone could help me to debug it.
c
c
asked Nov 24 '18 at 12:13
HosseinHossein
3115
3115
add a comment |
add a comment |
0
active
oldest
votes
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53458029%2fsegmentation-fault-in-implementing-traverse-for-binary-search-tree%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
0
active
oldest
votes
0
active
oldest
votes
active
oldest
votes
active
oldest
votes
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53458029%2fsegmentation-fault-in-implementing-traverse-for-binary-search-tree%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown