用按层次顺序遍历二叉树的方法,统计树中具有度为1的结点数目。
#define _CRT_SECURE_NO_WARNINGS#include <iostream>
using namespace std;typedef char ElemType;
#define ERROR 0
#define OK 1
#define Maxsize 100
#define STR_SIZE 1024typedef struct BiTNode
{ElemType data;BiTNode* lchild, * rchild;
}BiTNode, * BiTree;void draw(BiTNode* root);//-------------------------队列操作---------------------------//
typedef struct Queue
{BiTNode* data[Maxsize];int front;int rear;
}Queue;void Init_Queue(Queue& Q)
{Q.front = Q.rear = 0;
}bool Empty_Queue(Queue& Q)
{if (Q.front == Q.rear){cout << "队列为空" << endl;;return OK;}return ERROR;
}bool Enter_Queue(Queue& Q, BiTNode* x)
{if ((Q.rear + 1) % Maxsize == Q.front){cout << "队列已满,无法继续入队!!!" << endl;return ERROR;}Q.data[Q.rear] = x;Q.rear = (Q.rear + 1) % Maxsize;return OK;
}bool Leave_Queue(Queue& Q, BiTNode*& x)
{if (Q.rear == Q.front){cout << "队列为空,无法出队!!!" << endl;return ERROR;}x = Q.data[Q.front];cout << "出队元素为:" << x->data << endl;//Q.front = (Q.front + 1) % Maxsize;return OK;
}
//-------------------------队列操作---------------------------//bool Create_tree(BiTree& T) //递归创建二叉树
{ElemType x = 0;cin >> x;if (x == '#'){T = NULL;}else{T = (BiTree)malloc(sizeof(BiTNode));if (T == NULL){cout << "内存无法分配!!!" << endl;return ERROR;}T->data = x;T->lchild = NULL;T->rchild = NULL;Create_tree(T->lchild);Create_tree(T->rchild);}return OK;
}void PreOrder(BiTree T) //前序遍历非递归
{if (T != NULL){cout << T->data;PreOrder(T->lchild);PreOrder(T->rchild);}
}void InOrder(BiTree T) //中序遍历非递归
{if (T != NULL){InOrder(T->lchild);cout << T->data;InOrder(T->rchild);}
}void PostOrder(BiTree T) //后序遍历非递归
{if (T != NULL){PostOrder(T->lchild);PostOrder(T->rchild);cout << T->data;}
}//---------------------------------核心代码---------------------------------//
//主要思想:层次遍历
int one_du_count(BiTree T)
{int count = 0;if (T == NULL){cout << "树为空" << endl;return 0;}if (T->lchild == NULL && T->rchild == NULL){cout << "树中只有一个结点" << endl;return 0;}BiTNode* p = T;Queue Q;Init_Queue(Q);Enter_Queue(Q, p);while (Empty_Queue(Q) != OK){Leave_Queue(Q,p);if (p->lchild != NULL){Enter_Queue(Q, p->lchild);}if (p->rchild != NULL){Enter_Queue(Q, p->rchild);}if ( (p->lchild != NULL &&p->rchild==NULL)||(p->lchild==NULL&&p->rchild!=NULL) ){count++;}}return count;
}
//---------------------------------核心代码---------------------------------//
//用按层次顺序遍历二叉树的方法,统计树中具有度为1的结点数目。
//测试树1:012###3##
//测试树1:012###34###
int main(void)
{cout << "//------生成一颗树---------//" << endl;BiTree T = NULL;Create_tree(T);PreOrder(T);cout << endl;InOrder(T);cout << endl;PostOrder(T);cout << endl;cout << "//------生成一颗树---------//" << endl;cout << "//------原始树图形---------//" << endl;draw(T);cout << "树中度为1的结点个数为: " << one_du_count(T) << endl;return 0;
}//参考博客:https://blog.csdn.net/weixin_42109012/article/details/92250160
/*****************************************************************************
* @date 2020/4/19
* @brief 水平画树
* @param node 二叉树节点
* @param left 判断左右
* @param str 可变字符串
*****************************************************************************/
void draw_level(BiTNode* node, bool left, char* str) {if (node->rchild) {draw_level(node->rchild, false, strcat(str, (left ? "| " : " ")));}printf("%s", str);printf("%c", (left ? '\\' : '/'));printf("-----");printf("%c\n", node->data);if (node->lchild) {draw_level(node->lchild, true, strcat(str, (left ? " " : "| ")));}// " " : "| " 长度为 6str[strlen(str) - 6] = '\0';
}/*****************************************************************************
* @date 2020/4/19
* @brief 根节点画树
* @param root 二叉树根节点
*****************************************************************************/
void draw(BiTNode* root) {char str[STR_SIZE];memset(str, '\0', STR_SIZE);/*** 1. 在 windows 下,下面是可执行的* 2. 在 Linux 下,执行会报 Segmentation fault* 需要使用中间变量*/if (root->rchild) {draw_level(root->rchild, false, str);}printf("%c\n", root->data);if (root->lchild) {draw_level(root->lchild, true, str);}
}