C语言二叉树代码求助:输入触发中序打印后无输出
Hey there! Let's get to the bottom of why your binary tree isn't producing any output when you input 5 4 6 0. The root cause is how you're handling the root node during insertion—C's pointer passing behavior is tripping you up here. Let's break this down and fix it.
The Core Problem
Your insertNode function takes a TreeNode* node parameter, which is passed by value. That means when you pass your root pointer (initially NULL) to this function, you're working with a copy of the pointer inside the function. When you create a new node and assign it to node, you're only updating that local copy—not the actual root pointer in your main function. So your root stays NULL forever, and the in-order traversal has nothing to print.
The Fix
We need to make sure the insertion updates the actual root pointer. There are two common ways to do this:
1. Return the Updated Node Pointer
Modify insertNode to return the new (or existing) node pointer, then assign this back to your root in the main function. This is the more intuitive approach for binary tree insertions.
Here's the full corrected code, including a proper in-order traversal function (I'm assuming you might have omitted this part):
#include <stdio.h> #include <stdlib.h> typedef struct TreeNode TreeNode; struct TreeNode{ int data; TreeNode* left; TreeNode* right; }; TreeNode* newTree(){ return NULL; } // Updated insert function that returns the modified node TreeNode* insertNode(TreeNode* node, int value){ // If current node is NULL, create a new node if(node == NULL){ TreeNode* newNode = (TreeNode*)malloc(sizeof(TreeNode)); newNode->data = value; newNode->left = NULL; newNode->right = NULL; return newNode; } // Recursively insert into left or right subtree if(value < node->data){ node->left = insertNode(node->left, value); } else if(value > node->data){ node->right = insertNode(node->right, value); } // Ignore duplicate values (adjust this logic if needed) return node; } // In-order traversal function to print nodes void inorderTraversal(TreeNode* node){ if(node != NULL){ inorderTraversal(node->left); printf("%d ", node->data); inorderTraversal(node->right); } } int main(){ TreeNode* root = newTree(); int value; while(1){ scanf("%d", &value); if(value == 0){ break; } // Critical: Update the root with the returned pointer root = insertNode(root, value); } inorderTraversal(root); printf("\n"); // Note: For a complete program, add code to free allocated memory here return 0; }
2. Use a Double Pointer
Alternatively, you can pass a pointer to your root pointer (TreeNode** node) so the function can modify the original pointer directly:
void insertNode(TreeNode** node, int value){ if(*node == NULL){ *node = (TreeNode*)malloc(sizeof(TreeNode)); (*node)->data = value; (*node)->left = NULL; (*node)->right = NULL; return; } if(value < (*node)->data){ insertNode(&((*node)->left), value); } else if(value > (*node)->data){ insertNode(&((*node)->right), value); } } // In main, call it like this: insertNode(&root, value);
Testing the Fix
When you input 5 4 6 0 now, the program will correctly build the tree and output 4 5 6 as expected.
Key Takeaways
- In C, pointers are passed by value—modifying a pointer parameter inside a function won't change the original pointer in the caller.
- For tree insertions, always ensure you're updating the actual root (or parent) pointers, either by returning the modified pointer or using double pointers.
- Don't forget to implement the in-order traversal function (it's easy to overlook when focusing on insertion logic!).
内容的提问来源于stack exchange,提问作者Pedro Rossini Sardelich

