二叉树相关操作(层数,前序,后序,中序,按层遍历,生成二叉树,查找元素,排序)

 

#include <bits/stdc++.h>
 
using namespace std;
struct node{
	int data;
	struct node *left=NULL;
	struct node *right=NULL;
	
};
struct node *head=NULL;
//建树,比根结点小的插入到左子树的左叶子结点,比根结点大的数据插入到右叶子结点. 
bool insert(int value){
	if(head==NULL){
		head=new node;
		head->data=value;
		return true;
	}
	struct node *h=head;
	while(h!=NULL){
		if(h->data>value){
			struct node *t=new node;
			if(h->left==NULL){
				t->data=value;
				h->left=t;
				return true;
			}
			h=h->left;
		}else if(h->data<value){
			struct node *t=new node;
			if(h->right==NULL){
				t->data=value;
				h->right=t;
				return true;
			}
			h=h->right;
		}
	}
	return false;
}
struct node * find(int value){
	struct node *h=head;
	while(h!=NULL){
		if(h->data>value){
			h=h->left;
		}else if(h->data<value){
			h=h->right;
		}else{
			
			return h;
		}
	}
	return NULL;
}
void findDetail(int value){
	struct node * t=find(value);
		if(t!=NULL){
			if(t->left!=NULL){
				cout<<t->left->data<<" ";
			}else{
				cout<<"left null"<<" ";
			}
			cout<<value<<" ";
			if(t->right!=NULL){
				cout<<t->right->data<<" ";
			}else{
				cout<<"right null"<<" ";
			}
			cout<<endl;
		}else{
			cout<<value<<" not find"<<endl;
		}
}
 
void deletenode(int value){
	node * h=head;
	node * parent=head;
	while(h!=NULL){
		if(h->data>value){
			parent=h;
			h=h->left;
		}else if(h->data<value){
			parent=h;
			h=h->right;
		}else{
			break;
		}
	}
	cout<<"delete node is"<<h->data<<endl;
	if(h==NULL){
		cout<<"no found"<<endl;
	}
	if(h->left!=NULL&&h->right!=NULL){
		node *p_rtree=h;
		node * rtree=h->right;
		while(rtree->left!=NULL){
			p_rtree=rtree;
			rtree=rtree->left;
		}
		//用右子树中最小结点替换要删除的点的位置 
		h->data=rtree->data;
		h=rtree;
		parent=p_rtree;
	}
	
	//当我们要删除的结点是叶子结点或只有一个叶子结点的结点。 
	node  *child=NULL;
	if(h->right!=NULL){
		child=h->right;
	}else if(h->left!=NULL){
		child=h->left;
	}else{
		child=NULL;
	}
	if(parent==NULL){
		parent=child;
	}else if (parent->left==h){
		parent->left=child;
	}else{
		parent->right=child;
	}
	
}
//前序遍历 
void preorder(struct node *t){
	if(t!=NULL){
		cout<<t->data<<" ";
		preorder(t->left);
		preorder(t->right);
	}
}
//中序遍历 
void inorder(struct node *t){
	if(t!=NULL){
		inorder(t->left);
		cout<<t->data<<" ";
		inorder(t->right);
	}
}
//后序遍历 
void posorder(struct node *t){
	if(t!=NULL){
		posorder(t->left);
		posorder(t->right);
		cout<<t->data<<" ";
	}
}
//按层遍历 
void stepquery(){
	queue<struct node> q;
	struct node *h =head;
	q.push(*h);
	if(h==NULL){
		cout<<"null"<<endl;
	}else{
		while(!q.empty()){
			struct node p=q.front();
			q.pop();
			cout<<p.data<<" ";
			if(p.left!=NULL){
				q.push(*(p.left));
			}
			if(p.right!=NULL){
				q.push(*(p.right));
			}
		}
	}
	cout<<endl;
} 
//按层遍历 
void stepquery2(){
	queue<struct node*> q;
	struct node *h =head;
	q.push(h);
	if(h==NULL){
		cout<<"null"<<endl;
	}else{
		while(!q.empty()){
			struct node *p=q.front();
			q.pop();
			cout<<p->data<<" ";
			if(p->left!=NULL){
				q.push(p->left);
			}
			if(p->right!=NULL){
				q.push(p->right);
			}
		}
	}
	
} 
//求二叉树的最大深度 
int get_height(struct node *h){
	if(h==NULL){
		return 0;
	}else{
		int height_left=get_height(h->left);
		int height_right=get_height(h->right);
		int max=height_left;
		if(max<height_right){
			max=height_right;
		}
		return max+1;
	}
}
// 求二叉树元素的最大值  
int getmax(struct node * t){
	if(t==NULL){
		return -1;
	}else{
		int max=t->data;
		int left_max=getmax(t->left);
		int right_max=getmax(t->right);
		if(max<left_max){
			max=left_max;
		}
		if(max<right_max){
			max=right_max;
		}
		return max;
	}
}
 
int main(int argc, char** argv) { 
	//freopen("abc.txt","r",stdin);
	int n;
	n=7;
	int a[]={6,3,8,2,5,1,7}; 
	for(int i=0;i<n;i++){
		insert(a[i]);
	}
//	for(int i=0;i<n;i++){
//		findDetail(a[i]);
//	}
//	deletenode(10);
//	
//	cout<<"delete-----------"<<endl;
//	for(int i=0;i<n;i++){
//		findDetail(a[i]);
//	}
	
	preorder(head);
	cout<<endl;
	// 6 3 2 1 5 8 7
	inorder(head);
	cout<<endl;
	//1 2 3 5 6 7 8  是不是所有的二叉搜索树的中序遍历都是从小到大排列呢?答案是肯定的 
	posorder(head);
	cout<<endl;
	//1 2 5 3 7 8 6
	cout<<"height:"<<get_height(head)<<endl;
	cout<<"max:"<<getmax(head)<<endl;
	stepquery();
	stepquery2();
	return 0;
 


}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值