第4章 树 - 二叉搜索树数据结构实现
来源:互联网 发布:vue删除数组中的对象 编辑:程序博客网 时间:2024/06/02 04:18
在这里挖个坑,以后忘记了,再回到这里查询下
头文件
#ifndef _Tree_Hstruct TreeNode;typedef struct TreeNode *Position;typedef struct TreeNode *SearchTree;SearchTree MakeEmpty( SearchTree T);Position Find( int X, SearchTree T);Position FindMin( SearchTree T);Position FindMax( SearchTree T);SearchTree Insert( int X, SearchTree T);SearchTree Delete( int X, SearchTree T);int Retrieve( Position P);int Depth( SearchTree T);#endifcpp文件
#include <iostream>#include "SearchTree.h"using namespace std;struct TreeNode{int Element;SearchTree Left;SearchTree Right;};SearchTree MakeEmpty( SearchTree T){if(T != NULL){MakeEmpty(T->Left);MakeEmpty(T->Right);free(T);}return T;}Position Find( int X, SearchTree T){if(T == NULL)return NULL;else if(X < T->Element)return Find(X, T->Left);else if(X > T->Element)return Find(X, T->Right);else return T;}Position FindMin(SearchTree T){if(T == NULL)return NULL;else if(T->Left == NULL)return T;else return FindMin(T->Left);}Position FindMax(SearchTree T){if(T != NULL)while (T->Right != NULL)T = T->Right;return T;}SearchTree Insert( int X, SearchTree T){if(T == NULL){T = (TreeNode*)malloc(sizeof(TreeNode));T->Element = X;T->Left = T->Right = NULL;}else if(X < T->Element)T->Left = Insert(X, T->Left);else if(X > T->Element)T->Right = Insert(X, T->Right);return T;}SearchTree Delete( int X, SearchTree T){Position TmpCell;if(T == NULL)return NULL;else if(X < T->Element)T->Left = Delete(X, T->Left);else if(X > T->Element)T->Right = Delete(X, T->Right);else if(T->Left && T->Right){TmpCell = T;T = FindMin(T->Right);T->Right = Delete(T->Element, T->Right);free(TmpCell);}else{TmpCell = T;if(T->Left != NULL)T = T->Left;if(T->Right != NULL)T = T->Right;free(TmpCell);}return T;}<pre name="code" class="cpp">int Depth( SearchTree T){int DepthLeft, DepthRight;if(T == NULL)return 0;else{DepthLeft = Depth(T->Left);DepthRight = Depth(T->Right);if(DepthLeft >= DepthRight)return DepthLeft + 1;elsereturn DepthRight + 1;}}int Retrieve( Position P){return P->Element;}
0 0
- 第4章 树 - 二叉搜索树数据结构实现
- 数据结构 二叉搜索树实现
- 二叉树应用-二叉搜索树类模板的实现(数据结构基础 第6周)
- 算法导论-第12章-二叉搜索树:随机二叉搜索树数据结构C++实现(前中后序遍历,插入,搜索,前后毗邻元素,最大最小值)
- 二叉搜索树数据结构完整实现
- 二叉搜索树基本数据结构实现
- 数据结构(一)二叉搜索树-递归实现
- 《C++实现数据结构》:二叉搜索树
- 数据结构-二叉搜索树的实现
- 数据结构二叉搜索树
- 数据结构-----二叉搜索树
- 数据结构:二叉搜索树
- 数据结构 ---- 二叉搜索树
- 数据结构-二叉搜索树
- 【数据结构】二叉搜索树
- 【数据结构】二叉搜索树
- 数据结构---------二叉搜索树
- 数据结构--‘搜索二叉树’
- 归并排序同时计算逆序对数
- C# sqlserver
- C++ primer(第五版) 练习 7.9 个人 code
- 北方之星Java面试题
- 线段树
- 第4章 树 - 二叉搜索树数据结构实现
- SEO学习笔记(四)
- HDU 4941 Magical Forest(离散化)
- 栈之顺序栈
- uva--10220
- 电脑问题123
- php中对MYSQL操作之预处理技术(2)数据库dql查询语句
- HBase的伪分布模式安装
- SEO学习笔记(五)