赞
踩
给你一个括号表示法表示的二叉树,建立一棵用二叉链表方式存储的二叉树,并利用凹凸法进行二叉树的打印;并对其进行遍历(先序、中序、后序和层序),并打印输出遍历结果。
输入:A(B(C,D(E,F(G))))
输出:
先序遍历:ABCDEFG
中序遍历:CBEDGFA
后序遍历:CEGFDBA
层序遍历:ABCDEFG
- #pragma GCC optimize(3,"Ofast","inline")
- #include<iostream>
- #include<cstdio>
- #include<algorithm>
- #include<string>
- #include<vector>
- #include<cstring>
- #include<queue>
- #include<stack>
- #include<list>
- #include<map>
- #include<set>
- #include<cmath>
- #include<sstream>
- #include<cstdlib>
- #include<bitset>
- #include<climits>
- #include<functional>
- #define F(i,s,t) for(int i=(s);i<=(t);i++)
- #define D(i,s,t) for(int i=(s);i>=(t);i--)
- #define dBug(i) printf("Value=%d\n",i)
- #define ddBug(i,j) printf("Value=%d %d\n",i,j)
- #define ed putchar('\n')
- #define FO freopen("D:\\in.txt","r",stdin)
- #define IOS cin.tie(0) ,cout.tie(0), cout.sync_with_stdio(0)
- typedef long long ll;
- //const int INF = 1 << 30;
- //const double EPS = 1e-6;
- //#define MX 102
- //#define Mod 10000
- using namespace std;
-
- typedef struct Node{
- char data;
- Node* LChild;
- Node* RChild;
- }BiTree;
-
- //构造二叉链表
- BiTree* CreatBiTree(char str1[])
- {
- static BiTree *root = NULL;
- BiTree *demoFather[600], *TreePoint;
- int demoPoint = -1, curSurTree, strPoint = 0;
- char temp;
-
- temp = str1[strPoint]; //ch指向str1第一个字符,用[]访问
-
- while (temp != '\0'){
- switch (temp){
- case '(':
- demoPoint++;
- demoFather[demoPoint] = TreePoint; //记录括号里元素的父节点
- curSurTree = 1;
- break;
- case')':
- demoPoint--;
- break;
- case',':
- curSurTree = 2;
- break;
-
- default:
- TreePoint = (BiTree*)malloc(sizeof(BiTree));
- TreePoint->data = temp;
- TreePoint->LChild = TreePoint->RChild = NULL;
-
- if (root == NULL){
- root = TreePoint;
- }//if
- else{
- curSurTree == 1 ? demoFather[demoPoint]->LChild = TreePoint : demoFather[demoPoint]->RChild = TreePoint;
- }//else
- }//switch
-
- strPoint++;
- temp = str1[strPoint];
- }//while
- return root;
- }
-
-
- /*先序遍历二叉树, root为指向二叉树根结点的指针*/
- void PreOrderRecursion(BiTree *root)
- {
- if (root != NULL){
- printf("%c",root->data);
- PreOrderRecursion(root->LChild);
- PreOrderRecursion(root->RChild);
- }//if
- }
-
- /*中序遍历二叉树, root为指向二叉树根结点的指针*/
- void InOrderRecursion(BiTree *root)
- {
- if (root != NULL){
- InOrderRecursion(root->LChild);
- printf("%c", root->data);
- InOrderRecursion(root->RChild);
- }//if
- }
-
- /* 后序遍历二叉树,root为指向二叉树(或某一子树)根结点的指针*/
- void PostOrderRecursion(BiTree *root)
- {
- if (root != NULL){
- PostOrderRecursion(root->LChild);
- PostOrderRecursion(root->RChild);
- printf("%c",root->data);
- }//if
- }
-
- /* 先序遍历二叉树的非递归算法 */
- void PreOrder(BiTree *root)
- {
- BiTree *temp;
- stack<BiTree*> S;
-
- temp = root;
- while (!S.empty() || temp != NULL){
- while (temp != NULL){
- printf("%c", temp->data);
- S.push(temp);
- temp = temp->LChild;
- }//inner whlie
-
- if (!S.empty()){
- temp = S.top();
- S.pop();
- temp = temp->RChild;
- }//if
- }//extren while
- }
-
- /* 中序遍历二叉树的非递归算法 */
- void InOrder(BiTree *root)
- {
- BiTree *temp;
- stack<BiTree*> S;
-
- temp = root;
- while (!S.empty() || temp != NULL){
- while (temp != NULL){
- S.push(temp);
- temp = temp->LChild;
- }//inner whlie
-
- if (!S.empty()){
- temp = S.top();
- S.pop();
- printf("%c",temp->data);
- temp = temp->RChild;
- }//if
- }//extren while
- }
-
- /* 后序遍历二叉树的非递归算法 */
- void PostOrder(BiTree *root)
- {
- stack<BiTree*> S;
- BiTree *temp = root, *r = NULL;
- while (temp || !S.empty()){
- if (temp){
- S.push(temp);
- temp = temp->LChild;
- }//if
- else{
- temp = S.top();
- if (temp->RChild && temp->RChild != r){
- temp = temp->RChild;
- }//if
- else{
- temp = S.top();
- S.pop();
- printf("%c",temp->data);
- r = temp; // r 记录弹出的结点
- temp = NULL; //temp 调到NULL , 为了不让再往栈中添加结点了
- }//else
- }//extren else
- }//while
- }
-
- /* 层序遍历二叉树 */
- void LayerOrder(BiTree *root)
- {
- BiTree *temp;
- queue<BiTree*> Q;
- Q.push(root);
-
- while (!Q.empty()){
- temp = Q.front();
- Q.pop();
- printf("%c",temp->data);
- if (temp->LChild != NULL){
- Q.push(temp->LChild);
- }
- if (temp->RChild != NULL){
- Q.push(temp->RChild);
- }
- }//while
- }
-
- void PrintTree(BiTree *root, int nLayer)
- {
- if (root == NULL){
- return;
- }
-
- PrintTree(root->RChild,nLayer + 3);
- for (int i = 0; i < nLayer; i++){
- printf(" ");
- }
- printf("%c\n",root->data);
- PrintTree(root->LChild, nLayer + 3);
- }
-
- int main()
- {
- BiTree *T = NULL;
- char str1[1000]; //请输入括号法表示的二叉树序列
- printf("请输入括号法表示的二叉树序列:\n");
- scanf("%s", str1);
- printf("括号法表示的二叉树序列为:%s\n", str1);
-
- T = CreatBiTree(str1);//&T为BiNode类型指针的地址
- printf("先序遍历输出序列为:");
- PreOrderRecursion(T);
- printf("\n中序遍历输出序列为:");
- InOrderRecursion(T);
- printf("\n后序遍历输出序列为:");
- PostOrderRecursion(T);
-
- printf("\n先序非递归遍历输出序列为:");
- PreOrder(T);
- printf("\n中序非递归遍历输出序列为:");
- InOrder(T);
- printf("\n后序非递归遍历输出序列为:");
- PostOrder(T);
-
- printf("\n层序遍历输出序列为:");
- LayerOrder(T);
-
- printf("\n竖向打印二叉树:\n");
- PrintTree(T, 1);
- return 0;
- }

上面代码实现了递归和非递归的遍历。
非递归遍历中,用栈来存储结点,为了可以在遍历它的孩子时,还可以通过栈找到孩子结点的父结点。
而后序非递归遍历较前两种遍历方法比较难实现,原因在于需要遍历完左子树,遍历完右子树,最后才去访问根节点。这样栈顶结点可能会从他的左子树返回,也有可能从他的右子树返回,需要区分这种情况,如果是第一次从左子树返回,那么还需要去遍历其右子树,如果是从右子树返回,那么直接返回该结点就可以了。这里使用辅助指针来区分来源。
- temp = S.top();
- if (temp->RChild && temp->RChild != r){
- temp = temp->RChild;
- }//if
- else{
- temp = S.top();
- S.pop();
- printf("%c",temp->data);
- r = temp; // r 记录弹出的结点
- temp = NULL; //temp 调到NULL , 为了不让再往栈中添加结点了
- }//else
好的!下篇实现Moris遍历!
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。