//二叉树的链式存储结构
typedef struct BiTNode{
ElemType data;
struct BiTNode *lchild,*rchild;
}BiTNode,*BiTree;
//链队列
typedef struct LinkNode{
BiTNode *data;//存储指针
struct LinkNode *next;
}LinkNode;
typedef struct{
LinkNode *front,*rear;//队头队尾
}LinkQueue;
//二叉树的遍历
//先序遍历(1)递归(2)非递归
void preOrder1(BiTree T){
if(T!=null){
visit(T);
preOrder(T->lchild);
preOrder(T->rchild);
}
}
void preOrder2(BiTree T){
InitStack(S);BiTree p=T;//初始化栈S;p是遍历指针
while(p||isEmpty(S)){//栈不为空或p不空时循环
if(p)//一路向左
{
visit(p);push(S,p);//访问结点并入栈
p=p->lchild;//左孩子不空一直向左走
}
else//出栈,并转向出栈结点的右子树
{
Pop(S,p);//栈顶元素出栈
p=p->rchild;//向右孩子走,p赋值为当前结点的右孩子
}
}
}
//中序遍历
void InOrder1(BiTree T){
if(T!=null){
preOrder(T->lchild);
visit(T);
preOrder(T->rchild);
}
}
void InOrder2(BiTree T){
InitStack(S);BiTree p=T;//初始化栈S;p是遍历指针
while(p||isEmpty(S))
{//栈不为空或p不空时循环
if(p)//一路向左
{
push(S,p);//当前节点入栈
p=p->lchild;左孩子不空一路向左
}
else//出栈,并转向出栈结点的右孩子
{
Pop(S,p);visit(p);//栈顶元素出栈,访问出栈结点
p=p->rchild;//向右孩子走,p赋值为当前结点的右孩子
}
}
}
//后序遍历
void PostOrder1(BiTree T){
if(T!=null){
preOrder(T->lchild);
preOrder(T->rchild);
visit(T);
}
}
//层序遍历(队列)
void LevelOrder(BiTree T){
// LinkQueue Q;//链队列
InitQueue(Q);
BiTree p;
EnQueue(Q,p);
while(isEmpty(Q)){
DeQueue(Q,p);
visit(p);
if(p->lchild!=null)
EnQueue(Q,p->lchild);
if(p->rchild!=null)
EnQueue(Q,p->rchild);
}
}
//递归实现:二叉树的深度
void depth(BiTree T){
if(T==null)
return 0;
ldep=depth(T->lchild);
rdep=depth(T->rchild);
if(ldep>rdep)
return ldep+1;
else
return rdep+1;
}
//递归实现:二叉树中结点的个数
int count(BiTree T){
int sum=0;
if(T==null){
return 0;
}else{
return 1+count(T->lchild)+count(T->rchild);
}
}
//递归实现:二叉树中叶子结点的个数
int countLeafNodes(BiTree T) {
// 如果当前节点为空,返回0
if (T == NULL) {
return 0;
} else if (T->lchild == NULL && T->rchild == NULL) {
// 如果当前节点没有子节点,说明是叶子节点,返回1
return 1;
} else {
// 否则,递归计算左右子树的叶子节点个数
return countLeafNodes(T->lchild) + countLeafNodes(T->rchild);
}
}
// 递归函数:计算二叉树中度大于1的节点个数
int countNodesDegreeGreaterThan1(BiTree T, int *count) {
if (T == NULL) {
// 如果节点为空,直接返回0
return 0;
}
int degree = ( T->lchild!= NULL) + (T->rchild != NULL); // 计算当前节点的度
if (degree > 1) {
(*count)++; // 如果度大于1,增加计数
}
// 递归地检查左子树和右子树
countNodesDegreeGreaterThan1(T->lchild, count);
countNodesDegreeGreaterThan1(T->rchild, count);
return *count; // 返回当前的计数
}
// 递归函数:计算二叉树中度为1的节点个数
int countNodesDegree1(BiTree T, int *count) {
// 辅助计数器,由于是递归函数,需要一个外部的计数器来保存结果
(*count)++;
// 检查当前节点是否有且仅有一个非空子节点
if (( T->lchild!= NULL &&T->rchild == NULL) ||
( T->lchild== NULL &&T->rchild != NULL)) {
// 如果是度为1的节点,返回1,否则返回0
return 1;
}
// 如果当前节点不是度为1的节点,递归检查左右子树
int leftDegree1 = 0, rightDegree1 = 0;
if ( T->lchild!= NULL) {
leftDegree1 = countNodesDegree1(T->lchild, count);
}
if (T->rchild != NULL) {
rightDegree1 = countNodesDegree1(T->rchild, count);
}
// 返回左右子树中度为1的节点个数之和
return leftDegree1 + rightDegree1;
}
// 递归函数:计算二叉树中度为2的节点个数
int countNodesDegree2(BiTree T, int *count) {
if ( T== NULL) {
// 如果节点为空,返回0
return 0;
}
int degree = ( T->lchild!= NULL) + (T->rchild != NULL); // 计算当前节点的度
if (degree == 2) {
// 如果度为2,增加计数器
(*count)++;
}
// 递归地检查左子树和右子树
countNodesDegree2(T->lchild, count);
countNodesDegree2(T->rchild, count);
// 函数无返回值,计数器在外部维护
}
第五章:树和二叉树
最新推荐文章于 2024-09-06 19:52:51 发布