資料結構實驗之二叉樹二:遍歷二叉樹
problem description
已知二叉樹的乙個按先序遍歷輸入的字串行,如abc,,de,g,,f,,, (其中,表示空結點)。請建立二叉樹並按中序和後序的方式遍歷該二叉樹。
input
連續輸入多組資料,每組資料輸入乙個長度小於50個字元的字串。
output
每組輸入資料對應輸出2行:
第1行輸出中序遍歷序列;
第2行輸出後序遍歷序列。
sample input
abc,,de,g,,f,,,
sample output
cbegdfa
cgefdba
1、按先序遍歷輸入的字串行:
struct node * creat()
else
return root;
}
2.已知前序遍歷和中序遍歷,求二叉樹。
struct node * creat(char *st1, char *st2, int n)
int num = k - il;
root->l = built(pl+1, pl+num, il, k-1);
root->r = built(pl+num+1, pr, k+1, ir);
3.已知中序遍歷和後序遍歷,求二叉樹。
struct node *creat(char *st1, char *st2, int n)
int num = k - il;
root->l = built(ll, ll+num-1, il, k-1);
root->r = built(ll+num, rr-1, k+1, ir);
4.層次遍歷:
void level(struct node *root)
x++;
}}
5、求二叉樹的高度
int deep(struct node *root)
6、求葉子數。
int leaf(struct node *root)
7、按從上到下從左到右的順序輸出二叉樹的葉子結點。
void leaf(struct node *root)
x++;
}}
8、後序輸出:
void after(struct node *root)
}
二叉樹知識點
樹是一種常用的非線性資料結構,用於描述分支 分層關係。一 基本概念 1 什麼是二叉樹?二叉樹是也是一種樹,乙個節點最多有兩個子樹結構。2 什麼是節點的度?節點的子樹個數,叫做節點的度,所以二叉樹節點的度最大為2。3 什麼是葉子節點?沒有子樹的節點叫葉子節點,葉子節點的度為0。4 什麼是節點的深度?節...
二叉樹的基本知識
基本 pragma once include include include include using namespace std template struct binarytreenode template class binarytree binarytree t a,size t n,co...
資料結構 二叉樹的基本知識點總結
樹是一種資料結構,它是由n n 1 個有限的結點組成的具有層次關係的集合。樹具有的特點有 1 每個結點有零個或多個子結點 2 沒有父節點的結點稱為根節點 3 每乙個非根結點有且只有乙個父節點 4 除了根結點外,每個子結點可以分為多個不相交的子樹。樹的基本術語有 若乙個結點有子樹,那麼該結點稱為子樹根...