//在论坛提了很多关于把双亲结点的树转化为二叉树,但就是没人回答!!奋斗了一天自己写出个!自己按程序走了下觉得是对的但运行起来达不到应该的结果 
                              1 
                            / | \ 
                          2  3  4 
                              /\ 
                            5  6 
                                | 
                                7 
这是我的开始的双亲结点的树  下面程序里有 {CreateTree是建的树}  我在这里用图展示下!!!!! 
TobinaryTree是转化算法  
大家给看看 对不对  本人刚开始接触数据结构  但很喜欢!!希望高手能帮帮我!!!!
谢了~~~~~~~~~~~~~~~~~~~ 


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

typedef int Elem; 
typedef struct NTree 

Elem data; 
struct NTree *lchild,*rchild ; 
}NTree,*BiTree; 
typedef struct PTNode 

      Elem  data; 
      int    parent;  // 双亲位置域 
}PTNode,*PNode; 
typedef struct PTree 

    PNode nodes[20];//结点最大数量为20 
    int    n;    // 结点个数 
}PTree,*Tree; 

/*******************实现方法如下*********************************/ 
void addTree(Tree &t,PNode node); 

//构造双亲结点的树 
void InitTree(Tree &t); 
void InitTree(Tree &t) 


    t=(PTree*)malloc(sizeof(PTree)); //这是为啥呢 
    t->n=0; 
    for(int i = 0;i <20;i++) 
    t->nodes[i]=(PTNode*)malloc(sizeof(PTNode)); 

void CreateTree(Tree &t) 

int n=t->n; 
//记录结点的个数; 
PTNode *node=NULL; 
node=(PTNode*)malloc(sizeof(PTNode)); 
node->data=1; 
node->parent=-1; 

addTree(t,node);//1 

PTNode *node2=(PTNode*)malloc(sizeof(PTNode)); 
node2->data=2; 
node2->parent=0; 
addTree(t,node2);//2 

PTNode *node3=(PTNode*)malloc(sizeof(PTNode)); 
node3->data=3; 
node3->parent=0; 
addTree(t,node3);//3 

PTNode *node4=(PTNode*)malloc(sizeof(PTNode)); 
node4->data=4; 
node4->parent=0; 
addTree(t,node4);//4 

PTNode *node5=(PTNode*)malloc(sizeof(PTNode)); 
node5->data=5; 
node5->parent=2; 
addTree(t,node5);//5 

PTNode *node6=(PTNode*)malloc(sizeof(PTNode)); 
node6->data=6; 
node6->parent=2; 
addTree(t,node6);//6 

PTNode *node7=(PTNode*)malloc(sizeof(PTNode)); 
node7->data=7; 
node7->parent=5; 
addTree(t,node7);//7 
printf("%d\n",t->n); 

//建树一共有7个结点 

void addTree(Tree &t,PNode node) 

int n=t->n; 
int pa=node->parent; 
if(pa>n) 
printf("结点信息有错误"); 
  

t->nodes[n]->data=node->data; 
t->nodes[n]->parent=node->parent; 
t->n++;//节点数自增 



void ToBinaryTree(Tree t,BiTree &tree,int where)//更新结点的位置 

{//把where结点赋值给tree  然后寻找左子树和右子树 
int i=where+1; 
    int con=1; 
int pa=t->nodes[where]->parent; 
//printf("%d\n",pa); 
tree->lchild=(BiTree)malloc(sizeof(NTree)); 
tree->rchild=(BiTree)malloc(sizeof(NTree)); 
//各节点初始化 
tree->data=t->nodes[where]->data;//赋值根节点-> 

for(int j=i;j <t->n;j++) 
{  
//int j=i; 
    if(t->nodes[j]->parent==pa&&con==1)//说明有兄弟 

ToBinaryTree(t,tree->rchild,j); 
con=0; 
//printf("you子树\n"); 


      if(t->nodes[j]->parent==where)//寻找左 


//printf("左子树\n"); 
ToBinaryTree(t,tree->lchild,j);//第一个子树为左子树 

}//for 

int  Preorder(BiTree T) 
{  
    printf("%d\n",T->data); 
    if(T->lchild)Preorder(T->lchild); 
    if(T->rchild)Preorder(T->rchild); 
return 1; 

void main() 
{  
Tree t; 
InitTree(t); 
int n=t->n; 
printf("%d\n",n); 
BiTree T; 
T=(BiTree)malloc(sizeof(NTree)); 
//int n; 
LinkStack *s; 

CreateTree(t); 
printf("建树完成\n"); 
ToBinaryTree(t,T,0); 
printf("完成转换\n"); 
printf("前序遍历结果是:\n"); 
Preorder(T); 
printf("输出完成"); 
}


[fly]谢啦!!!!![/fly]