二叉樹的建立和遍歷

2022-06-02 12:51:12 字數 806 閱讀 5445

可學習的部落格:

實現**:

1 #include 2 #include 

3 #include 45//

定義節點

6 typedef struct

binodebinode,*bitree;

1112

//先序拓展序列建立二叉樹

13void create(bitree &t)

1424}25

26int treedeep(bitree t)//

樹的深度

2734}35

36//

先序遍歷 (遞迴)

37void

preorder (bitree t)

3845}46

47//

中序遍歷 (遞迴)

48void

inorder (bitree t)

4957

} 58

59//

後序遍歷 (遞迴)

60void

postorder (bitree t)

6168

} 69

70//

求二叉樹葉子結點個數

7172

int leafcount(bitree t,int &num)

7380

return

num;81}

8283

intmain()

84

二叉樹建立和遍歷

二叉樹建立遍歷規則 1.先序 根 左 右 2.中序 左 根 右 3.後序 左 右 根 二叉樹定義和輔助函式如下 struct node void visit int data int indata 先序建立二叉樹 struct node createbitree 先序建立乙個二叉樹 return t...

二叉樹建立和遍歷

include include 帶返回值建立二叉樹 最簡單方法 節點資料結構 struct bs node typedef struct bs node tree tree head,p,root 建立二元查詢樹 有返回值的可以不傳參 沒有的話如何傳參 輸入0代表到了某個葉子節點 tree crea...

二叉樹建立和遍歷

二叉樹建立遍歷規則 1.先序 根 左 右 2.中序 左 根 右 3.後序 左 右 根 二叉樹定義和輔助函式例如以下 struct node void visit int data int indata 先序建立二叉樹 struct node createbitree 先序建立乙個二叉樹 return...