有序二叉树的算法

#include<iostream>
using namespace std;
#include<time.h>

struct BTreeNode
{
 BTreeNode()
 {
  pLeft=NULL;
  pRight=NULL;
  item=0;
 }
 int item;
 BTreeNode* pLeft;
 BTreeNode* pRight;
};

BTreeNode *BT=NULL;
bool FindTwoTree(BTreeNode *BST,int &item)
{
 if(BST==NULL)
 {
  return false;
 }
 else
 {
  if(BST->item==item)
  {
   item=BST->item;
   return true;
  }
  else if(BST->item<item)
  {
   FindTwoTree(BST->pLeft,item);
  }
  else if(BST->item>item)
  {
   FindTwoTree(BST->pRight,item);
  }
 }


}

void InsertTwoTree(BTreeNode *&BST,int item)
{
 if(BST==NULL)
 {
  BTreeNode *p=new BTreeNode();
  p->item=item;
  BST=p;
  p->pLeft=NULL;
  p->pRight=NULL;
 }
 else if(BST->item>item)
 {
  InsertTwoTree(BST->pLeft,item);
 }
 else
 {
  InsertTwoTree(BST->pRight,item);
 }

}
bool DeleteTwoTree(BTreeNode *&BST,int &key)
{   //树为空的时候
 if(BST==NULL)
 {
  return false;
 }
 //关键字小于结点时候
 if(key<BST->item)
 {
  return DeleteTwoTree(BST->pLeft,key);
 }
 //关键字大于结点时候
 if(key>BST->item)
 {
  return DeleteTwoTree(BST->pRight,key);
 }
 BTreeNode *pTemp=BST;
 //左边的树为空时候
 if(BST->pLeft==NULL)
 {
  BST=BST->pRight;
  delete  pTemp;
  pTemp=NULL;
  return true;
 }
 //右边的树为空时候
 else if(BST->pRight==NULL)
 {
  BST=BST->pLeft;
  delete pTemp;
  pTemp=NULL;
  return true;

 }
 //左右都不为空的时候删除  根据书的特性找到中序前驱结点     把中序前驱结点的值给该结点然后删除中序前驱结点即可
 else
 {
  if(BST->pLeft->pRight==NULL)
  {
   BST->item=BST->pLeft->item;
   return DeleteTwoTree(BST->pLeft,BST->pLeft->item);
  }
  else
  {
   BTreeNode *p1=BST,*p2=BST->pLeft;
   while(p2->pRight!=NULL)
   {
    p1=p2;
    p2=p2->pRight;
   }
   BST->item=p2->item;
   return DeleteTwoTree(p1->pRight,p2->item);

  }
 }

}

void print(BTreeNode *BST)
{
 if(BST!=NULL)
 {
  print(BST->pLeft);
  cout<<BST->item<<"  ";
  print(BST->pRight);
 }


}

 


void main()
{
 const int N=100;
 int ifor=0;
 int Array[N];
 // srand((unsigned)time(NULL));
 cout<<"有序二叉树的算法"<<endl;
 cout<<"0为打印你输入的数据:"<<endl;
 cout<<"1为插入结点:"<<endl;
 cout<<"2为查找结点:"<<endl;
 cout<<"3为打印结点:"<<endl;
 cout<<"4为删除结点:"<<endl;
 cout<<"5为退出:"<<endl;
 int a=0;
 int i;
 while(true)
 {   ;
 cin>>i;
 switch(i)
 {
 case 0:
  cout<<"你依次输入的数据是:"<<endl;
  for(int i=0;i<ifor;i++)
  {
   cout<<Array[i]<<"  ";
  }
  cout<<endl;
  break;
 case 1:
  cout<<"请输入要插入的结点关键字:"<<endl;
  int k;
  cin>>k;
  Array[ifor]=k;
  ifor++;
  InsertTwoTree(BT,k);
  break;
 case 2:
  cout<<"请输入要查找的结点关键字:"<<endl;
  int key;
  cin>>key;
  if(FindTwoTree(BT,key))
  {
   cout<<"找到了你要找的"<<key<<endl;
  }
  else
  {
   cout<<"没有你要找的"<<key<<endl;
  }
  break;
 case 3:
  cout<<"打印结果如下:"<<endl;
  print(BT);
  cout<<endl;
  break;
 case 4:
  int key1;
  cout<<"请输入要删除的结点关键字:"<<endl;
  cin>>key1;
  if(DeleteTwoTree(BT,key1))
  {
   cout<<"删除成功"<<endl;
  }
  else
  {
   cout<<"删除失败"<<endl;
  }
  break;
 case 5:
  exit(0);
  break;
 }
 cout<<"有序二叉树的算法"<<endl;
 cout<<"0为打印你输入的数据:"<<endl;
 cout<<"1为插入结点:"<<endl;
 cout<<"2为查找结点:"<<endl;
 cout<<"3为打印结点:"<<endl;
 cout<<"4为删除结点:"<<endl;
 cout<<"5为退出:"<<endl;
 }

}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值