ADT之二叉树代码实现

最近学习下了二叉树的基本操作的coding,分享下代码,使用VS编译,下面的程序运行结果:
ADT之二叉树代码实现_第1张图片
/*

*
* 模块名称 : 二叉树
* 文件名称 : BiTree.c
* 版 本 : V1.0
* 说 明 : 使用动态分配的链式结构实现的二叉树和基本操作
* 改 进 : 增加对树动态排序;增加destroy tree 的操作;增加求树的深度和各个层数的节点数;每个节点添加父节点,方便求父节点,需要在插入函数里修改一下

*   修改记录 :
*       版本号  日期        作者     说明
*       V1.0    2016-03-27 yao  测试
*
*   Copyright (C), 2016,SCUTer
*
*********************************************************************************************************
*/
// _ooOoo_ 
// o8888888o 
// 88" . "88 
// (| -_- |) 
// O\ = /O 
// ____/`---'\____ 
// . ' \\| |// `. 
// / \\||| : |||// \ 
// / _||||| -:- |||||- \ 
// | | \\\ - /// | | 
// | \_| ''\---/'' | | 
// \ .-\__ `-` ___/-. / 
// ___`. .' /--.--\ `. . __ 
// ."" '< `.___\_<|>_/___.' >'"". 
// | | : `- \`.;`\ _ /`;.`/ - ` : | | 
// \ \ `-. \_ __\ /__ _/ .-` / / 
// ======`-.____`-.___\_____/___.-`____.-'====== 
// `=---=' 
// ............................................. 
//
#include"stdio.h"
#include"malloc.h"
#define Tree_Type int//定义数的具体类型
//定义树结点的结构体
typedef struct Tree_Node{
    Tree_Type value;
    struct Tree_Node *left;
    struct Tree_Node *right;
}TreeNode_TypeDef;
//定义树的结构体,通过结构体操作数
typedef struct Bi_tree_Struct{
    TreeNode_TypeDef root;
    unsigned char(*insert)(Tree_Type, struct Bi_tree_Struct*);
    TreeNode_TypeDef*(*find)(Tree_Type, struct Bi_tree_Struct*);
    void(*pre_order_traverse)(struct Bi_tree_Struct, void(*callback)(Tree_Type value));
}Bi_tree;
//-------------------function declare----------------------
void init_bitree(Bi_tree*Tree, Tree_Type root_value);


void print_tree_node(Tree_Type value)
{
    printf("%d ",value);
}
int main(void)
{
    //树根节点指针
     Bi_tree Tree1;
     TreeNode_TypeDef*temp;
    int i = 0xffffffff;
    init_bitree(&Tree1, 1);
    Tree1.pre_order_traverse(Tree1, print_tree_node);
    printf("\r\n"); 
    Tree1.insert(-1, &Tree1);
    if (Tree1.root.left == NULL)
    {
        if (Tree1.root.right == NULL)
            printf("insert error\r\n");
    }
    Tree1.pre_order_traverse(Tree1, print_tree_node);
    printf("\r\n");
    printf("insert error code:%d\r\n", Tree1.insert(-1, &Tree1));
    Tree1.pre_order_traverse(Tree1, print_tree_node);
    printf("\r\n");
    Tree1.insert(5, &Tree1);
    Tree1.insert(3, &Tree1);
    Tree1.insert(2, &Tree1);
    Tree1.pre_order_traverse(Tree1, print_tree_node);
    temp = Tree1.find(1, &Tree1);
    printf("find:%d\r\n", temp->value);
    while (i--);
    return 0;
}

//insert
//value:要插入的节点数据,
//选择插入的树
//return:0 sucess;
// 1:the data already exist
// 2:malloc data unsucess
static unsigned char insert(Tree_Type value, Bi_tree*Tree)
{
    TreeNode_TypeDef*current;
    TreeNode_TypeDef*temp;
    TreeNode_TypeDef**link;
    //从根节点开始
    temp = &Tree->root;
    link = &temp;
    //查找到合适的子树
    while ((current = *link) != NULL)
    {
        //根据大小选择左子树和右子树(确认没有出现重复的值)
        if (value < current->value)
        {
            link = &current->left;
        }

        else {
            if (value != current->value)
                link = &current->right;
            else
                return 1;
        }
    }
    //动态分配内存,插入新的节点
    current = malloc(sizeof(TreeNode_TypeDef));
    if (current == NULL)
        return 2;
    else
    {
        current->value = value;
        current->left = NULL;
        current->right = NULL;
        *link = current;
    }
    return 0;
}
//find
//value:要查找的节点数据,
//选择查找的树
//return:NULL don't find the node 
static TreeNode_TypeDef*find(Tree_Type value, Bi_tree*Tree)
{
    TreeNode_TypeDef*current;
    //从跟节点开始查找
    current = &Tree->root;
    while (current != NULL && current->value != value)
    {
    //选择合适的子树
        //根据大小选择左子树和右子树(确认没有出现重复的值)
        if (value < current->value)
            current = current->left;
        else {
                current = current->right;
        }
    }
    return current;
}
//前序遍历,这个函数帮助我们查看所有节点的信息
static void do_pre_order_traverse(TreeNode_TypeDef*current, void(*callback)(Tree_Type value))
{
    if (current != NULL)
    {
        callback(current->value);
        do_pre_order_traverse(current->left, callback);
        do_pre_order_traverse(current->right, callback);
    }
}
static void pre_order_traverse(Bi_tree Tree, void(*callback)(Tree_Type value))
{
    do_pre_order_traverse(&Tree.root, callback);
}
//初始化二叉树
//根节点的值
void init_bitree(Bi_tree*Tree, Tree_Type root_value)
{
    Tree->root.value = root_value;
    Tree->root.left = NULL;
    Tree->root.right = NULL;
    Tree->insert = insert;
    Tree->find = find;
    Tree->pre_order_traverse = pre_order_traverse;
}

你可能感兴趣的:(数据结构,二叉树,adt,Visual,Studio)