数据结构--二叉查找树的思想与实现

   二叉查找树(Binary Search Tree)又称二叉排序树(Binary Sort Tree)或者二叉搜索树,定义如下:

   二叉查找树或者是一棵空树,或者是具有下列性质的二叉树(一个结点不能多余两个儿子): (1)若左子树不空,则左子树上所有结点的值均小于或等于它的根结点的值; (2)若右子树不空,则右子树上所有结点的值均大于或等于它的根结点的值; (3)左、右子树也分别为二叉排序树;      如下图所示,便是一颗二叉查找树:
《数据结构--二叉查找树的思想与实现》

  二叉查找树的基本操作有寻找最大值,最小值,查找指定的值,插入元素,删除元素等等,均可以用递归的方法实现,具体代码如下:

 

#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;
}
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);
   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;
   MakeEmpty (Tree_1);
    return 0;
}

  

    上面的函数多用递归实现,好好分析这些函数有利于理解递归的含义。

 

    夜深了,我该怎么办?

 

    原文作者:算法小白
    原文地址: https://www.cnblogs.com/1242118789lr/p/6729843.html
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞