# 二叉查找树的部分例程

searchtree.h

#ifndef SEARCHTREE_H_INCLUDED
#define SEARCHTREE_H_INCLUDED

typedef  int ElementType;
struct treenode;
typedef  struct treenode *Position;
typedef  Position  SearchTree;
struct  treenode
{
ElementType  data;
SearchTree  left;
SearchTree  right;
};

SearchTree  MakeEmpty(SearchTree  T);
Position  Find(ElementType  X,SearchTree  T);
Position  FindMin(SearchTree  T);
Position  FindMax(SearchTree  T);
SearchTree  Insert(ElementType  X,SearchTree  T);
SearchTree  Delete(ElementType  X,SearchTree  T);

#endif // SEARCHTREE_H_INCLUDED


searchtree.c

#include  <stdio.h>
#include  <malloc.h>
#include   "searchtree.h"

SearchTree  MakeEmpty(SearchTree  T)
{
if(T!=NULL)
{
MakeEmpty(T->left);
MakeEmpty(T->right);
free(T);
}

return  NULL;
}

Position  Find(ElementType  X,SearchTree  T)
{
if(T==NULL)
return  NULL;
else if(X <T->data)
return  Find(X, T->left);
else if(X >T->data)
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(ElementType  X,SearchTree  T)
{
if(T==NULL)
{
T=(SearchTree)malloc(sizeof(struct treenode));
if(T==NULL)
printf("Error: out of space!!!");
else
{
T->data=X;
T->left =T->right =NULL;
}
}
else  if(X <T->data)
{
T->left =Insert(X, T->left);
}
else  if(X >T->data)
{
T->right  =Insert(X , T->right);
}

return T;   /**< don't forget  this line!!!! */
}

SearchTree  Delete(ElementType  X,SearchTree  T)
{
Position  temp;

if(T==NULL)
printf("Error: not this element!!!");
else if(X <T->data)
T->left =Delete(X, T->left);   /**<  Go left */
else if(X >T->data)
T->right =Delete(X, T->right);  /**< go  right */
else     /**< find  element,delete it */
{
if(T->left && T->right)
{
temp =FindMin(T->right) ;  /**< the minest value  in right subtree */
T->data =temp->data;
T->right =Delete(T->data, T->right);   /**< 删去右子树中的最小节点，可以编写deletemin函数以提高效率 */
}
else
{
temp =T;
if(T->left ==NULL)
T=T->right;
else
T=T->left;
free(temp);
}
}

return  T;
}

#### 实现基本二叉查找树操作的例程

2015-01-12 22:04:27

#### 二叉查找树的基本例程

2012-11-07 20:13:50

#### 二叉树搜索树的懒惰删除及相关的其他例程

2018-01-18 18:00:37

#### LintCode 85-在二叉查找树中插入节点

2017-01-04 18:12:30

#### Java-如何创建二叉查找树

2017-03-13 07:35:23

#### LintCode_二叉查找树迭代器

2017-08-23 21:21:12

#### lintcode(95)验证二叉查找树

2017-04-26 10:51:45

#### 二叉搜索树 （ 二叉查找树）（ 二叉排序树）的定义和遍历

2014-09-02 16:03:46

#### LintCode（85）在二叉查找树中插入节点

2016-06-15 21:41:27

#### LintCode 不同的二叉查找树

2015-12-08 22:03:15