0

I tried to implement a binary tree by using an iterative function to create it. I'm quite confused about why I'm getting an infinite loop as my output. I believe that the function being used is not having problems. But if anyone can explain me what exactly is causing this error I would appreciate it.

#include<stdio.h>
#include<stdlib.h>

typedef struct node{
    int data;
    struct node *left,*right;
} NODE;

NODE* createNode(int ele){
    NODE* newnode = (NODE*)malloc(sizeof(NODE));
    newnode->data = ele;
    newnode->left = NULL;
    newnode->right = NULL;
    return newnode;
}

void inorder(NODE* root){
    while(root != NULL){
        inorder(root->left);
        printf(" %d ",root->data);
        inorder(root->right);
    }
}


void preorder(NODE* root){
    while(root != NULL){
        printf(" %d ",root->data);
        preorder(root->left);
        preorder(root->right);
    }
}

void postorder(NODE* root){
    while(root != NULL){
        postorder(root->left);
        postorder(root->right);
        printf(" %d ",root->data);
    }
}


void createTree(NODE** root, int ele){
    NODE* newnode = createNode(ele);
    if(*root == NULL){
        //set newnode as root
        *root = newnode;
        return;
    }
    NODE* curr = *root;
    while (1){
        // If the data of the new node is less than the data of the current node,
    // go to the left child
        if(ele <  curr->data){
            // If the left child is empty, insert the new node here
            if(curr->left == NULL){
                curr->left = newnode;
                return;
            }
            curr = curr->left;
        }
            // If the data of the new node is greater than or equal to the data of the current node,
    // go to the right child
        else{
            if (curr->right == NULL){
                curr->right = newnode;
                return; 
            }
            curr = curr->right;
        }
    }
}

int main(){
    NODE* root = NULL;
    int data;
    while(1){
    printf("Enter the data for the root node of the binary tree(Enter -1 to stop): \n");
    scanf("%d",&data);
    createTree(&root,data);
    if(data == -1){
        break;
    }
}
    printf("\nPreorder traversal:\n");
    preorder(root);

    printf("\nInorder traversal:\n");
    inorder(root);

    
    printf("\nPostorder traversal:\n");
    postorder(root);
    
    return 0;
}
1
  • A true BST does not store duplicates. 'Left child nodes' are all less than the parent and 'right child nodes' are all greater than the parent... Commented Dec 26, 2022 at 7:37

1 Answer 1

3

In the print functions, the line

while(root != NULL){

will cause endless looping as root never change.

You probably want

if(root != NULL){
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.