heap
This commit is contained in:
parent
ab8a54af3a
commit
fb3595a829
103
heap.c
Normal file
103
heap.c
Normal file
@ -0,0 +1,103 @@
|
|||||||
|
#include <stdio.h>
|
||||||
|
#include <string.h>
|
||||||
|
#include <malloc.h>
|
||||||
|
#include "heap.h"
|
||||||
|
|
||||||
|
void heap_swap(heap *tree,int x,int y)
|
||||||
|
{
|
||||||
|
elem_t tt = tree->data[x];
|
||||||
|
tree->data[x] = tree->data[y];
|
||||||
|
tree->data[y] = tt;
|
||||||
|
}
|
||||||
|
|
||||||
|
heap* heap_init()
|
||||||
|
{
|
||||||
|
heap *tree = (heap *)malloc(sizeof(heap));
|
||||||
|
memset(tree,0,sizeof(heap));
|
||||||
|
return tree;
|
||||||
|
}
|
||||||
|
|
||||||
|
int heap_get_right(heap *tree,int i)
|
||||||
|
{
|
||||||
|
return 2 * i + 2;
|
||||||
|
}
|
||||||
|
|
||||||
|
int heap_get_left(heap *tree,int i)
|
||||||
|
{
|
||||||
|
return 2 * i + 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
int heap_get_parent(heap *tree,int i)
|
||||||
|
{
|
||||||
|
return (i - 1)/2;
|
||||||
|
}
|
||||||
|
|
||||||
|
void heap_push(heap *tree,int val)
|
||||||
|
{
|
||||||
|
if (tree->size == MAX_S-1)
|
||||||
|
{
|
||||||
|
printf("heap is full\n");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
tree->data[tree->size] = val;
|
||||||
|
int flog_i = tree->size;
|
||||||
|
while(1)
|
||||||
|
{
|
||||||
|
int flog_p = heap_get_parent(tree,flog_i);
|
||||||
|
if (tree->data[flog_i] <= tree->data[flog_p] || flog_p<0)
|
||||||
|
{
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
heap_swap(tree,flog_i,flog_p);
|
||||||
|
flog_i = flog_p;
|
||||||
|
}
|
||||||
|
tree->size++;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
void heap_print(heap* tree,int root)
|
||||||
|
{
|
||||||
|
int queue[3] = {0};
|
||||||
|
int font = root;
|
||||||
|
int rear = font+1;
|
||||||
|
int flog = 1;
|
||||||
|
printf("[%d]\n",tree->data[root]);
|
||||||
|
while (flog < tree->size)
|
||||||
|
{
|
||||||
|
int flog_r = 0,flog_l = 0;
|
||||||
|
for (int i = 0;i<2;i++)
|
||||||
|
{
|
||||||
|
switch (i)
|
||||||
|
{
|
||||||
|
case 0:
|
||||||
|
flog_l= heap_get_left(tree,queue[font]);
|
||||||
|
if (flog_l > tree->size)
|
||||||
|
{
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
queue[rear] = flog_l;
|
||||||
|
rear = (rear+1) % 3;
|
||||||
|
printf("[%d]",tree->data[flog_l]);
|
||||||
|
flog++;
|
||||||
|
break;
|
||||||
|
case 1:
|
||||||
|
flog_r = heap_get_right(tree,queue[font]);
|
||||||
|
if (flog_r > tree->size)
|
||||||
|
{
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
queue[rear] = flog_r;
|
||||||
|
rear = (rear+1) % 3;
|
||||||
|
printf("[%d]",tree->data[flog_r]);
|
||||||
|
flog++;
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
printf("\n");
|
||||||
|
font = (font+1) % 3;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
17
heap.h
Normal file
17
heap.h
Normal file
@ -0,0 +1,17 @@
|
|||||||
|
#ifndef HEAP_H
|
||||||
|
#define HEAP_H
|
||||||
|
#define MAX_S 256
|
||||||
|
#define elem_t int
|
||||||
|
typedef struct heap {
|
||||||
|
elem_t data[MAX_S];
|
||||||
|
int size;
|
||||||
|
}heap;
|
||||||
|
void heap_swap(heap *tree,int x,int y);
|
||||||
|
int heap_get_left(heap *tree,int i);
|
||||||
|
int heap_get_right(heap *tree,int i);
|
||||||
|
int heap_get_parent(heap *tree,int i);
|
||||||
|
heap* heap_init();
|
||||||
|
void heap_push(heap *tree,elem_t val);
|
||||||
|
elem_t heap_pop(heap *tree);
|
||||||
|
void heap_print(heap *tree,int root);
|
||||||
|
#endif
|
Loading…
x
Reference in New Issue
Block a user