二叉查找树
来源:互联网 发布:华军软件下载中心 编辑:程序博客网 时间:2024/06/07 20:02
参考资料:
http://blog.csdn.net/xiaofan086/article/details/8294382
<数据结构与算法分析>-C语言描述 Mark Allens Weiss
/* * searchTree.h * 以下为二叉查找树的定义 * Created on: 2016年4月2日 * Author: fay */#ifndef SEARCHTREE_H_#define SEARCHTREE_H_#include <stdio.h>#include <stdlib.h>typedef int ElementType;//以下定义Avl树的节点typedef struct TreeNode{ ElementType data; struct TreeNode * left;//定义左孩子 struct TreeNode * right;//定义右孩子 int height;}*Position,*SearchTree;SearchTree MakeEmpty(SearchTree T);/*以下定义树查找操作,找到最大,最小元素*/Position Find(ElementType x,SearchTree T );Position FindMax(SearchTree T);Position FindMin(SearchTree T);/*以下定义树的插入,删除,返回指定位置的元素*/SearchTree Insert(ElementType x,SearchTree T );SearchTree Delete(ElementType x,SearchTree T );ElementType Retrieve(Position T);/*定义返回树的高度*/int Height(SearchTree T);void InOrderRecur(SearchTree T);#endif /* SEARCHTREE_H_ */
/* * searchTree.c * * Created on: 2016年4月2日 * Author: fay */#include "searchTree.h"SearchTree MakeEmpty(SearchTree T) { if (T != 0) { MakeEmpty(T->left); MakeEmpty(T->right); free(T); } return 0;}Position Find(ElementType X, SearchTree T) { if (T == 0) { return 0; } if (X < T->data) { return Find(X, T->left); } else if (X > T->data) { return Find(X, T->right); } else { return T; }}Position FindMax(SearchTree T) { if (T == 0) { return 0; } else if (T->right != 0) { return FindMax(T->right); } else { return T; }}Position FindMin(SearchTree T) { if (T == 0) { return 0; } else if (T->left != 0) { return FindMin(T->left); } else { return T; }}ElementType Retrieve(Position T) { return T->data;}static int Max(int h1, int h2) { return h1 > h2 ? h1 : h2;}int Height(SearchTree T) { if (T == 0) { return -1; } else { return Max(Height(T->left), Height(T->right)) + 1; }}SearchTree Insert(ElementType X, SearchTree T) { if (T == 0) { T = (SearchTree) malloc(sizeof(struct TreeNode)); if (T == 0) { } else { T->data = X; T->height = 0; T->left = T->right = 0; } } else if (X < T->data) { T->left = Insert(X, T->left); } else if (X > T->data) { T->right = Insert(X, T->right); } T->height = Max(Height(T->left), Height(T->right)) + 1; return T;}/* * 中序遍历输出 */void InOrderRecur(SearchTree T) { static int n = 0; if (0 != T) { InOrderRecur(T->left); printf("%d \n", T->data); InOrderRecur(T->right); }}/** * 先根遍历 */void PreOrderRecur(SearchTree T) { static int n = 0; if (0 != T) { printf("%d \n", T->data); PreOrderRecur(T->left); PreOrderRecur(T->right); }}
/* ============================================================================ Name : test二叉查找树 Author : fay Version : Copyright : Your copyright notice Description : Hello World in C, Ansi-style ============================================================================ */#include "searchTree.c"#define N 15int main(void) { SearchTree T = 0; int i; int j = 0; T = MakeEmpty(0); for( i = 0,j=0; i < N; i++, j = (j + 7) % 50) { printf("j=%d \n",j); T = Insert( j, T ); } InOrderRecur(T); printf("×××××××××××××××××××\n"); PreOrderRecur(T); printf("height=%d \n", T->height); puts("插入 4 \n"); printf("===================\n"); T = Insert(4, T); InOrderRecur(T); printf("×××××××××××××××××××\n"); PreOrderRecur(T); printf("height=%d \n", T->height); return EXIT_SUCCESS;}
0 0
- 查找--二叉查找树
- 二叉树、二叉查找树
- 二叉树 & 二叉查找树
- 【查找结构】二叉查找树
- 查找之二叉树查找
- 查找之二叉树查找
- 查找:二叉查找树总结
- 二叉树查找树...
- 二叉树查找树
- 查找--遍历二叉树
- 二叉查找树
- 二叉查找树实现
- 二叉查找树
- 动态二叉查找树
- 最优二叉查找树
- 二叉查找树
- 二叉查找树
- 平衡二叉查找树
- 常用的文件和目录操作命令(转)
- 40. Combination Sum II 回溯算法
- AutoIt3 是Windows自动安装脚本语言
- 京东咚咚架构演进
- 从头学习编译过程
- 二叉查找树
- 解决eclipse开发Android时无法真机调试(使用魅族魅蓝note2实验)
- Android动画二:View动画
- Redis 的性能幻想与残酷现实
- python中模板引擎Jinja2学习
- 关于数组的一些常用函数
- Class.forName()详解
- 关于异常Canvas: trying to use a recycled bitmap android.graphics.Bitmap的解决
- input text中不能显示空格后的内容