c语言数据结构--好友推荐算法--图(社交网络)与二叉树查找问题
已经有下列函数及代码,请帮忙完善或者修改一下,来实现以上功能。
#include <stdio.h>
#include <stdlib.h>
typedef struct BiTreeNode
{
struct BTreeNode* left;
struct BTreeNode* right;
BTDataType data;
}BTNode;
typedef struct person_information
{
char name[20];
int age;
char job[20];
char friends[20];
}temp, person[10];
//创立二叉树
BTNode* CreateBTreeNode(BTDataType x)
{
BTNode* node = (BTNode*)malloc(sizeof(BTNode));
node->data = x;
node->left = NULL;
node->right = NULL;
return node;
}
int find(int x)
{
if(x == a[x])
return a[x];
a[x] = find(a[x]);
return a[x];
}
//判断两棵给定的树是否等价
int equal(tree t1,tree t2)
{
int k;
if(t1==NULL&&t2==NULL)
{
return TRUE;
}
else if(t1!=NULL&&t2==NULL||t1==NULL&&t2!=NULL)
{
return FALSE;
}
else if(t1->data!=t2->data)
{
return FALSE;
}
for(k=0;k<m;k++)
{
equal(t1->child[k],t2->child[k]);
if(equal(t1->child[k],t2->child[k])==FALSE)
{
return FALSE;
}
else
{
return TRUE;
}
}
}
//实现二叉树t的非递归前序遍历
void preorder1(bintree t)
{
seqstack s;
init(&s);
while(t||!empty(&s))
{
if(t)
{
printf("%c",t->data);
push(&s,t);
t=t->lchild;
}
else if(!empty(&s))
{
t=pop(&s);
t=t->rchild;
}
}
}
//输出以邻接表为存储结构的无向图的各顶点的度
void degree(LinkedGraph g)
{
int k;
int n;
EdgeNode *p;
for(k=0;k<g.n;k++)
{
p=g.adjlist[k].FirstEdge;
n=0;
while(p!=NULL)
{
n++;
p=p->next;
}
if(k==0)
{
printf("%d\n",n);
}
else
{
printf("%d\n",n);
}
}
}
int a[300];
int main()
{
int i,j,n,index;
scanf("%d",&n);
for(i=0;i<n;i++)
{
scanf("%s%d%s", person[i].name, &person[i].age, person[i].job,person[i].friends);
}
for(i=0;i<n;i++)
{
index=i;
for(j=i+1;j<n;j++)
{
if(person[index].age>person[j].age)
index=j;
}
temp=person[index];
person[index]=person[i];
person[i]=temp;
}
for(i=0;i<n;i++)
{
printf("%s %ld %s\n", person[i].name,person[i].age,person[i].job,person[i].friends));
}
int N;
scanf("%d\n", &N);
char graph_1[N+1][N+1];
for(int i=0;i<N;i++)
{
a[i]=i;
scanf("%s",graph_1[i]);
}
int graph_2[N][N];
for(int i = 0;i < N;i++)
for(int j = 0;j < N;j++)
{
graph_2[i][j] = graph_1[i][j]-'0';
if(graph_2[i][j] == 1)
{
if(find(i) != find(j))
a[find(i)] = find(j);
}
}
for(int i = 0;i < N;i++)
for(int j = 0;j < N;j++)
if(find(i) == find(j))
graph_2[i][j] = 1;
for(int i = 0;i < N;i++)
for(int j = 0;j < N;j++)
printf("%d",graph_2[i][j]);
printf("\n");
}
return 0;
}