BST二叉查找树

源代码如下所示:

#include<iostream>
using namespace std;
class BSTree
{
public:
	int data;
	BSTree *left;
	BSTree *right;
	BSTree(int data,BSTree *left = NULL ,BSTree * right= NULL)
	{
      this->data= data;
      this->left= left;
      this->right= right;
	}
};
void insert_BST(BSTree * &root, int n)// 这个*&好像很神奇的样子,直接传过来,省去了复制的麻烦
//而且必须这样
{
    if(root==NULL)
    {
         BSTree * temp= new BSTree(n);
         root = temp;
    }
    else 
    {
       if(root->data>n)
       {
       	 if(root->left!=NULL)
       	 	insert_BST(root->left,n);
       	 else
       	 {
             BSTree * temp1 = new BSTree(n);
             root->left=temp1;
       	 }
       }
       else 
       {
       	if (root->right!=NULL)
       	  insert_BST(root->right,n);
       	else 
       	{
       		BSTree * temp2= new BSTree(n);
       		root->right= temp2;
       	}
       }
    }
}
bool search_BST(BSTree *root, int key)//为了保证root的不被篡改,还是使用
{//指针,不传指针的地址了吧
   BSTree *temp=root;
   if(temp==NULL)
    return false;
  else if(temp->data==key)
    return true;
  else
  {
    if(temp->data<key)
      search_BST(temp->right,key);
    else 
      search_BST(temp->left,key);
  }
  //不想在这里加上return true;
}
/*
void print(int a)
{
  cout<<a<<endl;
}*/
void  inorder_BST(BSTree *root)
{ 
  if(root->left!=NULL)
  inorder_BST(root->left);
  cout<<root->data<<endl;//在函数的调用这里还是有点问题,要看老师的课件,好像是这么回事
  if(root->right!=NULL)
    inorder_BST(root->right);
  /*
  不如下面的代码简单
   if(root!=NULL)
        {
            inorder_BST(root->leftchildren);
            cout<<root->data<<" ";

            inorder_BST(root->rightchildren);
        }
        cout<<"\n";
  */
}
int findmax(BSTree *root)
{
  int max=0;
  if(root->right!=NULL)
    max=findmax(root->right);
  else 
    max=root->data;
  return max;
}
void deletion_BST(BSTree *& root,int key)
{
  if(root==NULL)
    cout<<"error"<<endl;
  else if(root->data<key)
  {
    deletion_BST(root->right,key);
  }
  else if(root->data>key)
  {
    deletion_BST(root->left,key);
  }
  else 
  {
    if(root->left==NULL)
    {
      BSTree *tmp=root->right;
      delete root;
      root=tmp;
    }
    else if(root->right==NULL)
    {
      BSTree *tmp=root->right;
      delete root;
      root=tmp;
    }
    else
    {
      //BSTree *par=NULL;
      BSTree *tmp3 = root->right;
      int s=findmax(tmp3->left);
      root->data=tmp3->data;
     deletion_BST(root->right,tmp3->data);
    }
  }
}
int main()
{
	int n;
	cout<<"this cpp is produced by chengyang"<<endl;
	BSTree * root  =  NULL;
	cout<<"please input the key you want to build a BST tree, unless you put -1"<<endl;
	while(1)
	{
       cin>>n;
       if(n==-1)
       {
       	break;
       }
       else 
       {
       	 insert_BST(root,n);
       }
	}
  inorder_BST(root);
  cout<<"this is the inorder of the binary tree"<<endl;
  insert_BST(root,234);
  cout<<"after insert 234 in to the tree"<<endl;
  inorder_BST(root);
    cout<<"this is the inorder of the binary tree"<<endl;
  deletion_BST(root,234);
  cout<<"after deletion of 234"<<endl;
  inorder_BST(root);

  system("pause");
  return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值