二叉搜索树
Posted rakint
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了二叉搜索树相关的知识,希望对你有一定的参考价值。
#include <iostream>
using namespace std;
typedef struct TreeNode *SearchTree;
typedef struct TreeNode *Position;
struct TreeNode
{
int Element;
SearchTree Left;
SearchTree Right;
};
//////////////函数声明//////////////////////////
void MakeEmpty(SearchTree T); //清空一个树
SearchTree CreatTree(int x); //创建一个根为x的空树
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); //删除元素
//////////////函数定义//////////////////////////
void MakeEmpty(SearchTree T)
{
if (T != NULL)
{
MakeEmpty(T->Left);
MakeEmpty(T->Right);
delete T;// free(T);//把最后的指向根结点的指针去掉。
}
}
SearchTree CreatTree(int x)
{
SearchTree T;
T = static_cast<SearchTree>(malloc(sizeof(struct TreeNode)));
T->Element = x;//这是指二叉树的结点值
T->Left = NULL;
T->Right = NULL;
return T;//T指向一颗二叉树
}
Position Find(int x, SearchTree T)
{
if (T == NULL)
{
return NULL;
}
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 = static_cast<SearchTree>(malloc(sizeof(struct TreeNode)));
if (T == NULL)
{
cout << "out of space" << endl;
}
else
{
T->Element = x;
T->Left = NULL;
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)
{
cout << "Element not found" << endl;
}
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 = FindMin(T->Right);
T->Element = TmpCell->Element;
T->Right = Delete(T->Element, T->Right);
}
else //一个儿子或者没有儿子
{
TmpCell = T;
if (T->Left == NULL)
{
T = T->Right;
}
else
{
if (T->Right == NULL)
{
T = T->Left;
}
}
free(TmpCell);
}
}
}
}
return T;
}
int main()
{
//////////////测试//////////////////////
SearchTree Tree_1 = CreatTree(6);//根结点为6
Tree_1 = Insert(2, Tree_1);
Tree_1 = Insert(8, Tree_1);
Tree_1 = Insert(1, Tree_1);
Tree_1 = Insert(4, Tree_1);
Tree_1 = Insert(3, Tree_1);
///////////////上面的二叉树为////////////////////////////
// 6
// /
// 2 8
// /
// 1 4
// /
// 3
///////////////////////////////////////////////////////
cout << FindMax(Tree_1)->Element << endl;
cout << FindMin(Tree_1)->Element << endl;
cout << Find(2, Tree_1)->Right->Element << endl;//结点2的右下方元素
MakeEmpty(Tree_1);
return 0;
}
以上是关于二叉搜索树的主要内容,如果未能解决你的问题,请参考以下文章