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);}
阅读全文
0 0
- C数据结构-二叉搜索树BSTree
- 二叉搜索树BSTree
- [C++]数据结构:基于二叉树类的二叉搜索树BSTree类的创建与使用
- 【转】二叉搜索树BSTree源码
- [BSTree] 二叉搜索树 -- C++ 模板实现
- 【BSTree】深入理解二叉搜索树
- 利用二叉搜索树来实现排序 BSTree(pku 1002)
- <C/C++数据结构>二叉搜索树
- 数据结构(c++)<二叉搜索树>
- 数据结构二叉搜索树
- 数据结构-----二叉搜索树
- 数据结构:二叉搜索树
- 数据结构 ---- 二叉搜索树
- 数据结构-二叉搜索树
- 【数据结构】二叉搜索树
- 【数据结构】二叉搜索树
- 数据结构---------二叉搜索树
- 数据结构--‘搜索二叉树’
- 深入理解java虚拟机-第一章:java虚拟机内存结构
- asp.net页面的地址栏中显示自定义的ICO图标
- Android wifiManager的使用案例
- 常用命令
- 二次规划
- C数据结构-二叉搜索树BSTree
- 最完整的php.ini中文注释
- Mist钱包的安装使用
- 将图片文件分解,再利用管道流合成 存储对象
- PyQt5编写简单界面
- assert_enum_string
- 前端基础
- cocos2dx解压zip
- Cousera 机器学习 week4