二叉樹的用法

2021-07-31 07:15:15 字數 1647 閱讀 5268

定義二叉樹

#include

#include

#define queue_maxsize 50

typedef

char data;

//定義元素型別

typedef

struct chaintree//定義二叉樹的節點型別

chainbintree;

初始化二叉樹

chainbintree *

bintreeinit

(chainbintree *node)

//初始化二叉樹的根節點

新增新節點到二叉樹

//新增資料到二叉樹,bt為父節點,node為子節點,n=1表示新增左子樹,n=2表示新增右子樹

intbintreeaddnode

(chainbintree *bt,chainbintree *node,

int n)

switch

(n)else

bt->left=node;

break

;case1:

//新增右節點

if(bt-

>right)

//右子樹不為空

else

bt->right=node;

break

;default

:printf

("引數錯誤!\n");

return0;

}return1;

}

獲取左右叉樹

chainbintree *

bintreeleft

(chainbintree *bt)

//返回左子結點

chainbintree *

bintreeright

(chainbintree *bt)

//返回右子節點

獲取二叉樹的狀態

int

bintreeisempty

(chainbintree *bt)

//檢查二叉樹是否為空,為空則返回1,否則返回0

計算二叉樹深度

int

bintreedepth

(chainbintree *bt)

}

二叉樹查詢

chainbintree *

bintreefind

(chainbintree *bt,data data)

//在二叉樹中查詢值為data的節點

}}

清空二叉樹

void

bintreeclear

(chainbintree *bt)

//清空二叉樹,使之變成一顆空樹

return

;}

二叉樹用法

二叉樹的建立 二叉樹的遍歷 二叉堆的基本用法建立二叉樹 先序,中序,後序 typedef struct node typedef node tree struct node tree bt void built tree bt 二維指標一不小心就出錯,還是引用好 二叉樹的遍歷 一.三種遍歷。void...

二叉樹的用法

include include define queue maxsize 50 typedef char data 定義元素型別 typedef struct chaintree 定義二叉樹的節點型別 chainbintree chainbintree bintreeinit chainbintre...

二叉樹的用法

1 用二叉鍊錶作為儲存結構 2 分別按先序,中序和後序遍歷二叉樹 3 編寫交換二叉樹中所有結點左右孩子的非遞迴演算法。二 實驗方案 include include include define stack max size 30 define queue max size 30 ifndef ele...