typedef struct BST {
    int key;
    BST *left;
    BST *right;
}BST;
BST *root;

void insert(BST *ROOT,int key)
{
    BST *p,*f;
    p = ROOT;
    f = NULL;
    while ( p != NULL )
    {
        if ( key < p->key )
        {
            f = p;
            p = p->left;
        }
        else
        {
            f = p;
            p = p->right;
        }
    }
    
    BST *tmp;
    if ( f == NULL )   
    {
        root = (BST *)malloc(sizeof(BST));
        root->key = key;
        root->left = root->right = NULL;
    }
    else if ( key < f->key )
    {
        tmp = (BST *)malloc(sizeof(BST));
        f->left = tmp;
        tmp->key = key;
        tmp->left = tmp->right = NULL;
    }
    else
    {
        tmp = (BST *)malloc(sizeof(BST));
        f->right = tmp;
        tmp->key = key;
        tmp->left = tmp->right = NULL;
    }
}

 

posted on 2012-12-21 21:19  Sinker  阅读(131)  评论(0编辑  收藏  举报