【练习】二叉树遍历_37+二叉排序树_38+二叉排序树的基本操作_39

二叉树遍历
输入格式
两个字符串,其长度n均小于等于26。
第一行为前序遍历,第二行为中序遍历。
二叉树中的结点名称以大写字母表示:A,B,C…最多26个结点。

输出格式
输入样例可能有多组,对于每组测试样例,
输出一行,为后序遍历的字符串。

样例输入
ABC
CBA
ABCDEFG
DCBAEFG
样例输出
CBA
DCBGFEA
【根据前序和中序遍历建立二叉树,并给出后序遍历】

#include<bits/stdc++.h>
using namespace std;
struct treenode{
	char data;
	treenode*leftchild;
	treenode*rightchild;
	treenode(char ch):data(ch),leftchild(NULL),rightchild(NULL){}
};
//根据前序遍历和中序遍历建树 
treenode * create(string str1,string str2){
	if(str1.size()==0){
		return NULL;//是空树 
	}
	//存储根结点 
	treenode*root=new treenode(str1[0]);
	//找到根结点在中序遍历中的位置 
	int position=str2.find(str1[0]);
	root->leftchild=create(str1.substr(1,position),str2.substr(0,position));
	root->rightchild=create(str1.substr(position+1),str2.substr(position+1));
	return root;
}
//后序遍历 
void PostOrder(treenode*root){
	if(root==NULL){
		return;
		//是空树 
	}
	PostOrder(root->leftchild);
	PostOrder(root->rightchild);
	cout<<root->data;
}
int main(){
	string str1,str2;
	while(cin>>str1>>str2){
		treenode*root=NULL;
		root=create(str1,str2);
		PostOrder(root);
		cout<<endl;
	}
	return 0;
}

二叉排序树
题目描述
输入一系列整数,建立二叉排序数,并进行前序,中序,后序遍历。

输入格式
输入第一行包括一个整数n(1<=n<=100)。接下来的一行包括n个整数。

输出格式
可能有多组测试数据,对于每组数据,将题目所给数据建立一个二叉排序树,并对二叉排序树进行前序、中序和后序遍历。每种遍历结果输出一行。每行最后一个数据之后有一个空格。

样例输入
1
2
2
8 15
4
21 10 5 39
样例输出
2
2
2
8 15
8 15
15 8
21 10 5 39
5 10 21 39
5 10 39 21

#include<bits/stdc++.h>
using namespace std;
//二叉排序树 
struct treenode{
	int data;
	treenode*leftchild;
	treenode*rightchild;
	treenode(int num):data(num),leftchild(NULL),rightchild(NULL){}
};
//建立二叉排序树 
treenode * insert(treenode * s, treenode * root){
	if(root==NULL){
		root=s;
	}
	else if(root->data>s->data){
		root->leftchild=insert(s,root->leftchild);
	}
	else if(root->data<s->data){
		root->rightchild=insert(s,root->rightchild);
	}
	return root;
}
//前序遍历
void PreOrder(treenode*root){
	if(root==NULL){
		return;
		//是空树 
	}
	cout<<root->data<<" ";
	PreOrder(root->leftchild);
	PreOrder(root->rightchild);
}
//中序遍历
void InOrder(treenode*root){
	if(root==NULL){
		return;
		//是空树 
	}
	InOrder(root->leftchild);
	cout<<root->data<<" ";
	InOrder(root->rightchild);
}
//后序遍历 
void PostOrder(treenode*root){
	if(root==NULL){
		return;
		//是空树 
	}
	PostOrder(root->leftchild);
	PostOrder(root->rightchild);
	cout<<root->data<<" ";
}
int main(){
	int n,x;
	while(cin>>n){
		treenode * root=NULL;
		for(int i=0;i<n;i++){
			cin>>x;
			//建立新的结点 
			treenode*s=new treenode(x);
			root=insert(s,root);
		}
		PreOrder(root);
		cout<<endl;
		InOrder(root);
		cout<<endl;
		PostOrder(root);
		cout<<endl;
	} 
	return 0;
}

二叉排序树的基本操作
输入格式
输入的第一行包含2个正整数n和k,分别表示共有n个整数和k次查询。其中n不超过500,k同样不超过500。
第二行包含n个用空格隔开的正整数,表示n个整数。
第三行包含k个用空格隔开的正整数,表示k次查询的目标。
输出格式
只有1行,包含k个整数,分别表示每一次的查询结果。如果在查询中找到了对应的整数,则输出1,否则输出0。
请在每个整数后输出一个空格,并请注意行尾输出换行。
样例输入
8 3
1 3 5 7 8 9 10 15
9 2 5
样例输出
1 0 1
【方法一:直接用二分法做的】

#include<bits/stdc++.h>
using namespace std;
//二叉排序树的基本操作
//用二分法做简单
int arr[500];
bool BinarySearch(int arr[],int target,int n){
	int left=0;
	int right=n-1;
	while(left<=right){
		int middle=left+(right-left)/2;
		if(arr[middle]==target){
			return true;
		}
		else if(arr[middle]<target){
			left=middle+1;
		}
		else if(arr[middle]>target){
			right=middle-1;
		}
	}
	return false;
}
int main(){
	int n,k,target;
	cin>>n>>k;
	for(int i=0;i<n;i++){
		cin>>arr[i];
	}
	//二分法的排序不能忘 
	sort(arr,arr+n);
	for(int j=0;j<k;j++){
		cin>>target;
		int result=BinarySearch(arr,target,n);
		cout<<result<<" ";
	}
	cout<<endl;
	return 0;
}

【方法二:用二叉树查找的基本操作做的】

#include<bits/stdc++.h>
using namespace std;
//二叉排序树的基本操作
//用二叉树的基本操作做 
struct treenode{
	int data;
	treenode*leftchild;
	treenode*rightchild;
	treenode(int num):data(num),leftchild(NULL),rightchild(NULL){}
};
//建立二叉排序树 
treenode * insert(treenode * s, treenode * root){
	if(root==NULL){
		root=s;
	}
	else if(root->data>s->data){
		root->leftchild=insert(s,root->leftchild);
	}
	else if(root->data<s->data){
		root->rightchild=insert(s,root->rightchild);
	}
	return root;
}
//递归查找结果,和二叉排序树的建立过程很像
//也是和根结点比较 
treenode * search(treenode*root,int target){
	if(root==NULL){
		return NULL;
	}
	else if(root->data<target){
		search(root->rightchild,target);
	}
	else if(root->data>target){
		search(root->leftchild,target);
	}
	//查找到了 
	else{
		return root;
	}
} 
int main(){
	int n,x,k,target;
	treenode * root=NULL;
	cin>>n>>k;
	for(int i=0;i<n;i++){
		cin>>x;
		//建立新的结点
		treenode*s=new treenode(x);
		root=insert(s,root);
	}
	treenode*s=NULL;
	//在二叉排序树中查找结点 
	for(int j=0;j<k;j++){
		cin>>target;
		s=search(root,target);
		if(s==NULL){
			cout<<"0 ";
		}
		else{
			cout<<"1 ";
		}
	}
	cout<<endl;
	return 0;
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

每天都要学算法(努力版)

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值