W8(BST,AVL,etc.)

7-1 是否同一棵二叉搜索树

分数 25

全屏浏览题目

切换布局

作者 陈越

单位 浙江大学

给定一个插入序列就可以唯一确定一棵二叉搜索树。然而,一棵给定的二叉搜索树却可以由多种不同的插入序列得到。例如分别按照序列{2, 1, 3}和{2, 3, 1}插入初始为空的二叉搜索树,都得到一样的结果。于是对于输入的各种插入序列,你需要判断它们是否能生成一样的二叉搜索树。

输入格式:

输入包含若干组测试数据。每组数据的第1行给出两个正整数N (≤10)和L,分别是每个序列插入元素的个数和需要检查的序列个数。第2行给出N个以空格分隔的正整数,作为初始插入序列。随后L行,每行给出N个插入的元素,属于L个需要检查的序列。

简单起见,我们保证每个插入序列都是1到N的一个排列。当读到N为0时,标志输入结束,这组数据不要处理。

输出格式:

对每一组需要检查的序列,如果其生成的二叉搜索树跟对应的初始序列生成的一样,输出“Yes”,否则输出“No”。

输入样例:

4 2
3 1 4 2
3 4 1 2
3 2 4 1
2 1
2 1
1 2
0

输出样例:

Yes
No
No
#include<bits/stdc++.h>
using namespace std;

struct tree{
	int val;
	tree *le;
	tree *ri;
	tree(int v){
		val = v;
		le = NULL;
		ri = NULL;
	}
}; 

tree* insert(tree* root,int x){
	if(root == NULL)
		root = new tree(x);
	else if(x < root->val)
		root->le = insert(root->le, x);
	else
		root->ri = insert(root->ri, x);	
	return root;
}

bool isSametree(tree* p,tree* q)
{
	if(p == NULL && q == NULL)
		return true;
	if(p->val != q->val)
		return false;
	return isSametree(p->le, q->le)&&isSametree(p->ri, q->ri);
}

int main()
{
	int N, L, i, x;
	cin>>N;
	while(N)
	{
		char c;
		cin>>L;
		tree* root0 = NULL;
		for(i = 0;i < N;i++)
		{
			cin>>x;
			root0 = insert(root0,x);
		}
		for(int j = 0;j < L;j++)
		{
 			tree* root1 = NULL;
 			for(i = 0;i < N;i++)
			{
				cin>>x;
				root1 = insert(root1,x);
			}
			if(isSametree(root0, root1))
				cout<<"Yes"<<endl;
			else
				cout<<"No"<<endl;
		}
		cin>>N;
	} 

	return 0;
}

 

7-2 平衡二叉树的根

分数 25

全屏浏览题目

切换布局

作者 DS课程组

单位 浙江大学

将给定的一系列数字插入初始为空的AVL树,请你输出最后生成的AVL树的根结点的值。

输入格式:

输入的第一行给出一个正整数N(≤20),随后一行给出N个不同的整数,其间以空格分隔。

输出格式:

在一行中输出顺序插入上述整数到一棵初始为空的AVL树后,该树的根结点的值。

输入样例1:

5
88 70 61 96 120

输出样例1:

70

输入样例2:

7
88 70 61 96 120 90 65

输出样例2:

88
#include<stdio.h>
#include<stdlib.h>
typedef struct treeNode {
	int data; 	//存储数据
	struct treeNode *lson;
	struct treeNode *rson;
} AVLtree;

int deep(AVLtree *t) { //求深度函数
	int d = 0;
	if(t) {
		int l = deep(t->lson)+1;
		int r = deep(t->rson)+1;
		d = l>r?l:r;
	}
	return d;
}

AVLtree *LL(AVLtree *t) { //左左旋转
	AVLtree *p;
	p = t->lson;
	t->lson = p->rson;
	p->rson = t;
	return p;
}
AVLtree *RR(AVLtree *t) {
	AVLtree *p;
	p = t->rson;
	t->rson = p->lson;
	p->lson = t;
	return p;
}
AVLtree *LR(AVLtree *t) {
	t->lson = RR(t->lson);
	return LL(t);
}
AVLtree *RL(AVLtree *t) {
	t->rson = LL(t->rson);
	return RR(t);
}
AVLtree *creat(AVLtree *t,int k) {
	if(t == NULL) {
		t = (AVLtree *)malloc(sizeof(AVLtree));
		t->data = k;
		t->lson = t->rson = NULL;
	} else {
		if(k<t->data) {
			t->lson = creat(t->lson,k);
			if((deep(t->lson)-deep(t->rson)) > 1) { //不满足平衡二叉树的条件,左子树比右子树高,要进行左旋
				if(t->lson->data > k) //插在了左子树上
					t = LL(t);//要左左旋
				else
					t = LR(t);//否则左右旋
			}
		} else {
			t->rson = creat(t->rson,k);
			if((deep(t->rson)-deep(t->lson)) > 1) { //不满足平衡二叉树的条件,右子树比左子树高,要进行右旋
				if(t->rson->data > k)//插在了左子树上
					t = RL(t);//要右左旋
				else
					t = RR(t);//否则右右旋
			}
		}
	}
	return t;
}
int main() {
	int i,n,m;
	scanf("%d",&n);
	AVLtree *t = NULL;
	for(i = 0; i<n; i++) {
		scanf("%d",&m);
		t = creat(t,m);
	}
	printf("%d\n",t->data);
	return 0;
}

7-3 二叉搜索树的结构

分数 25

全屏浏览题目

切换布局

作者 陈越

单位 浙江大学

二叉搜索树或者是一棵空树,或者是具有下列性质的二叉树: 若它的左子树不空,则左子树上所有结点的值均小于它的根结点的值;若它的右子树不空,则右子树上所有结点的值均大于它的根结点的值;它的左、右子树也分别为二叉搜索树。(摘自百度百科)

给定一系列互不相等的整数,将它们顺次插入一棵初始为空的二叉搜索树,然后对结果树的结构进行描述。你需要能判断给定的描述是否正确。例如将{ 2 4 1 3 0 }插入后,得到一棵二叉搜索树,则陈述句如“2是树的根”、“1和4是兄弟结点”、“3和0在同一层上”(指自顶向下的深度相同)、“2是4的双亲结点”、“3是4的左孩子”都是正确的;而“4是2的左孩子”、“1和3是兄弟结点”都是不正确的。

输入格式:

输入在第一行给出一个正整数N(≤100),随后一行给出N个互不相同的整数,数字间以空格分隔,要求将之顺次插入一棵初始为空的二叉搜索树。之后给出一个正整数M(≤100),随后M行,每行给出一句待判断的陈述句。陈述句有以下6种:

  • A is the root,即"A是树的根";
  • A and B are siblings,即"AB是兄弟结点";
  • A is the parent of B,即"AB的双亲结点";
  • A is the left child of B,即"AB的左孩子";
  • A is the right child of B,即"AB的右孩子";
  • A and B are on the same level,即"AB在同一层上"。

题目保证所有给定的整数都在整型范围内。

输出格式:

对每句陈述,如果正确则输出Yes,否则输出No,每句占一行。

输入样例:

5
2 4 1 3 0
8
2 is the root
1 and 4 are siblings
3 and 0 are on the same level
2 is the parent of 4
3 is the left child of 4
1 is the right child of 2
4 and 0 are on the same level
100 is the right child of 3

输出样例:

Yes
Yes
Yes
Yes
Yes
No
No
No

 

#include<bits/stdc++.h>
using namespace std;
struct node{
	int num, lchild = -1, rchild = -1, level, pa = -1;
}tree[210];
int n, m, x, a, b, res, root = 1, cnt;
map<int, int> Find;
string c;
void insert(int x){
	int u = root;
	while(tree[u].num != x){
		if(x < tree[u].num){
			if(tree[u].lchild == -1){
				tree[cnt].num = x;
				tree[cnt].level = tree[u].level + 1;
				tree[cnt].pa = u;
				tree[u].lchild = cnt;
			}
			u = tree[u].lchild;
		}else{
			if(tree[u].rchild == -1){
				tree[cnt].num = x;
				tree[cnt].level = tree[u].level + 1;
				tree[cnt].pa = u;
				tree[u].rchild = cnt;
			}
			u = tree[u].rchild;
		}
	}
}
int main(){
	cin >> n >> x;
	tree[++cnt].num = x;
	Find[x] = cnt;
	for(int i = 1;i < n;i++)	cin >> x, Find[x] = ++cnt, insert(x); 
	cin >> m;
	while(m--){
		res = 0;
		cin >> a >> c;
		if(c == "is"){
			cin >> c >> c;
			if(c == "root"){
				if(Find[a] == root)	res = 1;
			}else if(c == "parent"){
				cin >> c >> b;
				if(tree[Find[b]].pa == Find[a])	res = 1;
			}else if(c == "left"){
				cin >> c >> c >> b;
				if(tree[Find[b]].lchild == Find[a])	res = 1;
			}else{
				cin >> c >> c >> b;
				if(tree[Find[b]].rchild == Find[a])	res = 1;
			}
		}else{
			cin >> b >> c >> c;
			if(c == "siblings"){
				if(Find[a] && Find[b] && tree[Find[a]].pa == tree[Find[b]].pa)	res = 1;
			}else{
				cin >> c >> c >> c;
				if(Find[a] && Find[b] && tree[Find[a]].level == tree[Find[b]].level)	res = 1;
			}
		}
		if(res)	puts("Yes");
		else	puts("No");
	}
}

 

7-4 二叉搜索树的最近公共祖先

分数 25

全屏浏览题目

切换布局

作者 陈越

单位 浙江大学

给定一棵二叉搜索树的先序遍历序列,要求你找出任意两结点的最近公共祖先结点(简称 LCA)。

输入格式:

输入的第一行给出两个正整数:待查询的结点对数 M(≤ 1 000)和二叉搜索树中结点个数 N(≤ 10 000)。随后一行给出 N 个不同的整数,为二叉搜索树的先序遍历序列。最后 M 行,每行给出一对整数键值 U 和 V。所有键值都在整型int范围内。

输出格式:

对每一对给定的 U 和 V,如果找到 A 是它们的最近公共祖先结点的键值,则在一行中输出 LCA of U and V is A.。但如果 U 和 V 中的一个结点是另一个结点的祖先,则在一行中输出 X is an ancestor of Y.,其中 X 是那个祖先结点的键值,Y 是另一个键值。如果 二叉搜索树中找不到以 U 或 V 为键值的结点,则输出 ERROR: U is not found. 或者 ERROR: V is not found.,或者 ERROR: U and V are not found.

输入样例:

6 8
6 3 1 2 5 4 8 7
2 5
8 7
1 9
12 -3
0 8
99 99

输出样例:

LCA of 2 and 5 is 3.
8 is an ancestor of 7.
ERROR: 9 is not found.
ERROR: 12 and -3 are not found.
ERROR: 0 is not found.
ERROR: 99 and 99 are not found.
#include<iostream>
#include<map>
#include<algorithm>
using namespace std;
typedef struct Node* Tree;
struct Node{
	int key;
	Tree left = 0,right = 0;
}tree[10005];
int n,cnt,a[10005],b[10005];
map<int,int> mp;

Tree creat(int len,int loc,int start){
	if(len < 1) return 0;
	Tree T = &tree[cnt++];//保存地址该子树根节点的地址
	T->key = a[loc];
	int m = 0;
	m = lower_bound(b+m+start,b+len+start,a[loc])-b-start;//寻找树的根节点的位置
	T->left = creat(m,loc+1,start);//在范围内递归建树
	T->right= creat(len-m-1,loc+m+1,start+m+1);
	return T;//返回地址,由父结点指向左右子结点
}

int find(int x,int y,Tree Tr){//查找祖先结点
	if(x>Tr->key&&y>Tr->key)//大小比较结果情况相同
		return find(x,y,Tr->right);
	else if(x<Tr->key&&y<Tr->key)//情况不同
		return find(x,y,Tr->left);
	else return Tr->key;//如果有数值与该根节点相同时,返回该值
}

int main(){
	Tree T;
	int m;
	cin >> m >> n;
	for(int i = 0;i<n;++i){
		scanf("%d",&b[i]);
		a[i]=b[i];
		mp[a[i]] = 1;//标记该数值存在
	}
	sort(b,b+n);//获得中序遍历结果
	T = creat(n,0,0);//建树
	int u,v,k;
	while(m--){
		scanf("%d %d",&u,&v);
		//查找两个数值是否在树中均存在
		if(mp[u]==0&&mp[v]==0) cout <<"ERROR: "<<u<<" and "<<v<<" are not found."<<endl;
		else if(mp[u]==0) cout <<"ERROR: "<<u<<" is not found."<<endl;
		else if(mp[v]==0) cout <<"ERROR: "<<v<<" is not found."<<endl;
		else{//都存在时,判定
			k = find(u,v,T);//查找最近的祖先结点值
			if(k == u) cout <<u<<" is an ancestor of "<<v<<"."<<endl;
			else if(k == v) cout <<v<<" is an ancestor of "<<u<<"."<<endl;
			else cout <<"LCA of "<<u<<" and "<<v<<" is "<<k<<"."<<endl;
		}
	}
	return 0;
}

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

102101222_张凯权

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

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

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

打赏作者

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

抵扣说明:

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

余额充值