PTA-是否同一棵二叉搜索树

给定一个插入序列就可以唯一确定一棵二叉搜索树。然而,一棵给定的二叉搜索树却可以由多种不同的插入序列得到。例如分别按照序列{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

首先我们先大致理解一下题意:

N是二叉搜索树的节点个数,L是需要与初始序列作比较的序列个数。我们需要根据序列构造L二叉搜索树,并分别与检验二叉树(姑且这样称呼初始序列构成的BST吧)对比,判断是否为同一棵二叉搜索树。

设计的主要操作清楚了:插入数据,构建BST比较两棵BST是否相同;

如何构建一棵BST呢?

我们需要了解二叉搜索树(BST)的性质:

  • 每一个节点的值都要小于其左侧子树中任何值;
  • 每一个节点的值都要大于其右侧子树中任何值;

利用递归可以轻松解决这个问题,将要插入的值与当前节点数据data进行比较,大于data递归到右子树,反之,递归到左子树。下面附上参考代码:

Node Insert(Node T,int x)
{
	if(!T)  //这里要判断T是否为空树,若为空,x就是头结点的data;
	{
	    T=(Node)malloc(sizeof(struct TreeNode));
	    T->data=x;
	    T->left=T->right=NULL;
	} 
	else if(x>T->data) T->right=Insert(T->right,x);
	else if(x<T->data) T->left=Insert(T->left,x);
	return T;
}

二叉搜索树构建好了,那就要和检验二叉树进行比较了:

同样,我们继续采用递归的思想去解决这个问题。这里需要分析的情况不多,直接上代码看看吧

int judge(Node T1,Node T2)
{
	if(!T1&&!T2)//若两棵树都是空树,必然是相同的
	    return 1;
	else if((!T1&&T2)||(T1&&!T2)) return 0;//其一为空,则必然不同。
	else if(T1->data!=T2->data) return 0; //二者不为空,比较头结点的值,头结点的值不同,就没有比 
                                          // 下去的必要了
	else  return (judge(T1->left,T2->left)&&judge(T1->right,T2->right));//头结点相同,就递归 
                                                                  //到左右子树中去,逐一比较
}

小白啥也不懂ww,写个博客巩固一下,欢迎大家批评建议!!谢谢~

最后附上完整代码

#include<stdlib.h>
#include<stdio.h>

typedef struct TreeNode* Node;
struct TreeNode
{
	int data;
	Node left;
	Node right;
};

Node Insert(Node T,int x);//插入节点,创建树 
int judge(Node T1,Node T2);//比较两棵树是否相同 

Node Insert(Node T,int x)
{
	if(!T)
	{
	    T=(Node)malloc(sizeof(struct TreeNode));
	    T->data=x;
	    T->left=T->right=NULL;
	} 
	else if(x>T->data) T->right=Insert(T->right,x);
	else if(x<T->data) T->left=Insert(T->left,x);
	return T;
}

int judge(Node T1,Node T2)
{
	if(!T1&&!T2)
	    return 1;
	else if((!T1&&T2)||(T1&&!T2)) return 0;
	else if(T1->data!=T2->data) return 0;
	else  return (judge(T1->left,T2->left)&&judge(T1->right,T2->right));
}

int main()
{
	int n,l,a;
	Node BT,T;
	scanf("%d %d",&n,&l);
	while(n) //n==0时不做处理
	{   
	    BT=NULL;
        for(int i=0;i<n;i++)//循环一:由初始序列构建二叉树 
		{
		    scanf("%d",&a);
			BT=Insert(BT,a);	
		}
		for(int i=0;i<l;i++)//循环二:构建 L个 二叉搜索树 
		{
			T=NULL;
			for(int j=0;j<n;j++)  
			{
				scanf("%d",&a);
				T=Insert(T,a);
			}
			if(judge(BT,T)) printf("Yes\n");
			else printf("No\n");
		}
		scanf("%d %d",&n,&l);//当n为0的时候,标志输入结束,别漏了!不然会超时的	
	} 
	
	return 0;
}

 

 

 

  • 4
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
9.25实现下列函数: int Search(SSTable s, KeyType k); /* Index the element which key is k */ /* in StaticSearchTable s. */ /* Return 0 if x is not found. */ 静态查找表的类型SSTable定义如下: typedef struct { KeyType key; ... ... // 其他数据域 } ElemType; typedef struct { ElemType *elem; int length; } SSTable; int Search(SSTable a, KeyType k) /* Index the element which key is k */ /* in StaticSearchTable s. */ /* Return 0 if x is not found. */ { int i; if(a.length==0) return ERROR; //先判断a是否为空 a.elem[a.length+1].key=k; for(i=1;a.elem[i].key>k;i++) if(i>=a.length||a.elem[i].key<k) //不能忽略i=a.length这种情况 return ERROR; return i; } /* { int i; a.elem[a.length+1].key=k; for(i=1;a.elem[i].key>k;i++) if(i>a.length||a.elem[i].key<k) return ERROR; return i; } */ 9.26② 试将折半查找算法改写成递归算法。 实现下列函数: int BinSearch(SSTable s, int low, int high, KeyType k); /* Index the element which key is k */ /* in StaticSearchTable s. */ /* Return 0 if x is not found. */ 静态查找表的类型SSTable定义如下: typedef struct { KeyType key; ... ... // 其他数据域 } ElemType; typedef struct { ElemType *elem; int length; } SSTable; int BinSearch(SSTable s, int low, int high, KeyType k) /* Index the element which key is k */ /* in StaticSearchTable s. */ /* Return 0 if x is not found. */ { int mid; if(low<=high) { mid=(low+high)/2; if(s.elem[mid].key==k) return mid; if(s.elem[mid].key<k) return BinSearch(s,mid+1,high,k); if(s.elem[mid].key>k) return BinSearch(s,low,high-1,k); } return 0; } /* { int mid; if(low<=high) { mid=(low+high)/2; if(s.elem[mid].key<k) return BinSearch(s,mid+1,high,k); else return BinSearch(s,low,high-1,k); } return 0; } */ 9.31④ 试写一个判别给定二叉树是否为二叉排序树的算法,设此二叉树以二叉链表作存储结构。且树中结点的关键字均不同。 实现下列函数: Status IsBSTree(BiTree t); /* 判别给定二叉树t是否为二叉排序树。*/ /* 若是,则返回TRUE,否则FALSE */ 二叉树的类型BiTree定义如下: typedef struct { KeyType key; ... ... // 其他数据域 } ElemType; typedef struct BiTNode { ElemType data; BiTNode *lchild,*rchild; }BiTNode, *BiTree; KeyType predt=-32767; Status IsBSTree(BiTree t) /* 判别给定二叉树t是否为二叉排序树。*/ /* 若是,则返回TRUE,否则FALSE */ { if( t )//&& ! ( t->lchild || t->rchild ) )//空树和叶子不用判断 { if( t->lchild && ( t->data.key < t->lchild->data.key ) )//左孩子不空,左孩子的key比本身的大 return FALSE; else if( t->rchild && ( t->data.key > t->rchild->data.key ) )//右孩子不空,右孩子的key比本身的大 return FALSE; else if( !IsBSTree( t->lchild ) )//判断左子树 return FALSE; else if( !IsBSTree( t->rchild ) )//判断右子树 return FALSE; } return TRUE; } /* { if(!t) return OK; if(t&&!t->lchild&&!t->rchild) return OK; else { if(t->lchild->data.key<t->data.key) IsBSTree(t->lchild); if(t->lchild->data.key>=t->data.key) return ERROR; if(t->rchild->data.key>t->data.key) IsBSTree(t->rchild); else return ERROR; return OK; } } */ 9.33③ 编写递归算法,从大到小输出给定二叉排序树中所有关键字不小于x的数据元素。要求你的算法的时间复杂度为O(log2n+m),其中n为排序树中所含结点数,m为输出的关键字个数。 实现下列函数: void OrderOut(BiTree t, KeyType x, void(*visit)(TElemType)); /* Output is to use visit(t->data); */ 二叉树的类型BiTree定义如下: typedef struct { KeyType key; ... ... // 其他数据域 } ElemType; typedef struct BiTNode { ElemType data; BiTNode *lchild,*rchild; }BiTNode, *BiTree; void OrderOut(BiTree t, KeyType x, void(*visit)(TElemType)) /* Output is to use visit(t->data); */ { if(t->rchild) OrderOut(t->rchild,x,visit); if(t->data.key>=x) visit(t->data); if(t->lchild)OrderOut(t->lchild,x,visit); } /* { if(t->rchild) OrderOut(t->rchild,x); if(t->data<x) exit(); visit(x); if(t->lchild)OrderOut(t->lchild,x); } */ 9.44④ 已知某哈希表的装载因子小于1,哈希函数 H(key)为关键字(标识符)的第一个字母在字母表中的序号,处理冲突的方法为线性探测开放定址法。试编写一个按第一个字母的顺序输出哈希表中所有关键字的算法。 实现下列函数: void PrintKeys(HashTable ht, void(*print)(StrKeyType)); /* 依题意用print输出关键字 */ 哈希表的类型HashTable定义如下: #define SUCCESS 1 #define UNSUCCESS 0 #define DUPLICATE -1 typedef char StrKeyType[4]; typedef struct { StrKeyType key; void *any; } HElemType; int hashsize[] = { 7,11,17,23,29,37,47 }; typedef struct { HElemType elem[MAXLEN]; int count; int sizeindex; } HashTable; int H(char *s)//求Hash函数 { if( s[0] ) return s[0]-'A'+1; //求关键字第一个字母的字母序号(小写) else return 0; } void PrintKeys(HashTable ht, void(*print)(StrKeyType)) /* 依题意用print输出关键字 */ { int i,j; for( i = 1; i <= 26; i++ ) { for( j = (i-1)%hashsize[ht.sizeindex]; ht.elem[j].key[0]; ) { if( H ( ht.elem[j].key ) == i ) print(ht.elem[j].key); j = (j+1)%hashsize[ht.sizeindex]; } } } /* void PrintKeys(HashTable ht, void(*print)(StrKeyType)) /* 依题意用print输出关键字 { int i,j; for(i=1;i<=26;i++) for(j=i;ht.elem[j].key;j=(j+1)%MAXLEN) if(H(ht.elem[j].key)==i) print(ht); } int H(char *s) { if(s) return s[0]-96; else return 0; } */ 9.45③ 假设哈希表长为m,哈希函数为H(x),用链地址法处理冲突。试编写输入一组关键字并建造哈希表的算法。 实现下列函数: int BuildHashTab(ChainHashTab &H, int n, HKeyType es[]) /* 直接调用下列函数 */ /* 哈希函数: */ /* int Hash(ChainHashTab H, HKeyType k); */ /* 冲突处理函数: */ /* int Collision(ChainHashTab H, HLink &p); */ { int i = 0,l,flag; HLink p,node; while( es[i] ) { l = Hash( H, es[i] ); node = ( HLink )malloc( sizeof( HNode ) ); node->data = es[i]; node->next = NULL; i++; if( !H.elem[l] ) H.elem[l] = node; else { flag = 0; p = H.elem[l]; if( p->data == node->data ) flag = 1; while( Collision( H, p ) ) if( p->data == node->data ) { flag = 1; break; } if( !flag ) { p = H.elem[l]; node->next = p; H.elem[l] = node; } } } } "+userLink+""; $('miniAd').show(); } }, onFailure: function(){} }}); } showMiniAd();

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值