二叉排序

二叉排序,附带插入,查找和删除值。。

/*
  Author: buer
  Date: 2017/9/18 11:56:02
*/

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

typedef struct Tree
{
    int data;
    struct Tree *lchild;
    struct Tree *rchild;
}Tree;

void createBiTree(Tree *root);
void insertData(Tree *root, int data);
void printTree(Tree *root);
int search(Tree *root,int key);
int delTree(Tree *root,int key);

int main(int argc, char *argv[])
{
    int key, result;
    Tree root = {0, NULL, NULL};
    
    createBiTree(&root);
    
    printf("排序结果:
");
    printTree(&root);
    
    printf("
输入要查找的值:
");
    scanf("%d", &key);
    result = search(&root, key);
    
    if(result)
    {
        printf("找到了%d
", key);
    }else {
        printf("没找到
");
    }
    
    printf("输入插入的值:");
    scanf("%d", &key);
    insertData(&root, key);
    
    printf("排序结果:
");
    printTree(&root);
    
    printf("
输入要删除的值:
");
    scanf("%d", &key);
    result = delTree(&root, key);
    
    if(result)
    {
        printf("找到了%d,删除
", key);
    }else {
        printf("没找到
");
    }
    printTree(&root);
    return 0;
}

void createBiTree(Tree *root)
{
    int data;
    scanf("%d", &data);
    
    while(data != 0)
    {
        if(data == ' ')
        {
            scanf("%d", &data);
            continue;
        }
        insertData(root, data);
        scanf("%d", &data);
    }
} 

void insertData(Tree *root, int data)
{
    if(!root->data)
    {
        root->data = data;
    }
    else
    {
        if(root->data == data)
        {
            return;
        }
        else if(root->data > data)
        {
            if(!root->lchild)
            {
                root->lchild = (Tree *)malloc(sizeof(Tree));
                root->lchild->data = 0;
                root->lchild->lchild = NULL;
                root->lchild->rchild = NULL;
            }
            insertData(root->lchild, data);
        }
        else
        {
            if(!root->rchild)
            {
                root->rchild = (Tree *)malloc(sizeof(Tree));
                root->rchild->data = 0;
                root->rchild->lchild = NULL;
                root->rchild->rchild = NULL;
            }
            insertData(root->rchild, data);
        }
    }
}

void printTree(Tree *root)
{
    if(!root)
    {
        return;
    }
    printTree(root->lchild);
    printf("%d ", root->data);
    printTree(root->rchild);
}

int search(Tree *root, int key)
{
    if(!root)
    {
        return 0;
    }
    else if(root->data == key)
    {
        return 1;
    }
    else if(root->data > key)
    {
        return search(root->lchild, key);
    }
    else
    {
        return search(root->rchild, key);
    }
    
}

int delTree(Tree *root,int key)
{
    Tree *p = root;
    Tree *pre, *tmp;
    
    while(p)
    {
        if(p->data == key)
        {
            break;
        }else if(p->data > key)
        {
            tmp = p;
            p = p->lchild;
        }else 
        {
            tmp = p;
            p = p->rchild;
        }
    }
    if(p == NULL)
    {
        return 0;
    }
    
    if(p->lchild == NULL && p->rchild == NULL)
    {
        if(tmp->data > key)
        {
            tmp->lchild = NULL;
        }else {
            tmp->rchild = NULL; 
        }
        free(p);
        return 1;
    }
    if(p->lchild == NULL)
    {
        if(tmp->data > key)
        {
            tmp->lchild = p->rchild;
        }else {
            tmp->rchild = p->rchild; 
        }
        free(p);
        return 1;
    }
    if(p->rchild == NULL)
    {
        if(tmp->data > key)
        {
            tmp->lchild = p->lchild;
        }else {
            tmp->rchild = p->lchild; 
        }
        free(p);
        return 1;
    }
    
    pre = p;
    tmp = p->lchild;
    while(tmp->rchild)
    {
        pre = tmp;
        tmp = tmp->rchild;
    }
    if(pre == p)
    {
        p->data = tmp->data;
        p->lchild = NULL;
        free(tmp);
        return 1;
    }
    if(tmp->lchild == NULL)
    {
        p->data = tmp->data;
        pre->rchild == NULL;
        free(tmp);
        return 1;
    }
    p->data = tmp->data;
    tmp->data = tmp->lchild->data;
    free(tmp->lchild);
    tmp->lchild = NULL;
    return 1;
    
}
原文地址:https://www.cnblogs.com/buerr/p/7544595.html