二分查找树
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:
链表实现:
数组实现:
题目:
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; }
相关文章推荐
- poj 2139 Six Degrees of Cowvin Bacon 图论 Floyd最短路
- 【Leetcode】238-Product of Array Except Self【Java实现】【Medium】【two way traverse】
- linux RPM
- LeetCode Range Sum Query - Immutable
- XML操作
- HTML一般标签
- 关于Fragment与Fragment、Activity通信的四种方式
- 医药采购之供货商药品目录添加查询
- hdu2007 平方和与立方和 (朴素)
- html表单中get与post之间的区别
- python time 模块详解
- 指针数组与数组指针
- 新特性recyclerview 实现瀑布流
- 面向对象程序设计上机练习七(类和对象)
- Javascript设计模式理论与实战:桥接模式
- 关于org.apache.catalina.session.StandardManager doLoad错误的解决
- Java关键字final、static使用总结
- 面向对象程序设计上机练习六(类和对象)
- 如何将一个数组的地址作为函数的返回值!
- mysql nonInstall 版本的安装与配置