目的:设计并实现基于后序线索二叉树的后序遍历的非递归算法。
要求:
(1)创建二叉树。
(2)转换为后序线索二叉树。
(3)实现后序遍历的非递归算法。
(4)其它要求同课后作业-01要求。
软件环境:visual stdio 2017
硬件环境:①CPU:Intel(R)Core(TM)i7-8565U CPU @1.80Ghz
②内存:8.0GB
设计并实现基于后序线索二叉树的后序遍历的非递归算法。
本次实验主要解决的任务可以分解为:建立一颗二叉树,将二叉树进行后序线索化,对线索化后的二叉树进行后序遍历。
数据结构采取链式结构建立树,每个结点增加部分辅助空间以服务于二叉树的线索化。
通过编写后序线索化函数将树中叶子结点空闲的左儿子指针和右儿子指针指向逻辑上的前驱后继。
每个二叉树结点新增1个指针,2个bool型标记。指针为父亲指针,指向结点的父亲。两个bool型标记表示该节点的左/右儿子指针是否指向逻辑上的前驱后继还是子节点。
template
struct BTNode
{
T data;
bool ltag;
bool rtag;
BTNode* leftson;
BTNode* rightson;
BTNode* father;
};
将二叉树封装为类:
template
class BT
{
public:
BT();
~BT();
void build(); //建树
void erase(); //删除
void ToPoClueBT(); //转换为后序线索二叉树
void PoTraversal(); //后序线索遍历
protected:
void build(BTNode* & father, BTNode* & root);
void erasebyRecursion(BTNode* & root);
void ToPoClueBTAssist(BTNode*&root, string& str); //转换函数的辅助函数
private:
BTNode* root;
bool isclued; //标记是否被线索化
bool isbuilt; //标记是否已建树
};
建树采取输入扩展先序序列递归建树的方法。
template
void BT::build()
{
if (isbuilt)
{
cout << "The binary Tree has been built , if you want to rebuild , please erase it ! " << endl;
return;
}
this->isbuilt = true;
BTNode* null = NULL;
build(null, this->root);
}
template
void BT::build(BTNode* & father,BTNode* & root)
{
char ch = cin.get();
if (ch == '.')
{
root = NULL;
return;
}
root = new BTNode;
root->father = father;
root->data = ch;
root->ltag = false;
root->rtag = false;
build(root, root->leftson);
build(root, root->rightson);
}
对于二叉树的后序线索化,采用后序递归遍历的方法找到子节点为空的结点,将其空指针指向前驱/后继。有以下几种情况:
①左孩子结点为空:
template
void BT::ToPoClueBT()
{
if (isclued)
{
cout << "The binary Tree has been clued , your instruction is to be canceled ! " << endl;
return;
}
this->isclued = true;
string empty = "";
ToPoClueBTAssist(this->root, empty);
}
template
void BT::ToPoClueBTAssist(BTNode* & root, string& str)
{
if (root->leftson != NULL) ToPoClueBTAssist(root->leftson, str += '0');
if (root->rightson != NULL) ToPoClueBTAssist(root->rightson, str += '1');
if (root->leftson == NULL)
{
if (root->rightson != NULL)
{
root->ltag = true;
root->leftson = root->rightson;
}
else
{
BTNode* node = root;
for (int i = str.size() - 1; i > 0; i--)
{
if (str[i] == '1')
{
root->ltag = true;
if (node->father->leftson != NULL)
{
root->leftson = node->father->leftson;
break;
}
}
node = node->father;
}
}
}
if (root->rightson == NULL)
{
root->rtag = true;
if (str[str.size() - 1] = '1') root->rightson = root->father;
else
{
BTNode* Node = root->father->rightson;
if (Node == NULL) root->rightson = root->father;
else
{
while (Node->leftson != NULL) { Node = Node->leftson; }
root->rightson = Node;
}
}
}
}
对于后序线索二叉树的后序遍历,初始化node=根结点,建立循环,用字符串记录各结点data值,每次记录后便将node更新为前驱,更新的时候利用ltag与rtag进行判断逻辑上的前驱的位置。最后将字符串反向输出。
template
void BT::PoTraversal()
{
string OppositeResult, Result;
BTNode* node = this->root;
while (node != NULL)
{
OppositeResult += node->data;
if (node->ltag) node = node->leftson;
else
{
if (node->rightson != NULL && !node->rtag) node = node->rightson;
else node = node->leftson;
}
}
for (int i = OppositeResult.size() - 1; i >= 0; i--) Result += OppositeResult[i];
cout << "The result of postorder clue binary tree traversal is:" << Result << endl;
}
通过本次实验,我掌握如何将二叉树线索化的方法,同时本次实验也加深了我对数据结构的理解,自己的编码水平也得到了一定的提升。
#include
#include
using namespace std;
template
struct BTNode
{
T data;
bool ltag;
bool rtag;
BTNode* leftson;
BTNode* rightson;
BTNode* father;
};
template
class BT
{
public:
BT();
~BT();
void build(); //建树
void erase(); //删除
void ToPoClueBT(); //转换为后序线索二叉树
void PoTraversal(); //后序线索遍历
protected:
void build(BTNode* & father, BTNode* & root);
void erasebyRecursion(BTNode* & root);
void ToPoClueBTAssist(BTNode*&root, string& str); //转换函数的辅助函数
private:
BTNode* root;
bool isclued; //标记是否被线索化
bool isbuilt; //标记是否已建树
};
template
BT::BT()
{
root = new BTNode;
isclued = false;
isbuilt = false;
}
template
BT::~BT()
{
this->erase();
}
\
template
void BT::erase()
{
if (isbuilt)
{
BTNode* node = new BTNode;
while (root != NULL)
{
node = root;
if (root->ltag) root = root->leftson;
else
{
if (root->rightson != NULL && !node->rtag) root = root->rightson;
else root = root->leftson;
}
delete node;
}
}
else this->erasebyRecursion(this->root);
isbuilt = 0;
isclued = 0;
root = NULL;
}
template
void BT::erasebyRecursion(BTNode* &node)
{
if (node == NULL) return;
erasebyRecursion(node->leftson);
erasebyRecursion(node->rightson);
delete node;
}
template
void BT::build()
{
if (isbuilt)
{
cout << "The binary Tree has been built , if you want to rebuild , please erase it ! " << endl;
return;
}
this->isbuilt = true;
BTNode* null = NULL;
build(null, this->root);
}
template
void BT::build(BTNode* & father,BTNode* & root)
{
char ch = cin.get();
if (ch == '.')
{
root = NULL;
return;
}
root = new BTNode;
root->father = father;
root->data = ch;
root->ltag = false;
root->rtag = false;
build(root, root->leftson);
build(root, root->rightson);
}
template
void BT::ToPoClueBTAssist(BTNode* & root, string& str)
{
if (root->leftson != NULL) ToPoClueBTAssist(root->leftson, str += '0');
if (root->rightson != NULL) ToPoClueBTAssist(root->rightson, str += '1');
if (root->leftson == NULL)
{
if (root->rightson != NULL)
{
root->ltag = true;
root->leftson = root->rightson;
}
else
{
BTNode* node = root;
for (int i = str.size() - 1; i > 0; i--)
{
if (str[i] == '1')
{
root->ltag = true;
if (node->father->leftson != NULL)
{
root->leftson = node->father->leftson;
break;
}
}
node = node->father;
}
}
}
if (root->rightson == NULL)
{
root->rtag = true;
if (str[str.size() - 1] = '1') root->rightson = root->father;
else
{
BTNode* Node = root->father->rightson;
if (Node == NULL) root->rightson = root->father;
else
{
while (Node->leftson != NULL) { Node = Node->leftson; }
root->rightson = Node;
}
}
}
}
template
void BT::ToPoClueBT()
{
if (isclued)
{
cout << "The binary Tree has been clued , your instruction is to be canceled ! " << endl;
return;
}
this->isclued = true;
string empty = "";
ToPoClueBTAssist(this->root, empty);
}
template
void BT::PoTraversal()
{
string OppositeResult, Result;
BTNode* node = this->root;
while (node != NULL)
{
OppositeResult += node->data;
if (node->ltag) node = node->leftson;
else
{
if (node->rightson != NULL && !node->rtag) node = node->rightson;
else node = node->leftson;
}
}
for (int i = OppositeResult.size() - 1; i >= 0; i--) Result += OppositeResult[i];
cout << "The result of postorder clue binary tree traversal is:" << Result << endl;
}
int main()
{
BT tree;
cout << "Please enter the expanded tree preorder list :";
tree.build();
tree.ToPoClueBT();
tree.PoTraversal();
return 0;
}