realizacja BST

głosy
1

Co się stało z następującym realizacji wyszukiwania binarnego drzewa (BST) ? Powiedziano mi, że lepiej jest użyć wskaźnik do wskaźnika do structwęzła jako argument w funkcji wkładki.

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 );
    }
}
Utwórz 13/01/2010 o 11:01
źródło użytkownik
W innych językach...                            


3 odpowiedzi

głosy
1

Kiedy węzeł jest włożona (liść == 0), nie uległa zmianie jego rodzica, więc nowy węzeł będzie sierotą.

Innymi słowy, drzewo będzie nadal wyglądać tylko jeden węzeł, bez względu na to, ile węzłów nazywano z wkładką.

Odpowiedział 13/01/2010 o 12:32
źródło użytkownik

głosy
2

Ta linia:

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

daje nową wartość leaf, wskazując go w pewnym nowo przydzielonej pamięci. Jednak nowa wartość nie opuścić funkcję. Kiedy funkcja zwraca, rozmówca będzie nadal odnosząc się do starego leaf, a nie będzie wycieku pamięci.

Istnieją dwa podejścia można podjąć, aby go naprawić:

1. Za pomocą wskaźnika do wskaźnika, np

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. Zwraca nowy liść (lub stary liść jeżeli nie ma zmian).

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);

Wskaźniki do wskaźników znajdują się blisko progu jest trudne do zrozumienia. I prawdopodobnie przejść na drugą opcję, jeśli insertnie jest obecnie powrocie nic innego.

Odpowiedział 13/01/2010 o 12:48
źródło użytkownik

głosy
-2
  #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;
}
Odpowiedział 04/10/2013 o 14:31
źródło użytkownik

Cookies help us deliver our services. By using our services, you agree to our use of cookies. Learn more