数据结构复习6.1,堆的插入与删除

//
//  heap_sort.c
//  Data_structure
//
//  Created by 양송 on 2020/06/06.
//  Copyright © 2020 양송. All rights reserved.
//

#include 
#include 
#include 

#define parent(x) (x-1)/2
#define left(x)   (2*x)+1
#define right(x)  (2*x)+2

void swap(int* a, int* b)
{
    int temp = *a;
    *a = *b;
    *b = temp;
}

void Create_Max_Heap(int* data, int size)
{
    for(int i = 1; i < size;i++)
    {
        int child = i;
        while(child > 0)
        {
            int root = parent(child);
            if(data[root] < data[child])
            {
                swap(&(data[root]), &(data[child]));
            }
            child = root;
        }
    }
}

void Create_Min_Heap(int* data, int size)
{
    for(int i = 1; i< size;i++)
    {
        int child = i;
        while(child > 0)
        {
            int root = parent(child);
            if(data[root] > data[child])
            {
                swap((&data[root]), (&data[child]));
            }
            child = root;
        }
    }
}

//option = 1,从小到大
//option = 0,从大到小

void Heap_sort(int* data,int size,int option)
{
    for(int i = size -1; i>=0; i--)
    {
        swap(&data[0], &data[i]);
        
        switch (option) {
            case 1:
                Create_Max_Heap(data, i);
                break;
            case 0:
                Create_Min_Heap(data, i);
                break;
        }
        
    }
}

void print_heap(int* data, int size)
{
    for(int i = 0; i< size;i++)
    {
        printf("%d ",data[i]);
    }
    printf("\n");
}

void insert_some_data_in_maxheap(int* data, int be_insert,int size)
{
    data[size-1] = be_insert;
    int child = size-1;
    while (child > 0) {
        int root = parent(child);
        if(data[root] < data[child])
        {
            swap(&(data[root]), &data[child]);
        }
        child = root;
    }
}

void insert_some_data_in_minheap(int* data, int be_insert,int size)
{
    data[size-1] = be_insert;
    int child = size-1;
    while (child > 0) {
        int root = parent(child);
        if(data[root] > data[child])
        {
            swap(&(data[root]), &data[child]);
        }
        child = root;
    }
}

//最大树与最小树的删除都是对于根节点而言
void remove_some_data_form_maxheap(int* data, int size)
{
    swap(&data[0],&data[size-1]);
    data[size-1] = -1;//已被删除
    for(int i = 1; i< size-1;i++){
    int child = i;
        while (child > 0) {
            int root = parent(child);
            if(data[root] < data[child])
                swap(&data[root], &data[child]);
            child = root;
        }
    }
    
}

void remove_some_data_form_minheap(int*data, int size)
{
    swap(&data[0],&data[size-1]);
    data[size-1] = -1;//已被删除
    for(int i = 1; i< size-1;i++){
    int child = i;
        while (child > 0) {
            int root = parent(child);
            if(data[root] > data[child])
                swap(&data[root], &data[child]);
            child = root;
        }
    }
}

int main()
{
    int data[20];
    srand((unsigned)time(NULL));
    
    for(int i = 0; i< 10; i++)
    {
        int temp = rand()%10+1;
        printf("%d ",temp);
        data[i] = temp;
    }
    printf("\n");
    
     
    
    //从小到大
    Create_Max_Heap(data, 10);
    print_heap(data, 10);
    insert_some_data_in_maxheap(data, 54, 11);
    print_heap(data, 11);
    remove_some_data_form_maxheap(data,11);
    print_heap(data, 10);
    /*Heap_sort(data, 10,1);
    print_heap(data, 10);
    
    //从大到小
    Create_Min_Heap(data, 10);
    Heap_sort(data, 10, 0);
    print_heap(data, 10);*/
    
}

你可能感兴趣的:(C语言,二叉树)