C数据结构-二叉搜索树BSTree

来源:互联网 发布:软件或者神器 知乎 编辑:程序博客网 时间:2024/06/17 09:09

二叉搜索树BSTree

#ifndef BSTREE_H#define BSTREE_H#ifndef NULL#define NULL 0#endif/* 元素类型 */typedef int elem_t;/* 节点结构体 */typedef struct tag_bstree_node{    elem_t data;                    /* 数据域 */    struct tag_tree_node *lchild;   /* 左孩子指针 */    struct tag_tree_node *rchild;   /* 右孩子指针 */}bstree_node,bstree;/** * 创建bstree * @return 返回NULL */bstree *bstree_create();/** * 创建新节点 * @param pe * @return */bstree_node *bstree_create_node(elem_t *pe);/** * 插入新节点 * @param root * @param pnode * @return */bstree *bstree_insert(bstree *root, bstree_node *pnode);/** * 删除节点 * @param root * @param pe * @return */bstree_node *bstree_remove(bstree_node *root,elem_t *pe);/** * 查找节点 * @param ptree * @param pe * @return */bstree_node *bstree_find(bstree *ptree, elem_t *pe);/** * 销毁bstree * @param ptree */void bstree_destroy(bstree *ptree);/** * 先序遍历 * @param root * @param handler 处理函数指针 */void bstree_pre_travel(bstree_node *root,void (*handler)(bstree_node *));/** * 中序遍历 * @param root * @param handler 处理函数指针 */void bstree_mid_travel(bstree_node *root,void (*handler)(bstree_node *));/** * 后序遍历 * @param root * @param handler 处理函数指针 */void bstree_post_travel(bstree_node *root,void (*handler)(bstree_node *));#endif // BSTREE_H


#include "BSTree.h"#include <malloc.h>/*** 创建bstree* @return 返回NULL*/bstree *bstree_create(){    return NULL;}bstree_node *bstree_create_node(elem_t *pe){    bstree_node *pnode = (bstree_node *)malloc(sizeof(bstree_node));    if(pnode)    {        pnode->data = *pe;        pnode->lchild = NULL;        pnode->rchild = NULL;    }    return pnode;}bstree *bstree_insert(bstree_node *root, bstree_node *pnode){    if(pnode == NULL)        return root;    else if(root == NULL)        return pnode;    if(pnode->data > root->data)    {        root->rchild = bstree_insert(root->rchild,pnode);    }    else if(pnode->data < root->data)    {        root->lchild = bstree_insert(root->lchild,pnode);    }    else    {        free(pnode);        pnode = NULL;    }    return root;}bstree_node *bstree_remove(bstree_node *root,elem_t *pe){    /* 递归所有都没有找到,则返回NULL */    if(root == NULL)        return NULL;    /* 递归找到待删除的节点 */    if(*pe > root->data)    {        root->rchild = bstree_remove(root->rchild,pe);    }    else if(*pe < root->data)    {        root->lchild = bstree_remove(root->lchild,pe);    }    /* 如果不大不小就是找到了 */    else    {        bstree_node *tmp;        /* 判断是否有左子树 */        if(root->lchild != NULL)        {            for(tmp = root->lchild;tmp->rchild != NULL;tmp = tmp->rchild);            /* 将左子树中最大值替换到被删除的节点位置 */            root->data = tmp->data;            /* 删除原tmp位置的元素 */            root->lchild = bstree_remove(root->lchild,pe);        }        /* 判读是否有右子树 */        else if(root->lchild != NULL)        {            for(tmp  = root->rchild;tmp->lchild != NULL;tmp = tmp->lchild);            root->data = tmp->data;            root->rchild = bstree_remove(root->rchild,pe);        }        /* 没有孩子节点,直接释放该节点 */        else        {            free(root);            root = NULL;            return NULL;        }    }    return root;}bstree_node *bstree_find(bstree_node *root, elem_t *pe){    if(root == NULL)        return NULL;    if(*pe > root->data)        return bstree_find(root->rchild,pe);    else if(*pe < root->data)        return bstree_find(root->lchild,pe);    else        return root;}void bstree_destroy(bstree *ptree){    if (ptree == NULL)        return;    if(ptree->lchild != NULL)    {        bstree_destroy(ptree->lchild);    }    if(ptree->rchild != NULL)    {        bstree_destroy(ptree->rchild);    }    if(ptree->lchild == NULL && ptree->rchild == NULL)    {        free(ptree);    }}void bstree_pre_travel(bstree_node *root,void (*handler)(bstree_node *)){    if(root == NULL)        return;    handler(root);    bstree_pre_travel(root->lchild,handler);    bstree_pre_travel(root->rchild,handler);}void bstree_mid_travel(bstree_node *root,void (*handler)(bstree_node *)){    if(root == NULL)        return;    bstree_mid_travel(root->lchild,handler);    handler(root);    bstree_mid_travel(root->rchild,handler);}void bstree_post_travel(bstree_node *root,void (*handler)(bstree_node *)){    if(root == NULL)        return;    bstree_post_travel(root->lchild,handler);    bstree_post_travel(root->rchild,handler);    handler(root);}


原创粉丝点击