数据结构C语言版清华大学严蔚敏(学习笔记总结3,代码在下面)——栈和队列、进栈出栈、栈空、入队、出队、循环队列(前面是笔记方便理解,最下面有详细代码)_玛卡巴卡的博客-CSDN博客
1、创建树
#define _CRT_SECURE_NO_WARNINGS 1
#include "Tree.h"
BTNode *BinaryTreeCreate(BTDataType*a, int* pi) //创建树
{
if (a[*pi] == '#')
{
(*pi)++;
return NULL;
}
BTNode* root = (BTNode*)malloc(sizeof(BTNode));
root->_left = root->_right = NULL;
root->_data = a[*pi];
(*pi)++;
root->_left = BinaryTreeCreate(a, pi);
root->_right = BinaryTreeCreate(a, pi);
return root;
}
2、销毁树
void BinaryTreeDestory(BTNode* root) //销毁树
{
if (root)
{
BinaryTreeDestory(root->_left);
BinaryTreeDestory(root->_right);
free(root);
root = NULL;
}
}
3、统计叶子节点
int BinaryTreeSize(BTNode* root) //统计树节点个数
{
if (root == NULL)
return 0;
return 1 + BinaryTreeSize(root->_left) + BinaryTreeSize(root->_right);
}
int BinaryTreeLeafSize(BTNode* root) //统计树的叶子节点个数
{
if (root == NULL)
return 0;
else
{
if ((root->_left == NULL) && (root->_right == NULL))
return 1;
else
return BinaryTreeLeafSize(root->_left) + BinaryTreeLeafSize(root->_right);
}
}
4、统计第k层结点数
int BinaryTreeLevelkSize(BTNode* root, int k) //递归--统计树中第k层的节点个数
{
if (root == NULL) return 0;
if (k==1)
return 1;
return BinaryTreeLevelkSize(root->_left,k-1) + BinaryTreeLevelkSize(root->_right,k-1);
}
BTNode *BinaryTreeFind(BTNode* root, BTDataType x) //找到某个树节点
{
BTNode* ret;
if (root == NULL)
return NULL;
if (root->_data == x)
return root;
ret = BinaryTreeFind(root->_left, x);
{
if (ret)
return ret;
ret = BinaryTreeFind(root->_right, x);
if (ret)
return ret;
}
return NULL;
}
5、前序遍历、中序遍历、后序遍历(递归和非递归共六种)
void BinaryTreePrevOrder(BTNode* root) //递归--前序遍历
{
if (root == NULL)
return;
printf("%c ", root->_data);
BinaryTreePrevOrder(root->_left);
BinaryTreePrevOrder(root->_right);
}
void BinaryTreeInOrder(BTNode* root) //递归--中序遍历
{
if (root == NULL)
return;
BinaryTreeInOrder(root->_left);
printf("%c ", root->_data);
BinaryTreeInOrder(root->_right);
}
void BinaryTreePostOrder(BTNode* root) //递归--后序遍历
{
if (root == NULL)
return;
BinaryTreePostOrder(root->_left);
BinaryTreePostOrder(root->_right);
printf("%c ", root->_data);
}
void BinaryPrevOrder(BTNode* root) //非递归--前序遍历
{
Stack st;
StackInit(&st);
BTNode* cur = root;
while (StackEmpty(&st) != 0 || cur != NULL)
{
while (cur != NULL)
{
printf("%c ", cur->_data);
StackPush(&st, cur);
cur = cur->_left;
}
BTNode* top = StackTop(&st);
StackPop(&st);
cur=top->_right;
}
}
void BinaryInOrder(BTNode* root) //非递归--中序遍历
{
Stack st;
StackInit(&st);
BTNode* cur = root;
while (StackEmpty(&st) != 0 || cur != NULL)
{
while (cur != NULL)
{
StackPush(&st, cur);
cur = cur->_left;
}
BTNode* top = StackTop(&st);
printf("%c ", top->_data);
StackPop(&st);
cur = top->_right;
}
}
void BinaryPostOrder(BTNode* root) //非递归--后序遍历
{
BTNode* cur = root;
BTNode* prev = NULL;
Stack st;
StackInit(&st);
while (cur || StackEmpty(&st) != 0)
{
while (cur != NULL)
{
StackPush(&st, cur);
cur = cur->_left;
}
BTNode* top = StackTop(&st);
if (top->_right == NULL || top->_right == prev)
{
StackPop(&st);
printf("%c ", top->_data);
prev = top;
}
else cur=top->_right;
}
}
6、层序遍历
void BinaryTreeLevelOrder(BTNode* root) //层序遍历
{
Qu q;
QueueInit(&q);
if (root != NULL)
{
QueuePush(&q, root);
while (QueueEmpty(&q) != 0)
{
BTNode* front = QueueFront(&q);
printf("%c ", front->_data);
QueuePop(&q);
if (front->_left)
QueuePush(&q, front->_left);
if (front->_right)
QueuePush(&q, front->_right);
}
}
}
7、判断是否是完全二叉树
int BinaryTreeComplete(BTNode* root) //判断树是否是完全二叉树
{
Qu q;
QueueInit(&q);
if (root != NULL)
{
QueuePush(&q, root);
while (QueueEmpty(&q))
{
BTNode* front = QueueFront(&q);
QueuePop(&q);
if (front == NULL)
break;
QueuePush(&q, front->_left);
QueuePush(&q, front->_right);
}
while (QueueEmpty(&q) )
{
BTNode* front = QueueFront(&q);
if (front != NULL)
{
QueueDestory(&q);
return 0;
}
QueuePop(&q);
}
return 1;
}
return 0;
}