C語言之二叉樹(包括遍歷的實現)

2021-08-04 16:30:57 字數 3578 閱讀 7672

//頭函式

#ifndef __2tree_h__

#define __2tree_h__

#include "error.h"

#define true 1

#define false 0

typedef struct _btree

btree;

typedef struct _head

head;

//建立樹

head *creat_tree();

//插入結點

int insert_btree(head *tree, char data, int pos, int count, int flag);

//列印二叉樹

void display (head* tree);

//結點的刪除

int delete (head *tree, int pos, int count);

//二叉樹的高度

int btree_height (head *);

//二叉樹的度

int btree_degree (head *);

//二叉樹的清空

int btree_clear (head *);

//二叉樹的銷毀

int btree_destroy (head **);

//前序遍歷

void pre_order (btree *node);

//中序遍歷

void mid_order (btree *node);

//後序遍歷

void last_order (btree *node);

#endif //__2tree_h__

//主演**

#include "2tree.h"

#include head *creat_tree()

tree->head = null;

tree->count = 0;

return tree;

}int insert_btree(head *tree, char data, int pos, int count, int flag)

btree *node = (btree *)malloc(sizeof(btree)/sizeof(char));

if(node == null)

node->data = data;

node->rchild = null;

node->lchild = null;

btree *parent = null;

btree *current = tree->head;

int way;

while(count)

if(flag == 0)

node->lchild = current;

else

node->rchild = current;

if(parent != null)

else

tree->head = node;

tree->count++;

return true ;

}void r_display(btree *node,int gap)

printf("\n");

return; }

for(i = 0; i < gap; i++)

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

if(node->lchild != null || node->rchild !=null) }

void display (head* tree)

r_display(tree->head, 0);

}void r_delete(head *tree, btree *node)

r_delete(tree, node->lchild);

r_delete(tree, node->rchild);

free(node);

tree->count--;

}int delete (head *tree, int pos, int count)

btree *parent = null;

btree *current = tree->head;

int way = 0;

while(count)

if(parent != null)

else

tree->head = null;

r_delete(tree,current);

return true;

}int r_height(btree *node)

int lh = r_height(node->lchild);

int rh = r_height(node->rchild);

return (lh > rh ? lh+1 : rh+1);

}int btree_height (head *tree)

int subheight = r_height(tree->head);

return subheight;

}int r_degree(btree *node)

int degree = 0;

if(node->rchild != null)

degree++;

if(node->lchild != null)

degree++;

if(degree == 1)

return degree; }

int btree_degree(head *tree)

int degree = r_degree(tree->head);

return degree;

}int btree_clear (head *tree)

delete(tree,0,0);

return true;

}int btree_destroy (head **tree)

btree_clear(*tree);

free(*tree);

return true;

}void pre_order (btree *node)

printf("%4c",node->data);

pre_order(node->lchild);

pre_order(node->rchild); }

void mid_order (btree *node)

mid_order(node->lchild);

printf("%4c",node->data);

mid_order(node->rchild);

}void last_order (btree *node)

last_order(node->lchild);

last_order(node->rchild);

printf("%4c",node->data);

}

//主函式

#include #include "2tree.h"

int main()

c語言遍歷二叉樹實現

include include typedef struct node bitnode,bitree void createbitree bitree bt void preorder bitree root 先序遍歷二叉樹,root為指向二叉樹 或某一子樹 根結點的指標 void inorder ...

php之二叉樹,php如何實現的二叉樹遍歷 示例

php如何實現的二叉樹遍歷 示例 建立的二叉樹如下圖所示 php 如下所示 class node public value public child left public child right final class ergodic 前序遍歷 先訪問根節點,再遍歷左子樹,最後遍歷右子樹 並且在遍...

(4 2)樹與二叉樹之二叉樹的遍歷

typedef struct bitreenode bitreenode,bitree void preorder bitree bt typedef struct bitreenode bitreenode,bitree void inorder bitree bt typedef struct ...