树的介绍

数是一种非线性的数据结构,把它叫做数的原因是因为它的结构看起来像一颗倒挂的树一样,它的根是朝上的,叶子是朝下的,如下图

值得注意得是,树得子数之间不能有任何交点,否则就不能称作为树,比如下面的

 树的相关概念

节点的度:一个节点含有的子树的个数称为该节点的度; 如上图:A的度为6
叶节点或终端节点:度为0的节点称为叶节点, 如上图:B、C、H、I...等节点为叶节点
非终端节点或分支节点:度不为0的节点; 如上图:D、E、F、G...等节点为分支节点
双亲节点或父节点:若一个节点含有子节点,则这个节点称为其子节点的父节点; 如上图:A是B的父节点

孩子节点或子节点:一个节点含有的子树的根节点称为该节点的子节点; 如上图:B是A的孩子节点
兄弟节点:具有相同父节点的节点互称为兄弟节点; 如上图:B、C是兄弟节点
树的度:一棵树中,最大的节点的度称为树的度; 如上图:树的度为6
节点的层次:从根开始定义起,根为第1层,根的子节点为第2层,以此类推;
树的高度或深度:树中节点的最大层次; 如上图:树的高度为4
堂兄弟节点:双亲在同一层的节点互为堂兄弟;如上图:H、I互为兄弟节点
节点的祖先:从根到该节点所经分支上的所有节点;如上图:A是所有节点的祖先
子孙:以某节点为根的子树中任一节点都称为该节点的子孙。如上图:所有节点都是A的子孙
森林:由m(m>0)棵互不相交的树的集合称为森林;

树的表示

树的表示法一般有双亲表示法,兄弟表示法,这里我们简单的探讨探讨一下兄弟表示法

typedef int DataType;
struct Node
{
 struct Node* _firstChild1; // 第一个孩子结点
 struct Node* _pNextBrother; // 指向其下一个兄弟结点
 DataType _data; // 结点中的数据域
};

比如下面的

二叉树

二叉树的介绍

二叉树,顾名思义,就是有两个子树 ,

关于二叉树,有两种特殊的结构,分为满二叉树和完全二叉树

满二叉树:每一个根有两个节点

完全二叉树:一个根可能有一个节点,也可能有两个节点,但是得满足一个条件,完全二叉树得数据从左到右必须是连续的,比如下面的

下面的二叉树不是完全二叉树,因为数据从左到右不是连续的

注意:满二叉树也是一种特殊的完全二叉树

堆的介绍

堆的结构完全二叉树,堆又有大堆和小堆之分

大堆:父亲比任和一个儿子都要大(上一个节点比下面两个中的任何一个节点都要大)

小堆:父亲比任和一个小堆都要小(小堆和上面的相反)

堆的实现(实现小堆)

堆的实现主要可以由链表和顺序表实现,由顺序表的画,先要探讨它的结构以及如何用顺序表来存储数据

从上面可以看出来,从上到下,从左到右,元素的下标不断增加,其中,父类和子类由如下的关系

设两个子节点分别为左孩子和右孩子(childleft    childright)

父节点为parent

childleft=parent*2+1

childright=parent*2+2

parent=(child-1)/2,其中,child是左孩子,右孩子都行

初始化和销毁

typedef int HeapDatetype;
typedef struct Heap
{
	HeapDatetype* a;
	int size;
	int capcity;
}Heap;
//初始化堆
void heapinit(Heap* hp)
{
	assert(hp);
	hp->a = NULL;
	hp->capcity = 0;
	hp->size = 0;
}

//销毁堆
void heapdestory(Heap* hp)
{
	assert(hp);
	free(hp->a);
	hp->capcity = 0;
	hp->size = 0;
}

插入数据

//交换两个树的值
void swap(HeapDatetype* a, HeapDatetype* b)
{
	HeapDatetype temp = *a;
	*a = *b;
	*b = temp;
}
//向上调整
void adjustmentup(HeapDatetype* a, int size)
{
	int child = size-1;
	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;
		}
		
	}
}

void heappush(Heap* hp, HeapDatetype x)
{
	assert(hp);
	if (hp->capcity == hp->size)
	{
		int newcapacity = hp->capcity == 0 ? 4 : hp->capcity * 2;
		HeapDatetype* temp = (HeapDatetype*)realloc(hp->a, newcapacity * sizeof(HeapDatetype));
		if (temp == NULL)
		{
			perror("realloc");
			return;
		}
		hp->a = temp;
		hp->capcity = newcapacity;
	}
	hp->a[hp->size] = x;
	hp->size++;
	adjustmentup(hp->a, hp->size);
	
}

对于上面的插入数据,具体思路是:
先插入数据,这和顺序表的插入数据基本上一样

插入数据之后,还要比较插入的数据和自己的父亲相比哪个小,如果小的话还要向上调整数据,来确保这个堆是小堆

向上调整的话,比较两个的大小,小的话就交换两个的值,直到到达根部

删除堆顶的数据

void adjustmentdown(HeapDatetype* a, int size, int n)
{
	int parent = n;
	int childleft = parent * 2 + 1;
	int childright = parent * 2 + 2;
	while (childright<=size-1)
	{
		if ((a[parent]>a[childleft]) && (a[childleft] < a[childright]))
		{
			swap(&a[parent],&a[childleft]);
			parent = childleft;
			childleft = parent * 2 + 1;
			childright = parent * 2 + 2;
		}
		else if((a[parent] > a[childright]) && (a[childleft] >a[childright]))
		{
			swap(&a[parent], &a[childright]);
			parent = childright;
			childleft = parent * 2 + 1;
			childright = parent * 2 + 2;
		}
	}
}

void heappop(Heap* hp)
{
	assert(hp);
	swap(&hp->a[0], &hp->a[hp->size - 1]);
	hp->size--;
	adjustmentdown(hp->a, hp->size, 0);
}

先交换第一个数据(堆顶的数据)和最后一个数据,再删除最后一个数据(和顺序表的删除最后一个元素一样),再向下调整数据,因为交换了数据,所以这时候第一个数据比较大,要把它弄到下面去:
向下调整数据的思路:
比较两个儿子的大小,哪个儿子小就和哪个儿子交换值,直到老子全部比自己的儿子小

 有人可能会比较疑惑:
删除堆顶的数据,直接挪动数据,把第一个数据覆盖不就好了吗?

如果直接挪动的话,会破坏父子关系,兄弟之间可能会变成父子,比如下面的

12345

上面的,如果把1后面的向前移动,2和3原本是兄弟关系的,这时候会变父子

其他(堆顶元素,元素个数,为空吗)


HeapDatetype heaptop(Heap* hp)
{
	assert(hp);
	assert(hp->a);
	return hp->a[0];
}

int heapsize(Heap* hp)
{
	assert(hp);
	return hp->size;
}

bool heapempty(Heap* hp)
{
	assert(hp);
	return hp->size == 0;
}

完整代码

头文件

#include<stdio.h>
#include<stdlib.h>
#include<assert.h>

typedef int HeapDatetype;
typedef struct Heap
{
	HeapDatetype* a;
	int size;
	int capcity;
}Heap;


//初始化堆
void heapinit(Heap* hp);

//销毁堆
void heapdestory(Heap* hp);


//交换两个数的值
void swap(HeapDatetype* a, HeapDatetype* b);


//向上调整
void adjustmentup(HeapDatetype* a, int size);


//插入数据
void heappush(Heap* hp, HeapDatetype x);

//向下调整算法
void adjustmentdown(HeapDatetype* a, int size, int n);


//删除顶层的数据
void heappop(Heap* hp);

//取堆顶的数据
HeapDatetype heaptop(Heap* hp);


//数据的个数
int heapsize(Heap* hp);

bool heapempty(Heap* hp);

源文件

#include"Heap.h"


//初始化堆
void heapinit(Heap* hp)
{
	assert(hp);
	hp->a = NULL;
	hp->capcity = 0;
	hp->size = 0;
}

//销毁堆
void heapdestory(Heap* hp)
{
	assert(hp);
	free(hp->a);
	hp->capcity = 0;
	hp->size = 0;
}



void swap(HeapDatetype* a, HeapDatetype* b)
{
	HeapDatetype temp = *a;
	*a = *b;
	*b = temp;
}
void adjustmentup(HeapDatetype* a, int size)
{
	int child = size-1;
	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;
		}
		
	}
}

void heappush(Heap* hp, HeapDatetype x)
{
	assert(hp);
	if (hp->capcity == hp->size)
	{
		int newcapacity = hp->capcity == 0 ? 4 : hp->capcity * 2;
		HeapDatetype* temp = (HeapDatetype*)realloc(hp->a, newcapacity * sizeof(HeapDatetype));
		if (temp == NULL)
		{
			perror("realloc");
			return;
		}
		hp->a = temp;
		hp->capcity = newcapacity;
	}
	hp->a[hp->size] = x;
	hp->size++;
	adjustmentup(hp->a, hp->size);
	
}


void adjustmentdown(HeapDatetype* a, int size, int n)
{
	int parent = n;
	int childleft = parent * 2 + 1;
	int childright = parent * 2 + 2;
	while (childright<=size-1)
	{
		if ((a[parent]>a[childleft]) && (a[childleft] < a[childright]))
		{
			swap(&a[parent],&a[childleft]);
			parent = childleft;
			childleft = parent * 2 + 1;
			childright = parent * 2 + 2;
		}
		else if((a[parent] > a[childright]) && (a[childleft] >a[childright]))
		{
			swap(&a[parent], &a[childright]);
			parent = childright;
			childleft = parent * 2 + 1;
			childright = parent * 2 + 2;
		}
	}
}

void heappop(Heap* hp)
{
	assert(hp);
	swap(&hp->a[0], &hp->a[hp->size - 1]);
	hp->size--;
	adjustmentdown(hp->a, hp->size, 0);
}


HeapDatetype heaptop(Heap* hp)
{
	assert(hp);
	assert(hp->a);
	return hp->a[0];
}

int heapsize(Heap* hp)
{
	assert(hp);
	return hp->size;
}

bool heapempty(Heap* hp)
{
	assert(hp);
	return hp->size == 0;
}

测试文件

#include"Heap.h"

int main()
{
	Heap hp;
	heapinit(&hp);
	heappush(&hp, 50);
	heappush(&hp, 43);
	heappush(&hp, 45);
	heappush(&hp, 56);
	heappush(&hp, 20);
	heappop(&hp);
	heappop(&hp);
	printf("数据的个数是%d", heapsize(&hp));
	printf("\n");
	for (int i = 0; i < hp.size; i++)
	{
		printf("%d ", hp.a[i]);
	}
	heapdestory(&hp);
	return 0;
}

效果

  • 8
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值