Search code examples
cdata-structuresbinary-tree

My C code doesn't work, it's related to binary trees


I want to design a function in C that takes a binary tree as input and determines if the binary tree is symmetric around its center. It doesn't let me input data correctly. In the main function, it doesn't take the input requested and when executed the right side input is never asked .

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

// Structure for binary tree node
struct Node {
    int data;
    struct Node *left;
    struct Node *right;
};

// Function to create a new binary tree node from a given data.
struct Node *newNode(int data) {
    struct Node *node = (struct Node *)malloc(sizeof(struct Node));
    node->data = data;
    node->left = NULL;
    node->right = NULL;
    return node;
}

// Helper function to check if two subtrees are mirror images of each other.
bool isMirror(struct Node *leftSubtree, struct Node *rightSubtree) {
    if (leftSubtree == NULL && rightSubtree == NULL)
        return true;
    if (leftSubtree == NULL || rightSubtree == NULL)
        return false;
    
    return (leftSubtree->data == rightSubtree->data) &&
           isMirror(leftSubtree->left, rightSubtree->right) &&
           isMirror(leftSubtree->right, rightSubtree->left);
}

// Function to determine if the tree is symmetric.
bool isSymmetric(struct Node *root) {
    if (root == NULL)
        return true;
    
    return isMirror(root->left, root->right);
}

// Function to build a binary tree based on user input.
struct Node *buildTree() {
    int data;
    printf("Enter data (-1 for NULL): ");
    scanf("%d", &data);
    
    if (data == -1)
        return NULL;
    
    struct Node *newNodePtr = newNode(data);
    printf("Enter left child of %d:\n", data);
    newNodePtr->left = buildTree();
    printf("Enter right child of %d:\n", data);
    newNodePtr->right = buildTree();
    
    return newNodePtr;
}

int main() {
    struct Node *root = NULL;
    printf("Enter the root data:\n");
    root = buildTree();

    if (isSymmetric(root))
        printf("The binary tree is symmetric.\n");
    else
        printf("The binary tree is not symmetric.\n");

    return 0;
}

Solution

  • The code seems not to blame for the problem you describe, but there is an issue in buildTree(): you do not check the return value of scanf("%d", &data). If scanf successfully converts input as an integer, it will return 1, otherwise it may return 0 or EOF and the value of data may still be uninitialized or indeterminate. You should test for success and either report the error or consider EOF to mean end of input, hence all remaining nodes should be null.

    
    // Function to build a binary tree based on user input.
    struct Node *buildTree(void) {
        int data;
    
        printf("Enter data (-1 for NULL): ");
        switch (scanf("%d", &data)) {
          case EOF:
            return NULL;
          case 0:
            printf("invalid input\n");
            exit(1);
          case 1:
            if (data == -1)
              return NULL;
            break;
        }
        struct Node *newNodePtr = newNode(data);
        printf("Enter left child of %d:\n", data);
        newNodePtr->left = buildTree();
        printf("Enter right child of %d:\n", data);
        newNodePtr->right = buildTree();
        return newNodePtr;
    }