二叉樹的建立和遍歷(c語言描述)

2022-06-24 16:21:13 字數 1053 閱讀 3115

二叉樹是比較特殊的樹,二叉樹的儲存方式有順序儲存和鏈式儲存,我們基本上都是用的鏈式儲存,

1.宣告結構體

typedef char

elemtype;

typedef

struct

bitnode

bitnode,*bitree;

2.建立二叉樹

採用遞迴的方式:這裡採用的是按照先序序列建立二叉樹,

void createbitree(bitree *t)

else

(*t)->data=s;

createbitree(&(*t)->lchild);

createbitree(&(*t)->rchild);

}}

所有的**如下:

#include#include

typedef

char

elemtype;

typedef

struct

bitnode

bitnode,*bitree;

//二叉樹的建立 按照先序遍歷建立

void createbitree(bitree *t)

else

(*t)->data=s;

createbitree(&(*t)->lchild);

createbitree(&(*t)->rchild);

}}//

二叉樹先序遍歷

void preordervisit(bitree t) //

這裡不要使用bitnode *t , 不要直接對樹進行操作 }//

二叉樹中序遍歷

void middlevisit(bitree t) //

這裡不要使用bitnode *t , 不要直接對樹進行操作 }//

二叉樹後序遍歷

void postvisit(bitree t) //

這裡不要使用bitnode *t , 不要直接對樹進行操作

}int

main()

二叉樹建立和遍歷 C

題目描述 編乙個程式,讀入使用者輸入的一串先序遍歷字串,根據此字串建立乙個二叉樹 以指標方式儲存 例如如下的先序遍歷字串 abc de g f 其中 表示的是空格,空格字元代表空樹。建立起此二叉樹以後,再對二叉樹進行中序遍歷,輸出遍歷結果。輸入描述 輸入包括1行字串,長度不超過100。輸出描述 可能...

二叉樹建立和遍歷

二叉樹建立遍歷規則 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...