二叉查找树

  1. // BSTree.cpp : Defines the entry point for the console application.
  2. //问题描述:二叉查找树的实现
  3. //创建者:QGX
  4. //创建时间:2008-2-1
  5. #include "stdafx.h"
  6. #include<iostream>
  7. using namespace std;
  8. //二叉查找树的定义
  9. #define NULL 0
  10. typedef  int Type;
  11. class TreeNode
  12. {
  13.  friend class BSTree;
  14. private:
  15.  TreeNode *rchild,*lchild;
  16.  Type data;
  17. };
  18. class BSTree
  19. {
  20. private:
  21.  TreeNode *tree;
  22.  TreeNode* Search(TreeNode *t,Type x);
  23.  void traverse(TreeNode *t);
  24. public:
  25.  BSTree(){tree=NULL;}
  26.  ~BSTree(){delete tree;}
  27.  TreeNode* RSearch(Type x);
  28.  TreeNode* ISearch(Type x);
  29.  void Insert(Type x);
  30.  void Delete(Type x);
  31.  void Traverse(){traverse(tree);}
  32. };
  33. TreeNode* BSTree::RSearch(Type x)
  34. {
  35.  return Search(tree,x);
  36. }
  37. TreeNode* BSTree::Search(TreeNode* t,Type x)
  38. {
  39.  //递归查找
  40.  if(!t)return NULL;
  41.  if(t->data==x)return t;
  42.  else if(t->data<x)return Search(t->rchild,x);
  43.  else return Search(t->lchild,x);
  44. }
  45. TreeNode* BSTree:: ISearch(Type x)
  46. {
  47.  //迭代查找
  48.  TreeNode* t=tree;
  49.  bool found=false;
  50.  while(!found && t)
  51.  {
  52.   if(t->data==x)
  53.   {
  54.    found=true;
  55.   }
  56.   else if(t->data<x)
  57.   {
  58.    t=t->rchild;
  59.   }
  60.   else t=t->lchild;
  61.  }
  62.  if(found) return t;
  63.  else return NULL;
  64. }
  65. void BSTree:: Insert(Type x)
  66. {
  67.  TreeNode *p,*q;
  68.  bool found=false;
  69.  p=tree;
  70.  q=NULL;
  71.  while(p)
  72.  {
  73.   if(p->data==x)
  74.   {
  75.    found=true;
  76.    break;
  77.   }
  78.   else
  79.   {
  80.    q=p;
  81.    if(p->data>x)p=p->lchild;
  82.    else p=p->rchild;
  83.   }  
  84.  }
  85.  if(found)return;
  86.  else
  87.  {
  88.   TreeNode *temp=new TreeNode;
  89.   temp->lchild=NULL;
  90.   temp->rchild=NULL;
  91.   temp->data=x;
  92.   
  93.   if(!tree)tree=temp;//根结点
  94.   else
  95.   {
  96.    if(q->data>x)q->lchild=temp;
  97.    else q->rchild=temp;
  98.   }
  99.  }
  100. }
  101. void BSTree::Delete(Type x)
  102. {
  103.  TreeNode *p=tree,*q=tree;
  104.  bool found=false;
  105.  while(p)
  106.  {
  107.   if(p->data==x)
  108.   { //找到待删除的结点
  109.    found=true;
  110.    break;
  111.   }
  112.   else
  113.   { //q为p的父结点
  114.    q=p;
  115.    if(p->data>x)p=p->lchild;
  116.    else p=p->rchild;
  117.   }
  118.  }
  119.  if(!found)
  120.  {
  121.   //不存在此结点
  122.   cout<<"Does not exist this node"<<endl;
  123.         return;
  124.  }
  125.  else
  126.  {
  127.   if(p==q)
  128.   {
  129.    //删除的是根结点
  130.    if(!p->rchild && !p->lchild)
  131.    { //无左右子树
  132.     delete p;
  133.     tree=NULL;
  134.    }
  135.    else if(!p->rchild)
  136.    { //无右子树 
  137.     tree=p->lchild;
  138.     delete p;
  139.    }
  140.    else if(!p->lchild)
  141.    { //无左子树
  142.     tree=p->rchild;
  143.     delete p;
  144.    }
  145.    else
  146.    {
  147.     //有左右子树
  148.     //找到x的直接后继结点,交换,删除
  149.     TreeNode *p2=p->rchild;
  150.     TreeNode *p3=p->rchild;
  151.     while(p2->lchild)
  152.     {
  153.      p3=p2;
  154.      p2=p2->lchild;
  155.     }
  156.     if(p3==p2)
  157.     {
  158.      p2->lchild=p->lchild;
  159.      tree=p2;
  160.      delete p;
  161.     }
  162.     else
  163.     {
  164.      p->data=p2->data;
  165.      p3->lchild=p2->rchild;
  166.      delete p2;
  167.     }
  168.    }
  169.   }//end_if
  170.   else
  171.   {
  172.    //删除非根结点
  173.    if(!p->rchild && !p->lchild)
  174.    {
  175.     if(q->data>x)q->lchild=NULL;
  176.     else q->rchild=NULL;
  177.     delete p;
  178.    }
  179.    else if(!p->rchild)
  180.    {
  181.     if(q->data>x)q->lchild=p->lchild;
  182.     else q->rchild=p->lchild;
  183.     delete p;
  184.    }
  185.    else if(!p->lchild)
  186.    {
  187.     if(q->data>x)q->lchild=p->rchild;
  188.     else q->rchild=p->rchild;
  189.     delete p;
  190.    }
  191.    else
  192.    {
  193.     //有左右子树
  194.     //找到x的直接后继结点,交换,删除
  195.     TreeNode *p2,*p3;
  196.     p2=p->lchild;
  197.     p3=p->lchild;
  198.     while(p2->rchild)
  199.     {
  200.      p3=p2;
  201.      p2=p2->rchild;
  202.     }
  203.     if(p2==p3)
  204.     {
  205.      p->data=p2->data;
  206.      p->lchild=p2->lchild;
  207.      delete p2;
  208.     }
  209.     else
  210.     {
  211.      p->data=p2->data;
  212.      p3->rchild=p2->lchild;
  213.      delete p2;
  214.     }
  215.    }//end_else   
  216.   }//end_else
  217.  }//end_else
  218. }
  219. void BSTree::traverse(TreeNode *t)
  220. {
  221.  if(!t)return;
  222.  else
  223.  {
  224.   traverse(t->lchild);
  225.   cout<<t->data<<endl;
  226.   traverse(t->rchild);
  227.  }  
  228. }
  229. int _tmain(int argc, _TCHAR* argv[])
  230. {
  231.  BSTree *t=new BSTree();
  232.  const int N=8;
  233.  Type data[N]={8,6,5,7,12,9,20,14};
  234.  for(int i=0;i<N;i++)
  235.   t->Insert(data[i]);
  236.  cout<<"The tree is:"<<endl;
  237.  t->Traverse();
  238.  //t->Delete(6); 
  239.  //t->Delete(8);
  240.  //t->Delete(12);
  241.  //t->Delete(5);
  242.  //t->Delete(20);
  243.  //t->Insert(100);
  244.  //t->Insert(3);
  245.  //t->Insert(12);
  246.  //t->Insert(19);
  247.  //t->Traverse();
  248.     //if(t->ISearch(19))cout<<"OK"<<endl;
  249.  //if(t->RSearch(19))cout<<"OK too"<<endl;
  250.  getchar();
  251.  return 0;
  252. }
  253.  

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值