由遍歷序列構造二叉樹

2022-09-06 23:15:26 字數 1061 閱讀 1264

#include#include

#include

using

namespace

std;

#define maxsize 100

#define maxwidth 40typedef

char

elemtype;

typedef

struct

node

btnode;

btnode *createbt1(char *pre,char *in,int n)//

由先序和中序遍歷序列構造二叉樹

btnode *createbt2(char *post,char *in,int n,int m)//

由中序和後序遍歷序列構造二叉樹

}s=(btnode *)malloc(sizeof(btnode));//

建立二叉樹結點*s

s->data=post[maxpost];

s->lchild=createbt2(post,in

,maxin,m);

s->rchild=createbt2(post,maxp+1,n-maxin-1

,m);

returns;}

void dispbtnode(btnode *b)//

以括號表示法輸出二叉樹

}}void dispbtnode1(btnode *b)//

以凹入表示法輸出一顆二叉樹

for(i=1;i<=n;i++)//

其中n為顯示場寬,字元以右對齊顯示

printf("");

printf(

"%c(%c)

",p->data,type);

for(i=n+1;i<=maxwidth;i+=2

) printf("--

");printf("\n

");top--;

if(p->rchild!=null)

if(p->lchild!=null)}}

}void

main()

由遍歷序列構造二叉樹

由二叉樹的先序 中序 後序 中序 層次 中序都可以唯一的確定一棵二叉樹。需要注意的是,如果只知道二叉樹的先序序列和後序序列是無法唯一確定一棵二叉樹的。題意 輸入某二叉樹的先序遍歷和中序遍歷結果,請重建出該二叉樹,並輸出它的後序遍歷序列。解題思路 在二叉樹的先序遍歷中,第乙個數字是根結點,在中序遍歷中...

由遍歷序列構造二叉樹

根據前序中序確定二叉樹 根據後序中序確定二叉樹 根據層次中序確定二叉樹 注意,根據前序和後序無法確定一棵二叉樹 include include include include include include using namespace std define elemtype char typed...

二叉樹 根據二叉樹遍歷序列構造二叉樹

二叉樹的節點型別宣告如下 struct btnode 定理1任何 n 0 個不同節點的二叉樹,都可由它的前序序列和中序序列唯一地確定。根據前序遍歷的特點,知前序序列 presequence 的首個元素 presequence 0 為二叉樹的根 root 然後在中序序列 insequence 中查詢此...