二叉樹 鍊錶實現

2021-07-10 22:46:17 字數 2422 閱讀 8920

# include # include typedef struct tree

tree, * ptree;

void init_tree(ptree &root); //初始化二叉樹

void creat_tree(ptree &root); //建立二叉樹

void pre_order(ptree &root); //先序遍歷

void back_order(ptree &root); //後序遍歷

void mid_order(ptree &root); //中序遍歷

void leve_order(ptree &root); //層序遍歷

int depth_tree(ptree &root); //二叉樹深度

int leaf_tree(ptree &root); //二叉樹節點個數

bool seach_tree(ptree &root,char key); //尋找二叉樹中的值

bool destroy_tree(ptree &root); //銷毀二叉樹

void assign_tree(ptree &root,char ch,char value); //更改二叉樹節點的值

void parent_tree(ptree &root,char value); //找雙親結點

void leftchild_tree(ptree &root, char value); //左孩子結點

void leftbrother_tree(ptree &root,char value); //左兄弟結點

void rightchild_tree(ptree &root,char value); //右孩子結點

void rightbrother_tree(ptree &root,char value); //右兄弟結點

void delete_tree(ptree &root,char ch,int bl); //刪除結點

void insert_tree(ptree &root1,ptree &root2 ,char ch,int bl); //插入子樹

int main (void)

void init_tree(ptree &root)

void creat_tree(ptree &root)

else }

void pre_order(ptree &root)

}void mid_order(ptree &root)

else

return;

}void back_order(ptree &root)

}void leve_order(ptree &root) }}

int depth_tree(ptree &root)

}int leaf_tree(ptree &root)

else

return 0;

}bool seach_tree(ptree &root,char key) }}

bool destroy_tree(ptree &root)

}void assign_tree(ptree &root,char ch,char value)

}void parent_tree(ptree &root,char value)

if (root->lchild != null && root->rchild->data == value || root->rchild != null && root->rchild->data == value)

printf ("%c \n",root->data);

else

}}void leftchild_tree(ptree &root, char value)

else

}}void rightchild_tree(ptree &root,char value)

else

}}void leftbrother_tree(ptree &root,char value)

if (root->lchild != null && root->lchild->data == value)

printf ("此節點為左孩子\n");

if (root->rchild != null && root->rchild->data == value)

else

}}void rightbrother_tree(ptree &root,char value) }}

void delete_tree(ptree &root,char ch,int bl)

else

}}void insert_tree(ptree &root1,ptree &root2 ,char ch,int bl)

else

}else

}}

二叉樹(二叉鍊錶實現)

二叉鍊錶結構的二叉樹模型,棧用自己寫的模版,佇列就不了 直接用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 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 le...