您的位置:首页 > 其它

二分查找树

2015-11-11 22:48 302 查看
数据结构的project2

题目:

  A binary search tree is uniquely determined by a given ordered insertions of a

sequence of positive integers. On the other hand, a given binary search tree may

correspond to several different insertion sequences. Now given several insertion

sequences, it is your job to determine if they can generate the same binary search tree.

  Input Specification:

  Input consists of several test cases. For each test case, the first line contains

two positive integers N (<=10) and L, which are the total number of integers in an

insertion sequence and the number of sequences to be tested, respectively. The

second line contains N positive integers, separated by a space, which are the initially

inserted numbers. Then L lines follow, each contains a sequence of N integers to be

checked.

  For convenience, it is guaranteed that each insertion sequence is a permutation of

integers 1 to N – that is, all the N numbers are distinct and no greater than N. The

input ends with N being 0. That case must NOT be processed.

  Output Specification:

  For each test case, output to the standard output. Print in L lines the checking

results: if the sequence given in the i-th line corresponds to the same binary search

tree as the initial sequence, output to the i-th line “Yes”; else output “No”.

  Sample Input:

  4 2

  3 1 4 2

  3 4 1 2

  3 2 4 1

  2 1

  2 1

  1 2

  0

  Sample Output:

  Yes

  No

  No

  以下代码已通过Online Judge:

  链表实现:

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

typedef struct TreeNode *Tree;
struct TreeNode{
int Data;
Tree Left;
Tree Right;
};

Tree Insert(Tree T,int x)
{
if(!T){
T=(Tree)malloc(sizeof(struct TreeNode));
T->Data=x;
T->Left=T->Right=NULL;
}
else{
if(x<T->Data)T->Left=Insert(T->Left,x);
else if(x>T->Data)T->Right=Insert(T->Right,x);
}
return T;
}

int IsSame(Tree T1,Tree T2)
{
if(!T1||!T2){
if(!T1&&!T2)return 1;
else return 0;
}
if(T1->Data!=T2->Data)return 0;
return IsSame(T1->Left,T2->Left)&&IsSame(T1->Right,T2->Right);
}

int main()
{
//	freopen("test.txt","r",stdin);
int N,L;
int i;
int x;
while(scanf("%d",&N),N){
scanf("%d",&L);
Tree p=NULL;
for(i=0;i<N;i++){
scanf("%d",&x);
p=Insert(p,x);
}
while(L--){
Tree q=NULL;
for(i=0;i<N;i++){
scanf("%d",&x);
q=Insert(q,x);
}
if(IsSame(p,q))printf("Yes\n");
else printf("No\n");
}
}
return 0;
}


  数组实现:

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#define MaxSize 2000

int max;

void Insert(int *Tree,int x)
{
int i=0;
while(Tree[i]){
if(x>Tree[i])i=2*i+2;
else if(x<Tree[i])i=2*i+1;
}
Tree[i]=x;
if(i>max)max=i;
}

int IsSame(int *p,int *q)
{
int count=0;
while(*p==*q){
if(count==max)return 1;
p++;
q++;
count++;
}
return 0;
}

int main()
{
//	freopen("test.txt","r",stdin);
int N,L;
int i;
int x;
while(scanf("%d",&N),N){
max=0;
scanf("%d",&L);
int p[MaxSize];
memset(p,0,sizeof(p));
for(i=0;i<N;i++){
scanf("%d",&x);
Insert(p,x);
}
while(L--){
int q[MaxSize];
memset(q,0,sizeof(q));
for(i=0;i<N;i++){
scanf("%d",&x);
Insert(q,x);
}
if(IsSame(p,q))printf("Yes\n");
else printf("No\n");
}
}
return 0;
}



内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: