// 张雨辰 2021.6.30
#include <cstdlib>
#include <iostream>
#include <queue>
#include <stack>
using namespace std;
typedef struct TreeNode
{
char date;
TreeNode *lchild, *rchild;
} * PT;
PT creat_pre_in(char *a, char *b, int i, int j, int s, int t) //
{
if (i > j)
{
return nullptr; //branch is ending
}
int k;
PT root = new TreeNode();
root->date = a[i];
k = s;
while (k <= t && a[i] != b[k]) //find node in infix
k++;
if (k > t)
exit(0);
root->lchild = creat_pre_in(a, b, i + 1, i + k - s, s, k - 1);
root->rchild = creat_pre_in(a, b, i + k - s + 1, j, k + 1, t);
return root;
}
PT creat_in_post(char *b, char *c, int i, int j, int s, int t)
{
if (i > j)
return nullptr;
int k;
PT root = new TreeNode();
root->date = c[t]; //the root node
k = i;
while (k <= j && b[k] != c[t])
k++;
if (k > j)
exit(0);
root->lchild = creat_in_post(b, c, i, k - 1, s, s + k - i - 1);
root->rchild = creat_in_post(b, c, k + 1, j, s + k - i, t - 1);
return root;
}
//A C F # # E J # # # B # D H # # G # #
void creat_pre_binTree(PT &root)
{
char ch;
cin >> ch;
if (ch == '#')
root = nullptr;
else
{
root = new TreeNode;
root->date = ch;
creat_pre_binTree(root->lchild);
creat_pre_binTree(root->rchild);
}
}
PT creation_pre()
{
// string s = "ACF##EJ###B#DH##G##";
PT root = new TreeNode;
creat_pre_binTree(root);
return root;
}
//recursive traversal
void preorder(PT root)
{
if (root)
{
cout << root->date << " ";
preorder(root->lchild);
preorder(root->rchild);
}
}
void inorder(PT root)
{
if (root)
{
inorder(root->lchild);
cout << root->date << " ";
inorder(root->rchild);
}
}
void postorder(PT root)
{
if (root)
{
postorder(root->lchild);
postorder(root->rchild);
cout << root->date << " ";
}
}
void levelOrder(PT root)
{
queue<PT> qu;
PT temp;
qu.push(root);
while (!qu.empty())
{
temp = qu.front();
qu.pop();
cout << temp->date << " ";
if (temp->lchild)
qu.push(temp->lchild);
if (temp->rchild)
qu.push(temp->rchild);
}
}
//without recursion
void preorderWithoutRecursion(PT root) // 先一路输出并压栈所有左子树 然后压栈右子树
{
stack<PT> st;
while (!st.empty() || root)
{
while (root)
{
cout << root->date << " ";
st.push(root);
root = root->lchild;
}
root = st.top();
st.pop();
root = root->rchild;
}
cout << endl;
}
void inorderWithoutRecursion(PT root) //先一路压栈所有左子树,然后取出栈顶元素输出再将右子树压栈
{
stack<PT> st;
while (!st.empty() || root)
{
while (root)
{
st.push(root);
root = root->lchild;
}
root = st.top();
st.pop();
cout << root->date << " ";
root = root->rchild;
}
cout << endl;
}
void postorderWithoutRecursion(PT root)
{
stack<PT> st;
PT flag = nullptr; //上一次访问节点
while (!st.empty() || root)
{
while (root) //一路压栈左子树
{
st.push(root);
root = root->lchild;
}
root = st.top();
if (root->rchild == nullptr || root->rchild == flag)
{ // 出栈只有右子树为空或者右子树已经访问
cout << root->date << " ";
flag = root;
root = nullptr; // 防止已经出栈后 又再次while循环压栈
st.pop();
}
else
{
root = root->rchild;
}
}
cout << endl;
}
bool lsComplete(BiTree T) // 判断是否为完全二叉树
{
InitQueue(Q);
if (!T)
return 1;
EnQueue(Q, T);
while (!lsEmpty(Q))
{
DeQueue(Q, p);
if (P)
{
EnQueue(Q, p->lchild);
EnQueue(Q, p->rchild);
}
else
while (!lsEmpty(Q))
{
DeQueue(Q, p);
if (p)
return O;
}
}
return 1;
}
#endif
int main()
{
char a[] = {'a', 'b', 'd', 'e', 'c', 'f'}; //先序序列
char b[] = {'d', 'b', 'e', 'a', 'f', 'c'}; //中序序列
char c[] = {'d', 'e', 'b', 'f', 'c', 'a'}; //后序序列
// PT root = creation_pre(); //扩展先序集建立二叉树
// PT root = creat_pre_in(a, b, 0, sizeof(a) - 1, 0, sizeof(b) - 1); //先序,中序建立二叉树
PT root = creat_in_post(b, c, 0, sizeof(a) - 1, 0, sizeof(b) - 1); //中序,后序建立二叉树
preorder(root);
cout << endl;
inorder(root);
cout << endl;
postorder(root);
cout << endl;
levelOrder(root);
cout << endl;
cout << "**********traversal without recursion**********\n";
preorderWithoutRecursion(root);
inorderWithoutRecursion(root);
postorderWithoutRecursion(root);
return 0;
}
二叉树的各种构造与遍历
最新推荐文章于 2022-12-10 15:58:07 发布