某农业大学数据结构A-第14周作业

1.二叉排序树

【问题描述】

请根据输入的数据创建一棵二叉排序树。然后执行相应操作。

1 删除某一值为x的结点

2 求指定结点y在二叉排序树中的层数

【输入形式】

结点数据,以0代表结束输入。

待删除的x,待求层数的y

【输出形式】

创建好的二叉排序树的拓展的前序遍历结果

删除后的二叉排序树的中序遍历结果

y所在的层数

【样例输入】

29 39 15 25 28 10 11 2 0

10

11

【样例输出】

29 15 10 2 # # 11 # # 25 # 28 # # 39 # # 

2 11 15 25 28 29 39

4

【样例说明】

若待删除的结点包含左右子树,则以其左子树的最右结点代替它。

/*
29 39 15 25 28 10 11 2 0
*/

#include<iostream>
#include<stdlib.h>
using namespace std;

typedef struct node
{
	int key;
	struct node*lc;
	struct node*rc;
 } node,*Bitree;
 
void Insert(Bitree *g,int key)//插入点 
 {
 	if(*g==NULL)
	{
	 	node *s=new node;
	 	s->key=key;
	 	s->lc=NULL;
	 	s->rc=NULL;
	 	*g=s;
	} 
	else if((*g)->key>key)
	{
		Insert(&(*g)->lc,key);
	}
	else if((*g)->key<key)
	{
		Insert(&(*g)->rc,key);
	}
 }
 
 void creat(Bitree *g)//创建 
 {
 	int key;
 	*g=NULL; 
	cin >> key;
	while(key!=0)
	{
		Insert(g,key);
		cin >> key;
	}
 }
 
 void Preorder(Bitree *g)//先序遍历输出 
 {
 	if((*g)!=NULL)
 	{
 		cout << (*g)->key << " ";
 		Preorder(&(*g)->lc);
 		Preorder(&(*g)->rc);
	}
	else
	 cout << "#"<< " ";
 }
 
node*Delete(Bitree g)
{
	int keye;
	cin >> keye;
	Bitree p,f,s,q;
	p=g;f=NULL;
	while(p)//寻找keye的待删结点p 
	{
		if(p->key==keye)//找到则退出循环 
		break;
		f=p;
		if(p->key >keye)
		p=p->lc;
		else
		p=p->rc;
	}
	if(p==NULL) return g;//找不到,返回原来二叉树 
	if(p->lc==NULL)//无左子树 
	{
		if(f==NULL)
		    g=p->rc;
		else if(f->lc==p) f->lc=p->rc;
		else f->rc=p->rc;
		//free(p);
	}
	else
	{
		q=p;s=p->lc;
		while(s->rc)
		{
			q=s;s=s->rc;
		}
		if(q==p)
			q->lc=s->lc;
		else
			q->rc=s->lc;
		p->key=s->key;
		//free(s);
	}
	return g;
}
 
 void Inorder(Bitree *g)
 {
 	if((*g)!=NULL)
 	{
 		Inorder(&(*g)->lc);
 		cout << (*g)->key << " ";
 		Inorder(&(*g)->rc);
	 }
 }
 
 void findde(Bitree *g)
 {
 	int temp,count=1;
 	cin >> temp;
 	while((*g)!=NULL)
 	{
 		if((*g)->key>temp)
 		{
 			(*g)=(*g)->lc;
 			count++;
		 }
		else if((*g)->key<temp)
		{
			(*g)=(*g)->rc;
			count++; 
		}
		else
		break;
	 }
	cout << count <<endl;
	
 }
 
int main()
{
	node*g;
	Bitree t;
	creat(&g);
	Preorder(&g);
	 cout << endl;
	t=Delete(g);
	Inorder(&t);
	 cout << endl;
	findde(&t); 
	return 0;
}
3.二叉查找树的后序遍历

【问题描述】输入一个整数数组,判断该数组是不是某二叉查找树(折半查找树)后序遍历的结果。如果是返回true,否则返回false。
【输入形式】输入任意长度的数组,数字之间空格分开
【输出形式】true 或者 false
【样例输入】输入5 7 6 9 11 10 8
【样例输出】true
【样例说明】由于这一整数序列是如下树的后序遍历结果:

          8
       /      \
      6      10
    /   \     /   \
   5   7   9  11

因此返回true。

【评分标准】暴力求解法不得分。

【提示】后序遍历的最后一个结点一定是根结点,那么前面的数据就可以划分为比根小的、比根大的。依此类推下去。

/*
5 7 6 9 11 10 8
*/

#include<iostream>
#include<stdlib.h>
#include<stdio.h>
using namespace std;
bool post(int s,int q);
int a[100];
int main()
{
	
	int sum;
	char c;
	for(int i=0;i<100;i++)
	{
		cin >> a[i];
		sum=i+1;
		c=getchar();
		if(c=='\n')
		break;
	}
	int temp=post(1,sum);
	if(temp)
	    cout << "true" <<endl;
	else
	    cout << "false" <<endl;
}

bool post(int s,int q)
{
	if(s==q) return true;
	else if(s>q) return false;
	else
	{
		int i,j,k;
		k=j=s;
		for(i=s+1;i<=q;i++)
		{
			if(a[i]<a[q])
			j++;
			else break;
		}
		int m=j+1,n=q-1;
		return (post(k,j)&&post(m,n));
	}
}
4.世界上有2片相同的雪花吗

【题目来源】3349 -- Snowflake Snow Snowflakes (poj.org)

Description

You may have heard that no two snowflakes are alike. Your task is to write a program to determine whether this is really true. Your program will read information about a collection of snowflakes, and search for a pair that may be identical. Each snowflake has six arms. For each snowflake, your program will be provided with a measurement of the length of each of the six arms. Any pair of snowflakes which have the same lengths of corresponding arms should be flagged by your program as possibly identical.

Input

The first line of input will contain a single integer n, 0 < n ≤ 100000, the number of snowflakes to follow. This will be followed by n lines, each describing a snowflake. Each snowflake will be described by a line containing six integers (each integer is at least 0 and less than 10000000), the lengths of the arms of the snow ake. The lengths of the arms will be given in order around the snowflake (either clockwise or counterclockwise), but they may begin with any of the six arms. For example, the same snowflake could be described as 1 2 3 4 5 6 or 4 3 2 1 6 5.

Output

If all of the snowflakes are distinct, your program should print the message:

No two snowflakes are alike.

If there is a pair of possibly identical snow akes, your program should print the message:

Twin snowflakes found.

Sample Input

2

1 2 3 4 5 6

4 3 2 1 6 5

Sample Output

Twin snowflakes found.

/*
2
1 2 3 4 5 6
4 3 2 1 6 5
*/ 
#include<iostream>
using namespace std;



int main()
{
	int n,i,j,k,temp=0;
	cin >> n;
	int a[n][6];
	for(i=0;i<n;i++)
	{
		for(j=0;j<6;j++)
		{
			cin >> a[i][j];
		}
	}
	int flag=1;
	for(i=1;i<n;i++)
	{
		if(flag==0)
		break;
		for(j=0;j<6;j++)
		{
			if(flag==0)
			break;
			if(a[0][0]==a[i][j])
			{
				k=j;
			    int m1,n1=1,m2,n2=1;
				while(a[0][j]==a[i][j])
				{
					if(j==5)
					break;
					j=j+1;
				}
				//cout << j;
				if(j<5)
				{
					for(m1=j+1;m1<6;m1++)
	            	{
		            	if(a[0][m1]!=a[i][5-temp])
		            	{
		        	     	flag=0;
	            		}
						temp++;
            		}
            		temp=0;
            		for(m2=0;m2<=k;m2++)
	        	    {
		    	    	if(a[0][m2]!=a[i][k-m2])
		    	        {
		    	         	flag=0;
	        	    	}
            		}	
				}
				  
			}
		}
		
	}
	if(flag==1)
	    cout << "Twin snowflakes found.";
	else
		cout << "No two snowflakes are alike.";
	 
}


5.折半查找法的使用 (C/C++通用版本)

【问题描述】给定一个按值有序(升序)的N元整数数组A,采用折半查找法查找关键值k的位置,并给出查找的过程
【输入形式】

第一行:N

第二行:A[0], A[1], ... , A[N-1]

第三行:k
【输出形式】

第一行:k的位置(索引),若不存在则输出&lsquo;no&rsquo;

第二行:查找的过程,每一次折半的中间(mid)位置的值,以逗号分隔。例如,1 2 3 4 5的中间位置为3,1 2 3 4的中间位置为2。
【样例输入】

样例1

11

2,5,8,11,15,16,22,24,27,35,50

22

样例2

11

2,5,8,11,15,16,22,24,27,35,50

10
【样例输出】

样例1

6

16,27,22

样例2

no

16,8,11
【样例说明】
【评分标准】必须使用折半法,其他方法不能得分。

/*
11
2,5,8,11,15,16,22,24,27,35,50
22
11
2,5,8,11,15,16,22,24,27,35,50
10
*/
#include<iostream>
#include<stdlib.h>
#include<stdio.h>
using namespace std;

int main()
{
	int n,i,temp;
	cin >> n;
	int a[n],b[n];
	char c;
	for(i=0;i<n;i++)
	{
		cin >> a[i];
		c=getchar();
	}
	//for(i=0;i<n;i++) {cout << a[i];}
	cin >> temp;
	int low=1,high=n,mid;
	int sum=0;
	while(low<=high)
	{
		mid=(low+high)/2;
		if(a[mid-1]==temp) 
		{
			break;
		}
		else if(a[mid-1]>temp)
		{
			high=mid-1;
			b[sum]=a[mid-1];
			sum++;
		}
		else
		{
			low=mid+1;
			b[sum]=a[mid-1];
			sum++;
		}   
	}
	if(a[mid-1]!=temp)
	{
		cout << "no" <<endl;
	    for(i=0;i<sum-1;i++)
	    {
		cout << b[i] <<",";
	    }
	    cout << a[mid-1] <<endl;
	}
	else
	{
		cout << mid-1 <<endl;
	    for(i=0;i<sum;i++)
	    {
		   cout << b[i] <<",";
    	}
	    cout << a[mid-1] <<endl;
	}
 } 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值