溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

C語言數據結構堆的基本操作實現是怎樣的

發布時間:2021-11-29 09:21:11 來源:億速云 閱讀:167 作者:柒染 欄目:開發技術

本篇文章為大家展示了C語言數據結構堆的基本操作實現是怎樣的,內容簡明扼要并且容易理解,絕對能使你眼前一亮,通過這篇文章的詳細介紹希望你能有所收獲。

    1.基本函數實現

    a.代碼1(向下調整)

    void AdjustDown(DateType*a, int n, int parent)
    {
    	int child = parent * 2 + 1;
    	while (child<n)
    	{
    		if ((child+1) < n && a[child] > a[child + 1])
    		{
    			++child;
    		}
    		if (a[parent] > a[child])
    		{
    			Swap(&a[parent], &a[child]);
    			parent = child;
    			child = parent * 2 + 1;
    		}
    		else
    		{
    			break;
    		}
    	}
    }

    注意:if里面的條件語句(child +1)<n是防止越界的,因為不能保證有右孩子。

    b.代碼2(向上調整)

    void AdjustUp(DateType*a , int child)
    {
    	int parent = (child - 1) / 2;
    	while (child > 0)
    	{
    		if (a[child] < a[parent])
    		{
    			Swap(&a[child], &a[parent]);
    			child = parent;
    			parent = (child - 1) / 2;
    		}
    		else
    		{
    			break;
    		}
    	}
    }

    注意:while里面的條件語句是不能夠寫成(parent<0),因為當child==0時,parent=(child - 1) / 2,parent==0,再次進入循環不滿足a[child] < a[parent],恰好跳出循環。如果寫成(a[child] <= a[parent])就死循環了

    c.代碼3(交換)

    void Swap(DateType*p1, DateType*p2)
    {
    	DateType tmp = *p1;
    	*p1 = *p2;
    	*p2 = tmp;
    }

    2.建堆 

    void CreatHeap(Heap*p,DateType*num,int n)
    {
    	assert(p);
    	p->a = (DateType*)malloc(n * sizeof(DateType));
    	if (p->a == NULL)
    	{
    		printf("malloc failed\n");
    		exit(-1);
    	}
    	memcpy(p->a, num, n * sizeof(DateType));
    	p->size = n;
    	p->capacity = n;
    	//建小堆
    	for (int i = (n - 1 - 1) / 2; i >= 0; i--)
    	{
    		AdjustDown(p->a, p->size, i);
    	}
    }

    3.插入數據

    void HeapPush(Heap*p, DateType x)
    {
    	assert(p);
    	if (p->size == p->capacity)
    	{
    		DateType*tmp = (DateType*)realloc(p->a, (p->capacity) * 2 * sizeof(DateType));
    		if (tmp == NULL)
    		{
    			printf("realloc  failed\n ");
    			exit(-1);
    		}
    	}
    	(p->capacity) *= 2;
    	p->a[p->size] = x;
    	++(p->size);
    	//向上調整
    	AdjustUp(p->a, p->size-1);
    }

    4. 刪除數據

    void HeapPop(Heap*p, DateType x)
    {
    	assert(p);
    	Swap(&p->a[0], &p->a[p->size-1]);
    	--(p->size);
    	AdjustDown(p->a, p->size, 0);
    	//左右子樹還是小堆,直接調整行了
    }

    把堆頂的數據與最后一個數據交換,再次調整size-1個數據。 

    5.獲取堆頂的數據

    DateType HeapTop(Heap*p)
    {
    	assert(p);
    	return p->a[0];
    }

    6.堆的數據個數

    int HeapSize(Heap*p)
    {
    	assert(p);
    	return p->size;
    }

    7.判空

    bool HeapIsEmpty(Heap*p)
    {
    	assert(p);
    	return p->size == 0;
    }

    8.打印

    void Print(Heap*p)
    {
    	assert(p);
    	for (int i = 0; i < p->size; i++)
    	{
    		printf("%d ", (p->a)[i]);
    	}
    	printf("\n");
    	int count = 0;//計數
    	int levelsize = 1;
    	for (int i = 0; i < p->size; i++)
    	{
    		printf("%d ", p->a[i]);
    		++count;
    		if (count == levelsize)
    		{
    			printf("\n");
    			levelsize *= 2;
    			count = 0;//重新計數
    		}
    	}
    	printf("\n");
    }

    9.銷毀

    void HeapDestory(Heap*p)
    {
    	assert(p);
    	free(p->a);
    	p->a = NULL;
    	p->capacity = p->size = 0;
    }

    10.測試

    int main()
    {
    	int num[] = { 12,15,17,23,10,25 };
    	int n = sizeof(num) / sizeof(num[0]); 
    	Heap a;
     	//創建小堆
    	CreatHeap(&a,num, n);
    	Print(&a);
    	printf("\n"); 
    	//插入數據
    	HeapPush(&a, 1);
    	Print(&a);
     	//刪除對頂的數據
    	HeapPop(&a);
    	Print(&a);
    	printf("\n"); 
    	//獲取堆頂數據
    	int ret=HeapTop(&a);
    	printf("The top date is %d\n",ret); 
    	//堆的數據個數
    	int number=HeapSize(&a);
    	printf("The number of heap is %d\n", number); 
    	//銷毀
    	HeapDestory(&a); 
    	return 0;
    }

    11.測試結果

    C語言數據結構堆的基本操作實現是怎樣的

    12.用堆排序(降序)

    a.代碼1

    int main()
    {
    	DateType num[] = { 12,15,17,23,10,25 };
    	int n = sizeof(num) / sizeof(num[0]);
    	HeapSort(num, n);
    	for (int i = 0; i < n; i++)
    	{
    		printf("%d ", num[i]);
    	}
    	printf("\n\n");
    	return 0;
    }
    void HeapSort(int*num, int n)
    {
    	//建小堆
    	for (int i = (n - 1 - 1) / 2; i >= 0; i--)
    	{
    		AdjustDown(num, n, i);
    	}
    	int end = n - 1;
    	while (end>0)
    	{
    		Swap(&num[0], &num[end]);
    		AdjustDown(num, end, 0);
    		--end;
    	}
    }

    運行結果

    C語言數據結構堆的基本操作實現是怎樣的

    上述內容就是C語言數據結構堆的基本操作實現是怎樣的,你們學到知識或技能了嗎?如果還想學到更多技能或者豐富自己的知識儲備,歡迎關注億速云行業資訊頻道。

    向AI問一下細節

    免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。

    AI

    亚洲午夜精品一区二区_中文无码日韩欧免_久久香蕉精品视频_欧美主播一区二区三区美女