二叉树理解

一.树

1.树的概念

  我们想要了解二叉树首先就要先了解树这种数据结构。

  树是一种非线性的数据结构,它是由n(n>=0)个有限结点组成一个具有层次关系的集合。把它叫做树是因为它看起来像一棵倒挂的树,也就是说它是根朝上,而叶朝下的。

  

  如上图就是树实例简图。

  有一个特殊的结点,称为根结点,根结点没有前驱结点,就是我们的A节点。

  接下来我们介绍树的一些概念,如下:

  结点的度:一个结点含有的子树的个数称为该结点的度; 如上图: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)棵互不相交的树的集合称为森林;

2.树的表示

  我们常采用做兄弟右孩子表示法去描述树。

  左孩子指的是child指针指向的是它的从左往右的第一个子节点,右兄弟指的是brother指针指向它的右边相邻的兄弟节点。

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

二.二叉树

1.概念

  二叉树不存在度大于2的结点,二叉树的子树有左右之分,次序不能颠倒,因此二叉树是有序树。

如图所示。

2.特殊的二叉树

  满二叉树:一个二叉树,如果每一个层的结点数都达到最大值,则这个二叉树就是满二叉树。也就是说,如果一个二叉树的层数为K,且结点总数是 ,则它就是满二叉树。

  完全二叉树:完全二叉树是效率很高的数据结构,完全二叉树是由满二叉树而引出来的。对于深度为K的,有n个结点的二叉树,当且仅当其每一个结点都与深度为K的满二叉树中编号从1至n的结点一一对应时称之为完全二叉树。 要注意的是满二叉树是一种特殊的完全二叉树。

3.二叉树的性质

/*
* 假设二叉树有N个结点
* 从总结点数角度考虑:N = n0 + n1 + n2 ①

* 从边的角度考虑,N个结点的任意二叉树,总共有N-1条边
* 因为二叉树中每个结点都有双亲,根结点没有双亲,每个节点向上与其双亲之间存在一条边
* 因此N个结点的二叉树总共有N-1条边

* 因为度为0的结点没有孩子,故度为0的结点不产生边; 度为1的结点只有一个孩子,故每个度为1的结点* * 产生一条边; 度为2的结点有2个孩子,故每个度为2的结点产生两条边,所以总边数为:
n1+2*n2 
* 故从边的角度考虑:N-1 = n1 + 2*n2 ②
* 结合① 和 ②得:n0 + n1 + n2 = n1 + 2*n2 - 1
* 即:n0 = n2 + 1
*/

这是对第三点的解释说明。

4.二叉树链式结构的实现

  二叉树的链式结构采用更加直接的表示法,即left指向左边的子节点,right指向右边的子节点。

  这里面包含了了二叉树的创建与销毁,层序遍历,前序中序后序遍历,求节点个数,求树的高度,叶节点的个数,第k层的节点个数,判断是否为完全二叉树,二叉树的查找等函数。我真的懒,不想在讲了,直接看代码吧。

BT.h

#pragma once

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

typedef int BTDataType;

typedef struct BinaryTreeNode
{
	BTDataType data;
	struct BinaryTreeNode* left;
	struct BinaryTreeNode* right;
}BTNode;

BTNode* BuyNode(BTDataType x);

BTNode* CreateBT();

void PrevOrder(BTNode* root);

void InOrder(BTNode* root);

void PostOrder(BTNode* root);

int BTSize(BTNode* root);

int BTLeafSize(BTNode* root);

int BTHeight(BTNode* root);

int BTLevelKSize(BTNode* root,int k);

BTNode* BTFind(BTNode* root,BTDataType x);

void BTDestroy(BTNode* root);

void BTLevelOrder(BTNode* root);

bool IsCompleteBT(BTNode* root);

BT.c

#include "BT.h"

BTNode* BuyNode(BTDataType x)
{
	BTNode* node = (BTNode*)malloc(sizeof(BTNode));
	if (NULL == node)
	{
		perror("malloc fail");
		exit(1);
	}
	node->data = x;
	node->left = node->right = NULL;
	return node;
}

BTNode* CreateBT()
{
	BTNode* node1 = BuyNode(1);
	BTNode* node2 = BuyNode(2);
	BTNode* node3 = BuyNode(3);
	BTNode* node4 = BuyNode(4);
	BTNode* node5 = BuyNode(5);
	BTNode* node6 = BuyNode(6);
	node1->left = node2;
	node1->right = node4;
	node2->left = node3;
	node4->left = node5;
	node4->right = node6;
	return node1;
}

void PrevOrder(BTNode* root)
{
	if (NULL == root)
	{
		printf("N ");
		return;
	}
	printf("%d ", root->data);
	PrevOrder(root->left);
	PrevOrder(root->right);
}

void InOrder(BTNode* root)
{
	if (NULL == root)
	{
		printf("N ");
		return;
	}
	InOrder(root->left);
	printf("%d ",root->data);
	InOrder(root->right);
}

void PostOrder(BTNode* root)
{
	if (NULL == root)
	{
		printf("N ");
		return;
	}
	PostOrder(root->left);
	PostOrder(root->right);
	printf("%d ", root->data);
}

int BTSize(BTNode* root)
{
	return NULL == root ? 0 : 1 + BTSize(root->left) + BTSize(root->right);  
}

int BTLeafSize(BTNode* root)
{
	if (NULL == root)
	{
		return 0;
	}
	if (root->left == NULL && root->right == NULL)
	{
		return 1;
	}
	else
	{
		return BTLeafSize(root->left) + BTLeafSize(root->right);
	}
}
  
int BTHeight(BTNode* root)
{
	if (NULL == root)
	{
		return 0;  
	}
	int leftheight = BTHeight(root->left);
	int rightheight = BTHeight(root->right);
	return 1 + (leftheight > rightheight ? leftheight : rightheight);
}

int BTLevelKSize(BTNode* root, int k)
{
	if (NULL == root)
	{
		return 0;
	}
	if (1 == k)
	{
		return 1;
	}
	return BTLevelKSize(root->left,k-1)+BTLevelKSize(root->right,k-1);
}

BTNode* BTFind(BTNode* root, BTDataType x)
{
	if (NULL == root)
	{
		return NULL;
	}
	if (root->data == x)
	{
		return root;
	}
	BTNode* lefttree = BTFind(root->left,x);
	if (lefttree)
	{
		return lefttree;
	}
	BTNode* righttree = BTFind(root->right, x);
	if (righttree)
	{
		return righttree;
	}
	return NULL;
}

void BTDestroy(BTNode* root)
{
	if (NULL == root)
	{
		return;
	}
	BTDestroy(root->left);
	BTDestroy(root->right);
	free(root);
}

#include "Queue.h"

void BTLevelOrder(BTNode* root)
{
	if (NULL == root)
	{
		return;
	}
	Queue queue;
	QueueInit(&queue);
	QueuePush(&queue,root);
	while (!QueueEmpty(&queue))
	{
		BTNode* front = QueueFront(&queue);
		QueuePop(&queue);  
		printf("%d ", front->data);
		if (front->left)
		{
			QueuePush(&queue, front->left);
		}
		if (front->right)
		{
			QueuePush(&queue, front->right);
		}
	}
	QueueDestroy(&queue);
	printf("\n");
}

bool IsCompleteBT(BTNode* root)
{
	Queue queue;
	QueueInit(&queue);
	QueuePush(&queue,root);
	while (1)
	{
		BTNode* front = QueueFront(&queue);
		QueuePop(&queue);
		if (NULL == front)
		{
			break;
		}
		QueuePush(&queue, front->left);
		QueuePush(&queue, front->right);
	}
	bool ret = true;
	while (!QueueEmpty(&queue))
	{
		if (QueueFront(&queue))
		{
			ret = false;
			break;
		}
		QueuePop(&queue);
	}
	QueueDestroy(&queue);
	return ret;
}

  注意一下层序遍历会用到我们的队列,大致思路是先入根节点,然后Pop,把Pop节点的子节点全部入队,这样一直循环,直到队列为空,就完成了层序遍历了。基本上就是上一层带下一层的思路。

  然后简述一下前序中序与后序:

  二叉树的遍历有:前序/中序/后序的递归结构遍历:
  1. 前序遍历(Preorder Traversal 亦称先序遍历)——访问根结点的操作发生在遍历其左右子树之前。
  2. 中序遍历(Inorder Traversal)——访问根结点的操作发生在遍历其左右子树之中(间)。
  3. 后序遍历(Postorder Traversal)——访问根结点的操作发生在遍历其左右子树之后。

  还要注意二叉树基本是以递归来构建的,在学习时应该建立起把一个二叉树转换成根节点与左子树和右子树,然后左子树右子树又分为根节点与左右子树,从而建立递归,解决相关问题。

 三.堆

1.堆的概念

  堆是建立在完全二叉树之上的,只不过多了一些限制条件,它分为大堆和小堆。如下:

小堆与大堆示例如上。

2.堆的存储

  其实不止是堆对于完全二叉树而言都可以用数组去存储。

  普通的二叉树是不适合用数组来存储的,因为可能会存在大量的空间浪费。而完全二叉树更适合使用顺序结构存储。现实中我们通常把堆(一种二叉树)使用顺序结构的数组来存储,需要注意的是这里的堆和操作系统虚拟进程地址空间中的堆是两回事,一个是数据结构,一个是操作系统中管理内存的一块区域分段。

  并且有趣的发现,对于给定下标i,2*i+1就为其左孩子,2*i+2就为其右孩子,(i-1)/2就为其父节点,这一点就更加验证了用顺序结构存储的好处。即堆的底层存储是用顺序表来存储的。

3.堆的实现

我们以小堆举例。

Heap.c

#include "Heap.h"

void HPInit(HP* php)
{
	assert(php);
	php->size = php->capacity = 0;
	php->arr = NULL;
}

void HPDestroy(HP* php)
{
	assert(php);
	free(php->arr);
	php->capacity = php->size = 0;
	php->arr = NULL;
}

void Swap(HPDataType* p1, HPDataType* p2)
{
	HPDataType temp = *p1;
	*p1 = *p2;
	*p2 = temp;
}

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


void HPPush(HP* php, HPDataType x)
{
	assert(php);
	if (php->size == php->capacity)
	{
		int newcapacity = php->capacity == 0 ? 4: php->capacity * 2;
		HPDataType* temp  = (HPDataType*)realloc(php->arr,sizeof(HPDataType)*newcapacity);
		if (NULL == temp)
		{
			perror("realloc fail");
			exit(1);
		}
		php->arr = temp;
		php->capacity = newcapacity;
	}
	php->arr[php->size] = x;
	php->size++;
	AdjustUp(php->arr,php->size-1);
}

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

void HPPop(HP* php)
{
	assert(php);
	assert(php->size);
	Swap(&php->arr[0],&php->arr[php->size-1]);
	php->size--;
	AdjustDown(php->arr,php->size,0);
}

bool HPEmpty(HP* php)
{
	assert(php);
	return !php->size;
}

HPDataType HPTop(HP* php)
{
	assert(php);
	assert(php->size);
	return php->arr[0];
}

//降序为例
//时间复杂度为O(N*logN)
void HPSort(int* arr, int size)
{
	//建堆算法
	//向上建堆算法
	//时间复杂度O(N*logN)
	/*int i = 0;
	for (i = 1; i < size; i++)
	{
		AdjustUp(arr,i);
	}*/
	//向下建堆算法(推荐,时间复杂度O(N))
	int i = 0;
	for (i = (size - 2) / 2; i >= 0; i--)
	{
		AdjustDown(arr, size, i);
	}
	//交换,向下调整算法,时间复杂度O(N*logN)
	int end = size - 1;
	while(end > 0)
	{
		Swap(arr,arr+end);
		AdjustDown(arr,end,0);
		end--;
	}
}

//优化,调整区域,调整上线(下标),调整下限(下标),以小堆为例

void _AdjustUp(int* arr, int up, int down)
{
	int child = down;
	int parent = (down - 1) / 2;
	while (child > up)
	{
		if (arr[child] < arr[parent])
		{
			Swap(arr+child,arr+parent);
			child = parent;
			parent = (child - 1) / 2;
		}
		else
		{
			break;
		}
	}
}

void _AdjustDown(int* arr, int up, int down)
{
	int parent = up;
	int child = parent * 2 + 1;
	//while(parent <= (down-1)/2) //保证parent是非终端节点,确保parent有子节点,都可以
	while(child <= down)
	{
		if (child + 1 <= down && arr[child+1] < arr[child])
		{
			child++;
		}
		if (arr[child] < arr[parent])
		{
			Swap(arr+child,arr+parent);
			parent = child;
			child = parent * 2 + 1;
		}
		else
		{
			break;
		}
	}
}

//降序
void _HPSort(int* arr, int size)
{
	//建堆
	/*int i = 0;
	for (i = 1;i < size;i++)
	{
		_AdjustUp(arr,0,i);
	}*/
	//建堆
	int i = 0;
	for (i = (size - 2) / 2; i >= 0; i--)
	{
		_AdjustDown(arr,i,size-1);
	}
	//排序
	int end = size - 1;
	while (end > 0)
	{
		Swap(arr,arr+end);
		end--;
		_AdjustDown(arr,0,end);
	}
}

Heap.h

#pragma once

//以小堆为例,大堆类比

#define _CRT_SECURE_NO_WARNINGS 

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

typedef int HPDataType;

typedef struct Heap
{
	HPDataType* arr;
	int size;
	int capacity;
}HP;

void HPInit(HP* php);

void HPDestroy(HP* php);

void HPPush(HP* php, HPDataType x);

void HPPop(HP* php);

void AdjustUp(HPDataType* arr,int child);

void Swap(HPDataType* p1,HPDataType* p2);

void AdjustDown(HPDataType* arr, int size, int parent);

bool HPEmpty(HP* php);

HPDataType HPTop(HP* php);

void HPSort(int* arr,int size);

//优化向上向下调整算法
void _AdjustUp(int* arr,int up,int down);

void _AdjustDown(int* arr, int up, int down);

void _HPSort(int* arr,int size);


4.堆的应用

4.1堆排序

  我们需要知道升序要建大堆,降序要建小堆。这里涉及到向上建堆算法和向上向下调整算法。

  向上调整算法:

//优化,调整区域,调整上线(下标),调整下限(下标),以小堆为例

void _AdjustUp(int* arr, int up, int down)
{
	int child = down;
	int parent = (down - 1) / 2;
	while (child > up)
	{
		if (arr[child] < arr[parent])
		{
			Swap(arr+child,arr+parent);
			child = parent;
			parent = (child - 1) / 2;
		}
		else
		{
			break;
		}
	}
}

向下调整算法:

void _AdjustDown(int* arr, int up, int down)
{
	int parent = up;
	int child = parent * 2 + 1;
	//while(parent <= (down-1)/2) //保证parent是非终端节点,确保parent有子节点,都可以
	while(child <= down)
	{
		if (child + 1 <= down && arr[child+1] < arr[child])
		{
			child++;
		}
		if (arr[child] < arr[parent])
		{
			Swap(arr+child,arr+parent);
			parent = child;
			child = parent * 2 + 1;
		}
		else
		{
			break;
		}
	}
}

堆排序:

//降序
void _HPSort(int* arr, int size)
{
	//建堆
	/*int i = 0;
	for (i = 1;i < size;i++)
	{
		_AdjustUp(arr,0,i);
	}*/
	//建堆
	int i = 0;
	for (i = (size - 2) / 2; i >= 0; i--)
	{
		_AdjustDown(arr,i,size-1);
	}
	//排序
	int end = size - 1;
	while (end > 0)
	{
		Swap(arr,arr+end);
		end--;
		_AdjustDown(arr,0,end);
	}
}

这里我真的很粗糙,大家一定要自行了解其中的原理方法思路,这样有利于提升自己。我是纯纯因为懒才写得这么简略。

4.2 TOP-K问题
void Test4()
{
	//最大的Topk问题
	FILE* pf = fopen("Data", "r");
	if (NULL == pf)
	{
		perror("foen fail");
		exit(1);
	}
	int k = 0;
	scanf("%d", &k);
	int i = 0;
	int* arr = (int*)malloc(sizeof(int)*k);
	for (i = 0;i < 10;i++)
	{
		fscanf(pf, "%d", arr + i);
	}
	for (i = (k-2)/2;i >= 0;i--)
	{
		AdjustDown(arr, k, i);
	}
	int temp = 0;
	while (fscanf(pf,"%d",&temp) != EOF)
	{
		if (arr[0] < temp)
		{
			arr[0] = temp;
			AdjustDown(arr, k, 0);
		}
	}
	for (i = 0;i < k;i++)
	{
		printf("%d ", arr[i]);
	}
	printf("\n");
	fclose(pf);
}

这里设计到文件操作,大家要注意一下哈。思路大致如下。

使用这种方法就避免了对数据进行排序,优化了我们效率。

希望大家每天都能进步哦。

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值