二叉树:x的孩子
时间限制: 1s
类别: DS:树->简单
问题描述
目的:使用C++模板设计并逐步完善二叉树的抽象数据类型(ADT)。
内容:(1)请参照链表的ADT模板,设计二叉树并逐步完善的抽象数据类型。(由于该环境目前仅支持单文件的编译,故将所有内容都集中在一个源文件内。在实际的设计中,推荐将抽象类及对应的派生类分别放在单独的头文件中。参考教材、课件,以及网盘中的链表ADT原型文件,自行设计二叉树的ADT。)
注意:二叉树ADT的基本操作的算法设计很多要用到递归的程序设计方法。
(2)基本操作10:在二叉树的二叉链表存储形式建立的基础上,使用递归的程序设计方法,设计并完成查找以元素值x为父结点的孩子结点的算法。完成后将其加入到二叉树的ADT基本操作集中。
初始条件:二叉树T存在,x是T中某个结点。
操作结果:若x是T的结点,且孩子结点存在,则返回指向其孩子结点的指针(查找成功,flag=0,返回左孩子指针;flag=1,返回右孩子指针)。否则返回NULL(查找失败)。
参考函数原型:
(1)查找以元素值x为父结点的孩子结点(外壳部分,用户函数)
//返回以元素值x为父结点的孩子结点的指针(外壳,flag=0,左孩子;flag=1,右孩子)
template<class ElemType>
BinaryTreeNode<ElemType> * LocationChild(BinaryTree<ElemType> &T, ElemType &x, int flag);
(2)查找以元素值x为父结点的孩子结点(递归部分,成员函数)
//查找值为x的结点的孩子结点的指针(flag=0,左孩子;flag=1,右孩子)
template<class ElemType>
void BinaryTree<ElemType>::Location_Child( BinaryTreeNode<ElemType> *T, ElemType &x, int flag, BinaryTreeNode<ElemType> * &location ) const;
输入说明
第一行:表示无孩子或指针为空的特殊分隔符
第二行:二叉树的先序序列(结点元素之间以空格分隔)
第三行:元素值
第四行:flag值
输出说明
第一行:查找成功:左孩子指针 左孩子元素值(flag=0)
右孩子指针 右孩子元素值(flag=1)
查找失败:显示 NULL(空树、无对应的孩子结点、非树结点)
#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;
}
void Location_Child(BinaryTreeNode<ElemType> *T, ElemType &x, int flag, BinaryTreeNode<ElemType> * &location ) const
{
if (T == nullptr)
{
return;
}
if (flag == 0 && T->data == x && T->LChild != nullptr)
{
location = T->LChild;
return;
}
else if (flag == 1 && T->data == x && T->RChild != nullptr)
{
location = T->RChild;
return;
}
Location_Child(T->LChild, x, flag, location);
Location_Child(T->RChild, x, flag, location);
}
};
template<class ElemType>
ElemType LocationChild(BinaryTree<ElemType> &T, ElemType &x, int flag)
{
BinaryTreeNode<ElemType>* location=nullptr;
T.Location_Child(T.root, x, flag, location);
if(location != nullptr)
return location->data;
else
return "NULL";
}
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);
string x;
cin >> x;
int flag;
cin >> flag;
cout << LocationChild<string>(tree, x, flag) << endl;
return 0;
}