搜索二叉树

二叉搜索树定义:

       当树非空时,其右子树的结点值都大于根节点值,其左子树的结点值都小于根结点值,同样它的左右子树也是搜索树。

本文给出二叉搜索树的常用操作:
1.二叉搜索树查找指定元素

2.查找树中的最大与最小元素

3.向搜索树中插入元素(保证树的有序性)

4.搜索树中删除指定值的结点,同时也保证树的有序性

同时本文也给出了层序生成二叉树,与层序遍历二叉树(广度优先)的操作

#include <iostream>
#include <queue>
using namespace std;
struct Treenode
{
	int val;
	Treenode *left;
	Treenode * right;
	Treenode(int x):val(x),left(nullptr),right(nullptr){}
	Treenode():val(0),left(nullptr),right(nullptr){}
	
};

typedef struct Treenode TreeNode;

void layerCreate(TreeNode *&root)              //层序生成二叉树,层序生成二叉树测试案例
{                                               //30 15 41 0 0 33 50 0 35 45 52 34 0 0 0 0 0 0 0  
	queue<TreeNode*> que;
	int jud;
	cin>>jud;
	if(jud!=0)
	{
		root=new TreeNode(jud);
		que.push(root);
	}
	else return ;
	while(!que.empty())
	{
		TreeNode *cur=que.front();
		que.pop(); 
		cin>>jud;
		if(jud!=0)
		{
			TreeNode *temp=new TreeNode(jud);
			que.push(temp);
			cur->left=temp;
		}
		else cur->left=nullptr;
	    cin>>jud;
		if(jud!=0)
		{
			TreeNode *temp=new TreeNode(jud);
			que.push(temp);
			cur->right=temp;
		}
		else cur->right=nullptr;
	}
	
 } 
 
 void layerbrowse( TreeNode *root)             //层序遍历二叉树 
{
 	if(root==nullptr)
 	 return ;
 	 queue<TreeNode*> que;
 	 que.push(root);
 	 while(!que.empty())
     {
         int size=que.size();
		 for(int i=0;i<size;i++)
		 {
		     TreeNode *temp=que.front();
			 que.pop();
			 cout<<temp->val<<" ";
			 if(temp->left !=nullptr)  que.push(temp->left);
			 if(temp->right!=nullptr) que.push(temp->right); 	
		 	
		  } 	
     	
     	
	  } 	
 	
 }
 
 TreeNode*  findelement(TreeNode *root, int key)      //递归查找值为key的元素,找到返回其地址 
{
    if(root==nullptr) return nullptr;                 //树为空时,直接返回 
	if(root->val==key ) return root;
	if(key> root->val)
	   return  findelement(root->right,key);
	if(key<root->val )
	     return findelement(root->left,key); 
 	
}  
 
 TreeNode* findele(TreeNode *root ,int key)         //迭代法查找值为key的结点
 {
 	 if(root==nullptr) return nullptr;
	 if(root->val==key) return root;
	 while(root!=nullptr)
	 {
	 	if(key==root->val )
	 	 return root;
	 	else if(key>root->val) 
	 	  root=root->right;
	 	else
	 	   root=root->left;
	  } 
 	 return nullptr;
 	
}

int   findMax(TreeNode* root)         //找最大元素,迭代法,最大元素必定存在右子树的右子树上 
{
	if(root==nullptr) return -1;      //树为空返回-1 代表不存在最大元素 
	while(root!=nullptr)
	{
		if(root->right==nullptr)
		return root->val;
		else 
		root=root->right;
	}
} 
int findMin(TreeNode *root)             //找最小元素,递归法,最小元素必定在,左子树的左子树上 
{
	if(root==nullptr) return  -1;
	if(root->left !=nullptr) 
	   return findMin(root->left );
	else 
	   return root->val;
	
	
}
void insertNode(TreeNode* &root,int key)  //向二叉搜索树中插入值为key的结点
{
   	if(root==nullptr)
   	{
   	   root=new TreeNode(key);
	    return ;	
	}
	else 
	{
		if(key> root->val )
		{
			insertNode(root->right, key);
		}
		else  if(key<root->val) 
		   insertNode(root->left,key);
		else                                      //当原二叉树中存在与插入元素相等的元素时,不插入,保证搜索树中元素各不相同 
		    return ;
	}
	
} 

TreeNode* deleteNode(TreeNode *root,int key)        //删除值为 key的结点 
{
	if(root==nullptr)  return root;               //树根为空直接返回
	 if(key >root->val)
			root->right=deleteNode(root->right,key);
	 else if(key <root->val)
	     	root->left=deleteNode(root->left,key);
	  else{
	  	  if(root->left!=nullptr&& root->right!=nullptr)   //待删除结点,左右子树均非空时 
	  	   {
	  	        int min=findMin(root->right);	                  //在右子树中找到最小元素 
	  	   	    root->val=min;
	  	   	    root->right=deleteNode(root->right,min);

		    }
			else
			{
				if(root->left==nullptr)                //左节点为空,右节点可空可非空 
				{
					root=root->right;
				}
				else                                    //左节点非空,右节点为空 
					root=root->left;
	
			}	  	  	
	  }
	  return root;
	
	
	
	
}

 int main()
 {
 	TreeNode *root=nullptr;
 	layerCreate(root);
 	layerbrowse(root);
 	cout<<endl;
 	int target;
 	cin>>target;
 	TreeNode * tarpos=findele(root,target);
	if(tarpos==nullptr)
	cout<<"can not find "<<target<<" in the searchtree"<<endl;
	else 
	cout<<"the postion of "<<tarpos->val<<" is "<<tarpos<<endl;
 	cout<<"max value is "<<findMax(root)<<endl;
 	cout<<"min value is "<<findMin(root)<<endl; 
 	int key;
 	cout<<"please input the inserted number: ";
 	cin>>key;
    insertNode(root,key);
    cout<<"after insert: ";
	layerbrowse(root);
	cout<<endl; 
 	cout<<"please input deleted number: ";
 	int denum;
 	cin>>denum;
 	cout<<endl;
	root=deleteNode(root,denum);
	cout<<"after delete : ";
	layerbrowse(root);
 	return 0;
 	
 	
 }

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值