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