1

This is an operation to inert X into a BinTree, what confuses me is that why the BST->Left can not be changed in step①,however, it can be changed in step② or ster③,I think these three steps are all in the function, why there is a difference between them, I will appreciate it if someone could help me!

 BinTree Insert( BinTree BST, ElementType X ){
    if(!BST){
        BST=(BinTree)malloc(sizeof(struct TNode));
        BST->Data=X;①
        BST->Left=NULL;
        BST->Right=NULL;
    }else{
        if(X<BST->Data){
            **BST->Left=**Insert(BST->Left,X);②
        }else{
            **BST->Right=**Insert(BST->Right,X);③
    }
    return BST;
} 
Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
陈润泽
  • 13
  • 4
  • 1
    What makes you think that "the BST->Left can not be changed in step①"? – Yunnosch Mar 10 '20 at 10:17
  • 1
    If you're going to use a typedef in presented code, you need to show the typedef. Best solution is to *not* use typedefs. – William Pursell Mar 10 '20 at 10:18
  • With `BST = malloc(sizeof(struct TNode))` there is nothing posted to confirm you allocated the right amount of memory. – Weather Vane Mar 10 '20 at 10:19
  • 1
    In steps 2 and 3, are those asterisks part of your code, or just an attempt at highlighting? – William Pursell Mar 10 '20 at 10:21
  • The code isn't exactly clear, you are overwriting node pointer with the same or a new value. – Weather Vane Mar 10 '20 at 10:24
  • If the `BinTree` is `NULL`, then this function will create and return a newly allocated instance pointed by `BinTree`. The original parameter will *not* be changed since parameters are passed by value. So either change the function signature to accept a pointer to `BinTree`, or create a differentiation between a `BinTree` and a `BinNode`, where `BinTree` will contain only the head `BinNode`. – vgru Mar 10 '20 at 10:46
  • I am sorry that I have not asked the question clearly, it is the first time to ask in StackOverflow, thank you for solving my question, I have found where the problem is! thank you very much! – 陈润泽 Mar 10 '20 at 11:21

1 Answers1

0

The function deals with a copy of the pointer to the root node of Binary Search Tree. Changing the copy in this statement

    BST=(BinTree)malloc(sizeof(struct TNode));

does not influence on the original pointer passed to the function as an argument.You have to assign the returned pointer from the function to the original pointer to the root node of the Binary Search Tree like for example

BinTree root = NULL;
root = Insert( root, X );

In these statements

        BST->Left  = Insert(BST->Left,X);
        BST->Right = Insert(BST->Right,X);

you are doing the assignment of data ,members Left and Right that are passed to thj function by reference through the pointer BST.

Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
  • Thank you very much for solving my problem! I still have confusion that if you mean that the "malloc" statements make step① can not change the BST->Left? – 陈润泽 Mar 10 '20 at 11:05
  • @陈润泽 It can not change BST->Left of the original pointer passed to the function as an argument because a copy of the pointer is created and the function deals with a copy of the original pointer. – Vlad from Moscow Mar 10 '20 at 11:07