BST的实现

//--------------------BST.h--------------------------- 
#include <iostream>

#ifndef BINARY_SEARCH_TREE
#define BINARY_SEARCH_TREE

template <typename DataType>
class BST
{
 public:
  BST();
  bool empty() const;
  bool search(const DataType & item) const; 
  void insert(const DataType & item);
  void remove(const DataType & item);
  void inorder(ostream & out) const;
  void graph(ostream & out) const;
 private:
  class BinNode 
  {
   public:
    DataType data;
    BinNode * left;
    BinNode * right;
    BinNode()
    : left(0), right(0)
    {}

    BinNode(DataType item)
    : data(item), left(0), right(0)
    {}
 
  };
  typedef BinNode * BinNodePointer; 
  
  void search2(const DataType & item, bool & found,
               BinNodePointer & locptr, BinNodePointer & parent) const;
  void inorderAux(ostream & out, 
                  BinNodePointer subtreePtr) const;
  void graphAux(ostream & out, int indent,
                      BinNodePointer subtreeRoot) const;
  BinNodePointer myRoot; 
}; 

template <typename DataType>
inline BST<DataType>::BST()
: myRoot(0)
{}

template <typename DataType>
inline bool BST<DataType>::empty() const
{ return myRoot == 0; }

template <typename DataType>
bool BST<DataType>::search(const DataType & item) const
{
   BST<DataType>::BinNodePointer locptr = myRoot;
   bool found = false;
   while (!found && locptr != 0)
   {
      if (item < locptr->data)       
        locptr = locptr->left;
      else if (locptr->data < item)  
        locptr = locptr->right;
      else                          
        found = true;
   }
   return found;
}

template <typename DataType>
inline void BST<DataType>::insert(const DataType & item)
{
   BST<DataType>::BinNodePointer 
        locptr = myRoot,   
        parent = 0;        
   bool found = false;    
   while (!found && locptr != 0)
   {
      parent = locptr;
      if (item < locptr->data)      
         locptr = locptr->left;
      else if (locptr->data < item)  
         locptr = locptr->right;
      else                           
         found = true;
   }
   if (!found)
   {                                
      locptr = new BST<DataType>::BinNode(item);  
      if (parent == 0)              
         myRoot = locptr;
      else if (item < parent->data )  
         parent->left = locptr;
      else                           
         parent->right = locptr;
   }
   else
      cout << "Item already in the tree\n";
}

template <typename DataType>
void BST<DataType>::remove(const DataType & item)
{
   bool found;                      
   BST<DataType>::BinNodePointer  x,parent;                      
   search2(item, found, x, parent);

   if (!found)
   {
      cout << "Item not in the BST\n";
      return;
   }
   if (x->left != 0 && x->right != 0)
   {                                
      BST<DataType>::BinNodePointer xSucc = x->right;
      parent = x;
      while (xSucc->left != 0)       
      {
         parent = xSucc;
         xSucc = xSucc->left;
      }

     x->data = xSucc->data;
     x = xSucc;
   } 

   BST<DataType>::BinNodePointer 
      subtree = x->left;             
   if (subtree == 0)
      subtree = x->right;
   if (parent == 0)                 
      myRoot = subtree;
   else if (parent->left == x)      
      parent->left = subtree; 
   else                             
      parent->right = subtree;
   delete x;
}

template <typename DataType>
inline void BST<DataType>::inorder(ostream & out) const
{ 
   inorderAux(out, myRoot); 
}

template <typename DataType>
inline void BST<DataType>::graph(ostream & out) const
{ graphAux(out, 0, myRoot); }

template <typename DataType>
void BST<DataType>::search2(const DataType & item, bool & found,
                            BinNodePointer & locptr, 
                            BinNodePointer & parent) const
{
   locptr = myRoot;
   parent = 0;
   found = false;
   while (!found && locptr != 0)
   {
      if (item < locptr->data)       
      {
         parent = locptr;
         locptr = locptr->left;
      }
      else if (locptr->data < item) 
      {
         parent = locptr;
         locptr = locptr->right;
      }
      else                           
         found = true;
   }
}

template <typename DataType>
void BST<DataType>::inorderAux(ostream & out, 
                               BinNodePointer subtreeRoot) const
{
   if (subtreeRoot != 0)
   {
      inorderAux(out, subtreeRoot->left);   
      out << subtreeRoot->data << "  ";      
      inorderAux(out, subtreeRoot->right);   
   }
}

#include <iomanip>

template <typename DataType>
void BST<DataType>::graphAux(ostream & out, int indent, 
                             BinNodePointer subtreeRoot) const
{
  if (subtreeRoot != 0)
    {
      graphAux(out, indent + 8, subtreeRoot->right);
      out << setw(indent) << " " << subtreeRoot->data << endl;
      graphAux(out, indent + 8, subtreeRoot->left);
    }
}

#endif

//---------------------------BST_main.cpp ------------------------------

#include <iostream>
using namespace std;

#include "BST.h"

int main()
{
   BST<int> intBST;            
   cout << "Constructing empty BST\n";
   cout << "BST " << (intBST.empty() ? "is" : "is not") << " empty\n";

   cout << "Inorder Traversal of BST: \n";
   intBST.inorder(cout);

   cout << "\nNow insert a bunch of integers into the BST."
           "\nTry items not in the BST and some that are in it:\n";
   int number;
   for (;;)
   {
      cout << "Item to insert (-999 to stop): ";
      cin >> number;
      if (number == -999) break;
      intBST.insert(number);
   }
   intBST.graph(cout);

   cout << "BST " << (intBST.empty() ? "is" : "is not") << " empty\n";
   cout << "Inorder Traversal of BST: \n";
   intBST.inorder(cout);

   cout << endl;

   cout << "\n\nNow testing the search() operation."
           "\nTry both items in the BST and some not in it:\n";
   for (;;)
   {
      cout << "Item to find (-999 to stop): ";
      cin >> number;
      if (number == -999) break;
      cout << (intBST.search(number) ? "Found" : "Not found") << endl;
   }

   cout << "\nNow testing the remove() operation."
           "\nTry both items in the BST and some not in it:\n";
   for (;;)
   {
      cout << "Item to remove (-999 to stop): ";
      cin >> number;
      if (number == -999) break;
      intBST.remove(number);
      intBST.graph(cout);
   }
   cout << "\nInorder Traversal of BST: \n";
   intBST.inorder(cout);
   cout << endl;
   system("pause");
}

Constructing empty BST
BST is empty
Inorder Traversal of BST:

Now insert a bunch of integers into the BST.
Try items not in the BST and some that are in it:
Item to insert (-999 to stop): 55
Item to insert (-999 to stop): 66
Item to insert (-999 to stop): 88
Item to insert (-999 to stop): 77
Item to insert (-999 to stop): 20
Item to insert (-999 to stop): 10
Item to insert (-999 to stop): 30
Item to insert (-999 to stop): -999
                88
                        77
        66
 55
                30
        20
                10
BST is not empty
Inorder Traversal of BST:
10  20  30  55  66  77  88


Now testing the search() operation.
Try both items in the BST and some not in it:
Item to find (-999 to stop): 66
Found
Item to find (-999 to stop): 44
Not found
Item to find (-999 to stop): 10
Found
Item to find (-999 to stop): 11
Not found
Item to find (-999 to stop): -999

Now testing the remove() operation.
Try both items in the BST and some not in it:
Item to remove (-999 to stop): 10
                88
                        77
        66
 55
                30
        20
Item to remove (-999 to stop): 66
        88
                77
 55
                30
        20
Item to remove (-999 to stop): 55
        88
 77
                30
        20
Item to remove (-999 to stop): -999

Inorder Traversal of BST:
20  30  77  88
请按任意键继续. . .

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值