码迷,mamicode.com
首页 > 编程语言 > 详细

二叉排序

时间:2017-09-19 00:38:40      阅读:210      评论:0      收藏:0      [点我收藏+]

标签:res   class   bsp   插入   char   while   ==   null   arc   

 

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

/*
  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("排序结果:\n");
    printTree(&root);
    
    printf("\n输入要查找的值:\n");
    scanf("%d", &key);
    result = search(&root, key);
    
    if(result)
    {
        printf("找到了%d\n", key);
    }else {
        printf("没找到\n");
    }
    
    printf("输入插入的值:");
    scanf("%d", &key);
    insertData(&root, key);
    
    printf("排序结果:\n");
    printTree(&root);
    
    printf("\n输入要删除的值:\n");
    scanf("%d", &key);
    result = delTree(&root, key);
    
    if(result)
    {
        printf("找到了%d,删除\n", key);
    }else {
        printf("没找到\n");
    }
    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;
    
}

 

二叉排序

标签:res   class   bsp   插入   char   while   ==   null   arc   

原文地址:http://www.cnblogs.com/buerr/p/7544595.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!