質問

何が二分探索木(BST)のの次の実装が悪いですの?私は挿入関数の引数としてノードをstructへのポインタへのポインタを使用することをお勧めしますと言われています。

struct node
{
int key_value;
struct node* left;
struct node* right;
};

insert(int key, struct node *leaf)
{
    if( leaf == 0 )
    {
        leaf = (struct node*) malloc( sizeof( struct node ) );
        leaf->key_value = key;
        /* initialize the children to null */
        leaf->left = 0;    
        leaf->right = 0;  
    }
    else if(key < leaf->key_value)
    {
        insert( key, leaf->left );
    }
    else if(key > leaf->key_value)
    {
        insert( key, leaf->right );
    }
}
役に立ちましたか?

解決

この行:

leaf = (struct node*) malloc( sizeof( struct node ) );

いくつかの新しく割り当てられたメモリでそれを指して、leafするための新たな価値を提供します。しかし、新しい値は、関数を残しません。関数が戻ると、呼び出し側はまだ古いleafを参照され、メモリリークが発生します。

あなたがそれを固定に取ることができる2つの方法があります。

1 のポインタへのポインタを使用して、例えば

void insert(int key, struct node **leaf)
{
    if(*leaf == 0 )
    {
        *leaf = (struct node*) malloc( sizeof( struct node ) );
        ...
}

/* In caller -- & is prepended to current_leaf. */
insert(37, &current_leaf);

(変更がない場合や、古い葉)2のの新しい葉を返します。

struct node *insert(int key, struct node *leaf)
{
    if(leaf == 0 )
    {
        leaf = (struct node*) malloc( sizeof( struct node ) );
        ...
    }

    return leaf;
}

/* In caller -- & is prepended to current_leaf. */
current_leaf = insert(37, current_leaf);

のポインタへのポインタを理解するのは難しいというのしきい値に近いです。 insertは現在、他の何かを返すされていない場合、私はおそらく、目のオプションのために行くだろう。

他のヒント

ノードが(葉== 0)に挿入されると、新しいノードが孤児となりますので、

、あなたは、その親を変更しませんでした。

つまり、あなたのツリーはまだ挿入して呼び出されたどのように多くのノードに関係なく、一つのノードのみのように見えません。

  #include<stdio.h>
     typedef struct tnode{
       int data;
       struct tnode *left,*right;
     }TNODE;
     TNODE * createTNode(int key){
       TNODE *nnode;
       nnode=(TNODE *)malloc(sizeof(TNODE));    
       nnode->data=key;
       nnode->left=NULL;
       nnode->right=NULL;
      return nnode;
}

    TNODE * insertBST(TNODE *root,int key){
     TNODE *nnode,*parent,*temp;
     temp=root;
      while(temp){
        parent=temp;
        if(temp->data > key)
            temp=temp->left;
        else
            temp=temp->right;    
    }    
     nnode=createTNode(key);
    if(root==NULL)
        root=nnode;
    else if(parent->data>key)
        parent->left=nnode;
    else
        parent->right=nnode;
    return root;
}

     void preorder(TNODE *root){
       if(root){
         printf("%5d",root->data);    
         preorder(root->left);
         preorder(root->right);
       }    
     }  

    void inorder(TNODE *root){
       if(root){
        inorder(root->left);
        printf("%5d",root->data);    
        inorder(root->right);
      }    
    }

    void postorder(TNODE *root){
       if(root){
        postorder(root->left);    
        postorder(root->right);
        printf("%5d",root->data);
      }    
    }

     main(){
       TNODE *root=NULL;
       int ch,key;
       do{
         printf("\n\n1-Insert\t2-Preorder\n3-Inorder\t4-Postorder\n5-Exit\n");
         printf("Enter Your Choice: ");
         scanf("%d",&ch);  

        switch(ch){ 
            case 1:
                printf("Enter Element: ");
                scanf("%d",&key);
                root=insertBST(root,key);
                break;
            case 2:
                preorder(root);
                break;
            case 3:
                inorder(root);
                break;
            case 4:
                postorder(root);
                break;
            default:
                printf("\nWrong Choice!!");
        }

    }while(ch!=5);
    getch();
    return 0;
}
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top