어떤 요 소 를 집중 적 으로 찾 습 니 다.

             ,            ,     
#include<stdio.h>
#include<malloc.h>
#define ERROR 0
#define OK 1
int nodenum;
typedef struct node
{
	int data;
	int parent;
}sqtree;
typedef struct code
{
   sqtree tree[100];
}bingcha,*bingchatree;
void initialization(bingchatree &ss,int elem[],int n)
{
	int i;
	nodenum=n;
	for(i=0;i<nodenum;i++)
	{
		ss->tree[i].data=elem[i];
		ss->tree[i].parent=-1;
	}
}
int find(bingchatree &ss,int x,int n)
{
	int pos,i;
	for(i=0;i<n;i++)
	{
		if(x==ss->tree[i].data)
		{
            pos=i;
			break;
		}
	}
    if(pos<0||pos>=n)
	return -1;
	i=pos;
	while(ss->tree[i].parent>0)
	i=ss->tree[i].parent;
	int root=i;
	i=pos;
	int temp;
	while(i!=root)
	{
		temp=ss->tree[i].parent;
		ss->tree[i].parent=root;
		i=temp;
	}
	return root;
}
int main()
{
	int n,elem[100];
	printf("   n     S         : ");
	scanf("%d",&n);
	printf("        : ");
	for(int i=0;i<n;i++)
	scanf("%d",&elem[i]);
    bingchatree ss;
	ss=(bingchatree)malloc(sizeof(bingcha));
	initialization(ss,elem,n);
	int x,x1;
	printf("     S      , SS      ,      : ");
	scanf("%d",&x);
	x1=find(ss,x,n);
	printf("%d
",x1); return 0; }

좋은 웹페이지 즐겨찾기