二叉排序树(字符串)

来源:互联网 发布:mac 终端切换root用户 编辑:程序博客网 时间:2024/06/07 05:36

#include <stdio.h>
#include <stdlib.h>

typedef int  datatype;

struct bi_search_tree
{
    char* key;
    struct bi_search_tree *left,*right;
};

typedef struct bi_search_tree bst_tree;

bst_tree *bst_insert(bst_tree *root, char* value);
int bst_search(bst_tree *root, char* value);
int bst_delete(bst_tree *root, char* value);
void bst_print(bst_tree *root);

/*插入操作,value是待插入的值*/
bst_tree *bst_insert(bst_tree *root, char* value)
{
    bst_tree *parent, *node, *child;
    /*树为空,创建根节点*/
    if(root == NULL)
    {
        root = (bst_tree *)malloc(sizeof(bst_tree));
        root->key = value;
        root->left = NULL;
        root->right = NULL;
        return root;
    }
   
    parent = root;    /*记录下根节点的位置*/
    node = root;
    while(node != NULL)
    {
        /*待插入数据已经存在,则返回*/
        if(node->key == value)
            return parent;
        else
        {
            parent = node;
            /*若小于节点的值,则查看节点的左孩子,否则,查看右孩子*/
            if(node->key < value)
                node = node->right;
            else
                node = node->left;
        }
    }

    child = (bst_tree *)malloc(sizeof(bst_tree));
    child->key = value;
    child->left = NULL;
    child->right = NULL;
   
    if(value > parent->key)
        parent->right = child;
    else
        parent->left = child;
    return root;
}

/*查找,找到返回1,否则,返回0*/
int bst_search(bst_tree *root, char* value)
{
    bst_tree *p;
    p = root;
    if(p == NULL)
        return 0;
    if(p->key == value)
        return 1;
    else if(p->key > value)
        return bst_search(p->left, value);
    else
        return bst_search(p->right, value);
}

/*删除节点值为value的节点*/
int bst_delete(bst_tree *root, char* value)
{
    bst_tree *p, *pre=NULL, *mid;
   
    p = root;
    if(root == NULL)
        return 0;
       
    /*找到该节点*/
    while((p != NULL) && (p->key != value))
    {
        pre = p;
        if(p->key < value)
        {
            p = p->right;
        }
        else
            p = p->left;
    }
    if(p == NULL)
        return 0;
    /*至少有一个子节点为空*/
    if( (p->left == NULL) || (p->right == NULL) )
    {
        if( pre->left == p )
        {
            pre->left = ( (p->left == NULL) ? p->right : p->left );
        }
        else
            pre->right = ( (p->left == NULL) ? p->right : p->left );
       
        free(p);    /*释放节点*/
    }
    else
    {
        /*删除的节点有2个子女*/
        mid = p->right;
        pre = p;
        /*寻找中序的第一个节点*/
        while(mid->left != NULL)
        {   
            pre = mid;
            mid = mid->left;
        }
        /*移花接木,直接赋值,避免交换节点*/
        p->key = mid->key;
       
        /*将mid节点的子节点作为pre的子节点,并将mid所指向的节点删除*/
        if(pre->right == mid)
            pre->right = mid->right;
        else
            pre->left = mid->right;
        free(mid);
    }
    return 1;
}

/*中序输出bst树*/
void bst_print(bst_tree *root)
{
    if(root == NULL)
        return;
    bst_print(root->left);
    printf(" %d ", root->key);
    bst_print(root->right);
}

 

/*int main()
{
    int a[10] = {5,4,2,8,7,1,9,3,6,10};
    int i=0;
    bst_tree *root=NULL;
    for(i=0; i<10; i++)
        root = bst_insert(root, a[i]);
    bst_delete(root, 5);
    bst_print(root);
    printf("/n%d %s/n", root->key, bst_search(root, 10) ? "yes":"no");
    return 0;
}*/

int main()
{
    bst_tree * bsroot;
    int loop,i;
 char* data;
    loop=1;
    while(1)
    {
        printf("/n/n/n");
        printf(" 1.Creat/n");
        printf(" 2.Search/n");
        printf(" 3.Insert/n");
        printf(" 4.Delete/n");
        printf(" 5.Print/n");
        printf(" 0.exit  /n");
        scanf("%d",&i);
        switch(i)
        {
        case 0: {
            loop=0;
            exit(0);
                }
        /*case 1:
            {
                CreatBST(&root);
            }break; */
        case 2:
            {
                printf("Please input the data you want search./n");
                scanf("%d",&data);
                bst_search(bsroot,data);
            }break;
        case 3:
            {
                printf("Please input the data you want insert./n");
                scanf("%d",&data);
                bst_insert(bsroot,data);
            }break;
        case 4:
            {
                printf("Please input the data you want delete./n");
                scanf("%d",&data);
                bsroot=bst_delete(bsroot,data);
            }break;
        case 5:{
            printf("/n");
            if (bsroot!=NULL)
                printf("The BSTree's root is:%d/n",bsroot->key);
            bst_print(bsroot);
            break;

               }
        }
    }
 return 0;
}

原创粉丝点击