二叉树:度为2的结点个数
时间限制: 1s
类别: DS:树->简单
问题描述
内容:(1)请参照链表的ADT模板,设计二叉树并逐步完善的抽象数据类型。(由于该环境目前仅支持单文件的编译,故将所有内容都集中在一个源文件内。在实际的设计中,推荐将抽象类及对应的派生类分别放在单独的头文件中。参考教材、课件,以及网盘中的链表ADT原型文件,自行设计二叉树的ADT。)
注意:二叉树ADT的基本操作的算法设计很多要用到递归的程序设计方法。
(2)基本操作17:在二叉树的二叉链表存储形式建立的基础上,使用递归的程序设计方法,设计并完成统计度为2的结点个数的算法。完成后将其加入到二叉树的ADT基本操作集中。
参考函数原型:
(1)//统计度为2的结点个数(外壳部分,用户函数)
//统计度为2的结点个数
template<class ElemType>
int CountDegreeTwo(BinaryTree<ElemType> &T);
(2)统计度为2的结点个数(递归部分,成员函数)
//统计度为2的结点个数
template<class ElemType>
int BinaryTree<ElemType>::CountDegreeTwo( BinaryTreeNode<ElemType> *T ) const;
输入说明
第一行:表示无孩子或指针为空的特殊分隔符
第二行:二叉树的先序序列(结点元素之间以空格分隔)
输出说明
第一行:度为2的结点个数
#include <iostream>
#include <sstream>
#include <vector>
#include <string>
using namespace std;
template<class ElemType>
struct BinaryTreeNode
{
ElemType data;
BinaryTreeNode<ElemType>* LChild;
BinaryTreeNode<ElemType>* RChild;
BinaryTreeNode(ElemType item = ElemType(), BinaryTreeNode<ElemType>* L = nullptr, BinaryTreeNode<ElemType>* R = nullptr)
: data(item), LChild(L), RChild(R) {}
};
template<class ElemType>
class BinaryTree
{
public:
BinaryTreeNode<ElemType>* root;
void destroy(BinaryTreeNode<ElemType>*& node)
{
if (node != nullptr)
{
destroy(node->LChild);
destroy(node->RChild);
delete node;
node = nullptr;
}
}
void preorder(BinaryTreeNode<ElemType>* node, vector<ElemType>& result)
{
if (node != nullptr)
{
result.push_back(node->data);
preorder(node->LChild, result);
preorder(node->RChild, result);
}
}
void inorder(BinaryTreeNode<ElemType>* node, vector<ElemType>& result)
{
if (node != nullptr)
{
inorder(node->LChild, result);
result.push_back(node->data);
inorder(node->RChild, result);
}
}
void postorder(BinaryTreeNode<ElemType>* node, vector<ElemType>& result)
{
if (node != nullptr)
{
postorder(node->LChild, result);
postorder(node->RChild, result);
result.push_back(node->data);
}
}
BinaryTree() : root(nullptr) {}
~BinaryTree()
{
destroy(root);
}
void createFromPreorder(vector<ElemType> elements, ElemType empty)
{
auto it = elements.begin();
root = create(it, elements.end(), empty);
}
BinaryTreeNode<ElemType>* create(typename vector<ElemType>::iterator& it, typename vector<ElemType>::iterator end, ElemType empty)
{
if (it == end || *it == empty)
{
return nullptr;
}
BinaryTreeNode<ElemType>* node = new BinaryTreeNode<ElemType>(*it);
++it;
node->LChild = create(it, end, empty);
++it;
node->RChild = create(it, end, empty);
return node;
}
void printPreorder()
{
vector<ElemType> result;
preorder(root, result);
printResult(result);
}
void printInorder()
{
vector<ElemType> result;
inorder(root, result);
printResult(result);
}
void printPostorder()
{
vector<ElemType> result;
postorder(root, result);
printResult(result);
}
void printResult(const vector<ElemType>& result)
{
if (!result.empty())
{
cout << result[0];
for (size_t i = 1; i < result.size(); ++i)
{
cout << ',' << result[i];
}
}
cout << endl;
}
int CountDegreeTwo( BinaryTreeNode<ElemType> *T ) const;
};
//统计度为2的结点个数
template<class ElemType>
int BinaryTree<ElemType>::CountDegreeTwo( BinaryTreeNode<ElemType> *T ) const
{
int sum=0;
if(T==nullptr) return 0;
if(T->LChild!=nullptr&&T->RChild!=nullptr) sum++;
else sum=0;
int left=CountDegreeTwo(T->LChild);
int right=CountDegreeTwo(T->RChild);
return left+right+sum;
}
//统计度为2的结点个数(外壳部分,用户函数)
//统计度为2的结点个数
template<class ElemType>
int CountDegreeTwo(BinaryTree<ElemType> &T)
{
return T.CountDegreeTwo(T.root);
}
int main()
{
string nullSymbol;
string preorderInput;
getline(cin, nullSymbol);
getline(cin, preorderInput);
stringstream ss(preorderInput);
string item;
vector<string> elements;
while (ss >> item)
{
elements.push_back(item);
}
BinaryTree<string> tree;
tree.createFromPreorder(elements, nullSymbol);
cout<<CountDegreeTwo(tree)<<endl;
return 0;
}