資料結構(二叉樹) 二叉樹的建立 儲存 遍歷

2021-10-10 22:32:07 字數 1511 閱讀 6486

建立二叉樹,並通過呼叫函式,,輸出先序遍歷、中序遍歷與後序遍歷的結果

#include

#include

#define true 1

#define false 0

#define ok 1

#define error 0

//datatype 是二叉樹資料元素型別,此程式定義為char型

typedef

char datatype;

typedef

struct node

bitnode,

*bitree;

void

createbitree

(bitree *bt)

}int

create

(bitree *t)

intdestroybitree

(bitree *t)

return ok;

}int

visit

(char c)

void

preorder

(bitree root)

/*先序遍歷二叉樹, root為指向二叉樹(或某一子樹)根結點的指標*/

}void

inorder

(bitree root)

/*中序遍歷二叉樹, root為指向二叉樹(或某一子樹)根結點的指標*/

}void

postorder

(bitree root)

/* 後序遍歷二叉樹,root為指向二叉樹(或某一子樹)根結點的指標*/

}void

printtree

(bitree bt,

int nlayer)

/* 按豎向樹狀列印的二叉樹 */

void

main()

else

printf

("二叉樹為空!\n");

break

;case2:

if(t)

else

printf

("二叉樹為空!\n");

break

;case3:

if(t)

else

printf

("二叉樹為空!\n");

break

;case4:

printtree

(t,0);

break

;case5:

destroybitree

(&t)

;fflush

(stdin);

createbitree

(&t)

;break

;default

: flag=0;

printf

("程式執行結束,按任意鍵退出!\n");

getchar()

;}}destroybitree

(&t)

;}

資料結構 二叉樹 反轉二叉樹

include using namespace std define maxsize 1000 struct binary tree node class queue queue queue void queue push binary tree node btn binary tree node ...

資料結構 樹結構 二叉樹 完全二叉樹 滿二叉樹

樹結構是一種描述非線性層次關係的資料結構。除根結點外,其餘每個結點有且僅有乙個直接前驅。每個結點可以有任意多個直接後繼。英文名詞表示 tree,root,node,leaf,edge,child,subtree 要麼二叉樹沒有根結點,是一棵空樹。要麼二叉樹由根結點,左子樹,右子樹組成,且左子樹和右子...

二叉樹 二叉樹

題目描述 如上所示,由正整數1,2,3 組成了一顆特殊二叉樹。我們已知這個二叉樹的最後乙個結點是n。現在的問題是,結點m所在的子樹中一共包括多少個結點。比如,n 12,m 3那麼上圖中的結點13,14,15以及後面的結點都是不存在的,結點m所在子樹中包括的結點有3,6,7,12,因此結點m的所在子樹...