二叉樹的建立和遍歷

2021-09-29 14:10:29 字數 672 閱讀 5484

**#題目描述:從鍵盤接收擴充套件先序序列,以二叉鍊錶作為儲存結構,建立二叉樹。輸出這棵二叉樹的先序、中序和後序遍歷序列。二叉樹結點的data是字元型別資料, 其中#表示空格字元。

##樣例輸入:

abc##de#g##f###

##樣例輸出:

abcdegf

cbegdfa

cgefdba

#include

#include

//定義節點

typedef

struct nodebetree;

//以擴充套件的序列建立二叉樹

betree *

creattree

(betree *root)

return root;

}//先序遍歷

void

preorder

(betree *b)

}//中序遍歷

void

midorder

(betree *b)

}//後序遍歷

void

postorder

(betree *b)

}int

main()

二叉樹建立和遍歷

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