You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

C++箭头运算符->使用疑问?BST插入逻辑异常导致遍历问题求解

Why does replacing ptr->left=insert(ptr->left, item) with ptr = ptr->left; ptr = insert(ptr, item) break my BST in-order traversal?

I have a simple Binary Search Tree (BST) implementation with only insert and in-order traversal functions:

struct Node{ int data; Node* left; Node* right; };
Node* create(int data){ Node* node = new Node; node -> data = data; node->left=node->right=NULL; return node; }
Node* insert(Node* ptr,int item);
void inorder(Node* ptr);
int main(){ Node* root = NULL; int temp,ch; root = insert(root, 10); root = insert(root, 20); root = insert(root, 30); inorder(root); cout<<endl; }
Node* insert(Node* ptr,int item){ if(ptr==NULL){ cout<<"Inserted "<<item<<endl; return create(item); } if(item<ptr->data){ // ptr = ptr->left; // ptr = insert(ptr, item) ptr->left=insert(ptr->left, item); } else{ // ptr=ptr->right; // ptr = insert(ptr, item) ptr->right=insert(ptr->right, item); } return ptr; }
void inorder(Node* ptr){ if(ptr==NULL) return; inorder(ptr->left); cout<<ptr->data<<" "; inorder(ptr->right); }

When I replace ptr->left=insert(ptr->left, item) with ptr = ptr->left; ptr = insert(ptr, item) (and do the same for the right side), the in-order traversal only prints the last inserted element. Is this a C++ language issue, or am I misunderstanding some core concept here?


This is a misunderstanding of how pointers work in C++, not a language issue

Let's break down why your modified code fails, and why the original approach works:

How the correct code works

The line ptr->left = insert(ptr->left, item) does two critical things:

  1. It calls insert with the current value of ptr->left (which might be NULL if there's no left child yet).
  2. It takes the return value of insert (either the newly created node, or the existing child if the item was already present) and directly updates the left member of the current ptr node. This maintains the link between the parent node and its left subtree.

Why your modified code breaks the tree

When you use ptr = ptr->left; ptr = insert(ptr, item), you're only modifying the local ptr variable inside the insert function, not the actual parent node's pointer member:

  1. ptr = ptr->left changes the local ptr to point to the current left child (or NULL if there isn't one).
  2. ptr = insert(ptr, item) updates this local ptr to point to the newly created node, but this doesn't affect the original parent node's left member— that parent's left is still NULL (or whatever it was before).
  3. To make matters worse, since your insert function returns this local ptr, each time you call root = insert(root, ...) in main, you're overwriting root to point to the last inserted node instead of maintaining the root of the full tree. All previous nodes are effectively lost because they're never linked to the rest of the tree.

For example, when inserting 20 after 10:

  • The original code updates root->right to point to the new 20 node, keeping it connected to the root.
  • Your modified code changes the local ptr (which started as root) to point to root->right (NULL), then sets that local ptr to the new 20 node. But root->right is still NULL, and the function returns the local ptr (20), so root becomes 20. Then inserting 30 repeats this process, overwriting root to 30— leaving you with only a single node, hence the in-order traversal only prints 30.

Key takeaway

In C++, when you want to modify a pointer that's a member of a struct/class (like Node::left), you need to assign directly to that member variable. Changing a local pointer variable inside a function doesn't alter the original struct's members— it just changes where that local variable points.

内容的提问来源于stack exchange,提问作者Rajan

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.09 06:32:50