鍊錶實現二叉樹

2021-10-22 15:24:36 字數 3828 閱讀 3469

#include

#include

#include

#include

#include

/* 3 5 8 2 6 9 7

前序遍歷:3 5 2 6 8 9 7

中序遍歷:2 6 5 3 9 8 7

後序遍歷:2 6 5 9 7 8 3

樹:3(0)

5(1) 8(2)

2(3) 6(4) 9(5) 7(6)

*/#define left 1

#define right 2

typedef

struct tag_node

node;

typedef

struct tag_tree

tree;

bool createtree

(tree*

* ptree, node* proot)

;void

destroytree

(tree* ptree)

;node*

searchnodeindex

(node* pnode,

int nodeindex)

;//節點層面

node*

searchnode

(tree* ptree,

int nodeindex)

;//樹層面

bool addnode

(tree* ptree,

int nodeindex, node* pnode,

int d1irection)

;void

deleten

(node* pnode,

int direaction)

;//節點層面

bool deletenode

(tree* ptree,

int nodeindex, node* pnode)

;void

preordernode

(node* pnode)

;void

preorderprinttree

(tree* ptree)

;void

midordernode

(node* pnode)

;void

midorderprinttree

(tree* ptree)

;void

finordernode

(node* pnode)

;void

finorderprinttree

(tree* ptree)

;bool createtree

(tree*

* ptree, node* proot)

else

//else 採用鍊錶形式,不需要再左初始化

//// //建立的數的第乙個節點需要有資料,將傳進來的資料複製給第乙個節點

// else}}

void

deleten

(node* pnode,

int direaction)

if(direaction == right)

free

(pnode)

; pnode =

null;}

}void

destroytree

(tree* ptree)

//bool searchnode(tree* ptree, int nodeindex ,node *node)

//// else

// // else

//

// }

//}node*

searchnodeindex

(node* pnode,

int nodeindex)

else

else

else}}

}node*

searchnode

(tree* ptree,

int nodeindex)

bool addnode

(tree* ptree,

int nodeindex, node* pnode,

int d1irection)

else

}else}if

(d1irection == right)

else

}else}}

else

}bool deletenode

(tree* ptree,

int nodeindex, node* pnode)

if(temp->pparent->prightchild == temp)

free

(temp)

; temp =

null

;return true;

}else

}void

preordernode

(node* pnode)

}void

preorderprinttree

(tree* ptree)

void

midordernode

(node* pnode)

}void

midorderprinttree

(tree* ptree)

void

finordernode

(node* pnode)

}void

finorderprinttree

(tree* ptree)

intmain()

;//給乙個初值

二叉樹(二叉鍊錶實現)

二叉鍊錶結構的二叉樹模型,棧用自己寫的模版,佇列就不了 直接用stl的,不然 太長了 檔案 tree.h include include includeusing namespace std templateclass my stack templateclass node 結點類 node t d...

二叉鍊錶實現二叉樹

二叉樹的遍歷 前序遍歷,中序遍歷,後序遍歷,層序遍歷 二叉鍊錶的儲存實現 1 定義結構體,儲存二叉樹的結點資料,該結點的左兒子,右兒子。2 每乙個函式都要有乙個對應的私有成員 includeusing namespace std templatestruct binode templateclass...

二叉樹 鍊錶實現

include include typedef struct tree tree,ptree void init tree ptree root 初始化二叉樹 void creat tree ptree root 建立二叉樹 void pre order ptree root 先序遍歷 void b...