首先我们要在结点类里加一个bool类型的函数用于判断该结点度是否为1👇
//判断结点度是否为1
bool isAlone() {
if (leftchild == NULL && rightchild != NULL) {
return true;
}
else if(leftchild != NULL && rightchild == NULL){
return true;
}
else {
return false;
}
}
随后我们再到二叉树类里编写我们的算法
递归算法和上一篇blog里的求二叉树深度(递归+非递归)非常类似👇
//递归统计二叉树度为1的节点个数
int CaculateAlone1(BinaryTreeNode<T>* treeNode) {
int count = 0;
if (treeNode != NULL) {
if (treeNode->isAlone()) {
count++;
}
count += CaculateAlone1(treeNode->getLeft());
count += CaculateAlone1(treeNode->getRight());
}
return count;
}
非递归也和求深度差不多,核心也是借助队列先进先出的特点进行操作👇
//非递归统计二叉树度为1的节点个数
int CaculateAlone2(BinaryTreeNode<T>* treeNode) {
int count = 0;
if (treeNode != NULL) {
queue<BinaryTreeNode<T>*> q;
q.push(treeNode);
while (!q.empty()) {
for (int size = q.size(); size > 0; size--) {
BinaryTreeNode<T>* p = q.front();
q.pop();
if (p->isAlone()) {
count++;
}
if (p->getLeft()) {
q.push(p->getLeft());
}
if (p->getRight()) {
q.push(p->getRight());
}
}
}
}
return count;
}
最后附上完整代码👇
#include<iostream>
#include<queue>
using namespace std;
//结点类
template<class T>
class BinaryTreeNode {
//友元类声明
template<class T>
friend class BinaryTree;
private:
T data;
BinaryTreeNode<T>* leftchild;
BinaryTreeNode<T>* rightchild;
public:
//构造函数
BinaryTreeNode() {
leftchild = NULL;
rightchild = NULL;
}
BinaryTreeNode(const T& dataValue) {
data = dataValue;
leftchild = NULL;
rightchild = NULL;
}
BinaryTreeNode(const T& dataValue, BinaryTreeNode<T>* leftchildValue, BinaryTreeNode<T>* rightchildValue) {
data = dataValue;
leftchild = leftchildValue;
rightchild = rightchildValue;
}
//析构函数
~BinaryTreeNode() {
leftchild = NULL;
rightchild = NULL;
}
//取左结点
BinaryTreeNode<T>* getLeft() {
return leftchild;
}
//取右结点
BinaryTreeNode<T>* getRight() {
return rightchild;
}
//判断结点度是否为1
bool isAlone() {
if (leftchild == NULL && rightchild != NULL) {
return true;
}
else if(leftchild != NULL && rightchild == NULL){
return true;
}
else {
return false;
}
}
};
//二叉树类
template<class T>
class BinaryTree {
private:
BinaryTreeNode<T>* root;
public:
//构造函数
BinaryTree() {
root = NULL;
cout << "Now start to construct the BinaryTree" << endl;
CreateNode(root);
}
//析构函数
~BinaryTree() {
root = NULL;
}
//前序构建二叉树
void CreateNode(BinaryTreeNode<T>* &treeNode) {
cout << "Please enter a value or '#' to stop:";
T dataValue;
cin >> dataValue;
treeNode = new BinaryTreeNode<T>(dataValue);
if (treeNode->data == '#') {
delete treeNode;
treeNode = NULL;
}
else {
CreateNode(treeNode->leftchild);
CreateNode(treeNode->rightchild);
}
}
//取根节点
BinaryTreeNode<T>* getRoot() {
return root;
}
//递归统计二叉树度为1的节点个数
int CaculateAlone1(BinaryTreeNode<T>* treeNode) {
int count = 0;
if (treeNode != NULL) {
if (treeNode->isAlone()) {
count++;
}
count += CaculateAlone1(treeNode->getLeft());
count += CaculateAlone1(treeNode->getRight());
}
return count;
}
//非递归统计二叉树度为1的节点个数
int CaculateAlone2(BinaryTreeNode<T>* treeNode) {
int count = 0;
if (treeNode != NULL) {
queue<BinaryTreeNode<T>*> q;
q.push(treeNode);
while (!q.empty()) {
for (int size = q.size(); size > 0; size--) {
BinaryTreeNode<T>* p = q.front();
q.pop();
if (p->isAlone()) {
count++;
}
if (p->getLeft()) {
q.push(p->getLeft());
}
if (p->getRight()) {
q.push(p->getRight());
}
}
}
}
return count;
}
};
int main() {
//前序构建二叉树
BinaryTree<char> test;
//统计二叉树度为1的节点个数
cout << endl << "递归求得度为1的节点个数:";
cout << test.CaculateAlone1(test.getRoot());
cout << endl << "非递归求得度为1的节点个数:";
cout << test.CaculateAlone2(test.getRoot());
}