I seem to be having an issue where when I am assigning values into my binary search tree, and the root will take in the first value entered, but then after that nothing else is entered. If I go and directly take a look at root -> left or root -> right it just returns with null. I've stared at this so long I'm at my wits end. I'm sure its something really basic wrong with my recursion, but I just can't see it. I'd really appreciate any help seeing where I went wrong here.
#include <stdio.h>
#include <stdlib.h>
#include "Bst.h"
int main(int argc, char** argv) {
int value;
TreeNode* root = NULL;
printf ("Enter an integer\n");
scanf ("%d", &value);
while (value > 0) {
root = insert (value, root);
printf ("Enter an integer\n");
scanf ("%d", &value);
}
printf("The inorder traversal of the tree\n");
inOrder(root);
printf("\n");
printf("The preorder traversal of the tree\n");
preOrder(root);
printf("\n");
return (EXIT_SUCCESS);
}
TreeNode* insert(int newValue, TreeNode* root) {
TreeNode* temp = NULL;
//Sets a value to the root
if (root == NULL) {
temp = (TreeNode*)malloc(sizeof(TreeNode));
temp -> value = newValue;
temp -> left = NULL;
temp -> right = NULL;
return temp;
}
//Will put a larger value to the right within the tree
else if (newValue > (root -> value)) {
temp = insert(newValue, (root -> right));
}
//Will put a smaller value to the left
else {
temp = insert (newValue, (root -> left));
}
return root;
}
void inOrder(TreeNode* root){
if(root == NULL){
return;
}
else {
inOrder(root -> left);
printf("%d", root -> value);
printf(" ");
inOrder(root -> right);
}
return;
}
void preOrder(TreeNode* root){
if(root == NULL) {
return;
} else {
preOrder(root -> right);
printf("%d", root -> value);
printf(" ");
preOrder(root -> left);
}
return;
}
Change:
temp = insert(newValue, (root -> right));
to
root->right = insert(newValue, (root -> right));
Also change the left version the same way. Right now you are allocating the child nodes but never assigning them to the right or left pointers. They are essentially getting thrown away.
Pretty sure the error is that you assign the newly inserted right and left nodes to temp
and not to root->right
and root->left
which leaves them dangling outside the tree.
Change these lines:
//Will put a larger value to the right within the tree
else if (newValue > (root -> value)){
temp = insert(newValue, (root -> right));
}
//Will put a smaller value to the left
else{
temp = insert (newValue, (root -> left));
}
To this:
//Will put a larger value to the right within the tree
else if (newValue > (root -> value)) {
root->right = insert(newValue, (root -> right));
}
//Will put a smaller value to the left
else {
root->left = insert (newValue, (root -> left));
}
This change makes your code work as expected when I tested it; both inOrder and preOrder gave correct results.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.