#include <iostream>
#include <sstream>
#include <vector>
#include <string>
using namespace std;
template<class T>
struct BinaryTreeNode
{
T data;
BinaryTreeNode<T>* LChild;
BinaryTreeNode<T>* RChild;
BinaryTreeNode(T item = T(), BinaryTreeNode<T>* L = nullptr, BinaryTreeNode<T>* R = nullptr)
: data(item), LChild(L), RChild(R) {}
};
template<class T>
class BinaryTree
{
private:
BinaryTreeNode<T>* root;
void destroy(BinaryTreeNode<T>*& node)
{
if (node != nullptr)
{
destroy(node->LChild);
destroy(node->RChild);
delete node;
node = nullptr;
}
}
void preorder(BinaryTreeNode<T>* node, vector<T>& result)
{
if (node != nullptr)
{
result.push_back(node->data);
preorder(node->LChild, result);
preorder(node->RChild, result);
}
}
void inorder(BinaryTreeNode<T>* node, vector<T>& result)
{
if (node != nullptr)
{
inorder(node->LChild, result);
result.push_back(node->data);
inorder(node->RChild, result);
}
}
void postorder(BinaryTreeNode<T>* node, vector<T>& result)
{
if (node != nullptr)
{
postorder(node->LChild, result);
postorder(node->RChild, result);
result.push_back(node->data);
}
}
public:
BinaryTreeNode<T>* getroot()
{
return root;
}
BinaryTree() : root(nullptr) {} ~BinaryTree()
{
destroy(root);
}
void printResult(const vector<T>& result)
{
if (!result.empty())
{
cout << result[0];
for (size_t i = 1; i < result.size(); ++i)
{
cout << ',' << result[i];
}
}
cout << endl;
}
void createFromPreorder(vector<T> elements, T empty)
{
auto it = elements.begin();
root = create(it, elements.end(), empty);
}
BinaryTreeNode<T>* create(typename vector<T>::iterator& it, typename vector<T>::iterator end, T empty)
{
if (it == end || *it == empty)
{
return nullptr;
}
BinaryTreeNode<T>* node = new BinaryTreeNode<T>(*it);
++it;
node->LChild = create(it, end, empty);
++it;
node->RChild = create(it, end, empty);
return node;
}
void printPreorder()
{
vector<T> result;
preorder(root, result);
printResult(result);
}
void printInorder()
{
vector<T> result;
inorder(root, result);
printResult(result);
}
void printPostorder()
{
vector<T> result;
postorder(root, result);
printResult(result);
}
void ssign (BinaryTreeNode<T> *node, T x,T value,int &flag)
{
if(node!=nullptr)
{
if(node->data==x)
{
node->data=value;
flag=1;
}
ssign(node->LChild,x,value,flag);
ssign(node->RChild,x,value,flag);
}
}
};
template <class T>
bool Ssign(BinaryTree<T> &t, T &x, T &value)
{
int flag=0;
t.ssign (t.getroot(), x, value,flag);
if(flag==1)return true;
return false;
}
int main()
{
string nullSymbol;
string preorderInput;
getline(cin, nullSymbol);
getline(cin, preorderInput);
stringstream input(preorderInput);
string item;
vector<string> elements;
while (input >> item)
{
elements.push_back(item);
}
string x,value;
cin>>x>>value;
BinaryTree<string> tree;
tree.createFromPreorder(elements, nullSymbol);
tree.printPreorder();
tree.printInorder();
tree.printPostorder();
cout<<endl;
if(Ssign(tree,x,value))
{
cout<<"success"<<endl;
tree.printPreorder();
tree.printInorder();
tree.printPostorder();
}
else
cout<<"false";
return 0;
}
二叉树,给x结点赋值
最新推荐文章于 2024-08-16 08:17:29 发布