二叉樹實現

2021-10-08 15:55:05 字數 3906 閱讀 1219

#pragma once

#include

#include

typedef

char btdatatype;

typedef

struct binarytreenode

btnode;

#include

"queue.h"

// 通過前序遍歷的陣列"abd##e#h##cf##g##"構建二叉樹

btnode*

binarytreecreate

(btdatatype* a,

int n,

int* pi)

;// 二叉樹銷毀

void

binarytreedestory

(btnode* root)

;// 二叉樹節點個數

intbinarytreesize

(btnode* root)

;// 二叉樹葉子節點個數

intbinarytreeleafsize

(btnode* root)

;// 二叉樹第k層節點個數

intbinarytreelevelksize

(btnode* root,

int k)

;// 二叉樹查詢值為x的節點

btnode*

binarytreefind

(btnode* root, btdatatype x)

;// 二叉樹前序遍歷

void

binarytreeprevorder

(btnode* root)

;// 二叉樹中序遍歷

void

binarytreeinorder

(btnode* root)

;// 二叉樹後序遍歷

void

binarytreepostorder

(btnode* root)

;// 層序遍歷

void

binarytreelevelorder

(btnode* root)

;// 判斷二叉樹是否是完全二叉樹

intbinarytreecomplete

(btnode* root)

;

#include

"binarytree.h"

btnode*

binarytreecreate

(btdatatype* a,

int n,

int* pi)

btnode* root =

(btnode*

)malloc

(sizeof

(btnode));

root-

>_data = a[

*pi];(

*pi)++;

root-

>_left =

binarytreecreate

(a, n, pi);(

*pi)++;

root-

>_right =

binarytreecreate

(a, n, pi)

;return root;

}void

binarytreedestory

(btnode* root)

free

(root)

;binarytreedestory

(root-

>_left)

;binarytreedestory

(root-

>_right);}

intbinarytreesize

(btnode* root)

return1+

binarytreesize

(root-

>_left)

+binarytreesize

(root-

>_right);}

intbinarytreeleafsize

(btnode* root)

if(root-

>_left ==

null

&& root-

>_right ==

null

)return

binarytreeleafsize

(root-

>_left)

+binarytreeleafsize

(root-

>_right);}

intbinarytreelevelksize

(btnode* root,

int k)

if(k ==1)

return

binarytreelevelksize

(root-

>_left, k -1)

+binarytreelevelksize

(root-

>_right, k -1)

;}btnode*

binarytreefind

(btnode* root, btdatatype x)

if(root-

>_data == x)

binarytreefind

(root-

>_left, x)

;binarytreefind

(root-

>_right, x);}

void

binarytreeprevorder

(btnode* root)

printf

("%c "

, root-

>_data)

;binarytreeprevorder

(root-

>_left)

;binarytreeprevorder

(root-

>_right);}

void

binarytreeinorder

(btnode* root)

binarytreeinorder

(root-

>_left)

;printf

("%c "

, root-

>_data)

;binarytreeinorder

(root-

>_right);}

void

binarytreepostorder

(btnode* root)

binarytreepostorder

(root-

>_left)

;binarytreepostorder

(root-

>_right)

;printf

("%c "

, root-

>_data);}

void

binarytreelevelorder

(btnode* root)

if(root !=

null

)while(!

queueempty

(&q))if

(front-

>_right)

}queuedestroy

(&q);}

intbinarytreecomplete

(btnode* root)

if(root !=

null

)while(1

)queuepush

(&q, front-

>_left)

;queuepush

(&q, front-

>_right);}

while(!

queueempty

(&q))}

return1;

}

二叉樹實現

include include include include define maxsize 100 define ok 1 define error 0 define true 1 define false 0 typedef int status typedef int telemtype ty...

二叉樹實現

課內最近學了二叉樹,參考書上的 做了二叉樹的實現,尚不完善,還有很多地方不明白。二叉樹實現。define maxsize 100 include using namespace std 定義二叉樹節點 class btnode void createbt btnode bt,char str voi...

二叉樹 二叉樹

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