zoukankan      html  css  js  c++  java
  • 二叉排序

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

    /*
      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;
        
    }
  • 相关阅读:
    List for game to play latter
    C语言基础问题总结
    Java基础学习总结(70)——开发Java项目常用的工具汇总
    谈谈普通码农如何不靠工资也能月入过万
    Java基础学习总结(69)——匿名内部类与Lambda表达式
    Html学习总结(2)——Html页面head标签元素的意义和应用场景
    Android学习总结(5)——9个非常有用的Andorid 程序片段
    Mysql学习总结(39)——30条MySql语句优化技巧
    Java基础学习总结(68)——有关Java线程方面的面试题
    Mysql学习总结(38)——21条MySql性能优化经验
  • 原文地址:https://www.cnblogs.com/buerr/p/7544595.html
Copyright © 2011-2022 走看看