我写的把双亲结点的树转化为二叉树算法!对不对???

lz12366007 2009-06-24 03:31:45
//在论坛提了很多关于把双亲结点的树转化为二叉树,但就是没人回答!!奋斗了一天自己写出个!自己按程序走了下觉得是对的但运行起来达不到应该的结果
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("输出完成");
}
...全文
250 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
lz12366007 2009-06-25
  • 打赏
  • 举报
回复
太感谢了!!
logiciel 2009-06-25
  • 打赏
  • 举报
回复

以下是在LZ基础上用VC++6.0修改调试成功的代码:

#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; // ½áµã¸öÊý
bool done[20];
}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));
t->done[i] = false;
}
}
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->data=t->nodes[where]->data;//¸³Öµ¸ù½Úµã->
tree->lchild = NULL;
tree->rchild = NULL;

for(int j=i;j <t->n;j++)
{
if (t->done[j] == false)
{
if(t->nodes[j]->parent==pa&&con==1)//˵Ã÷ÓÐÐÖµÜ
{
tree->rchild=(BiTree)malloc(sizeof(NTree));
ToBinaryTree(t,tree->rchild,j);
con=0;
}

if(t->nodes[j]->parent==where)//Ñ°ÕÒ×ó
{
tree->lchild=(BiTree)malloc(sizeof(NTree));
ToBinaryTree(t,tree->lchild,j);//µÚÒ»¸ö×ÓÊ÷Ϊ×ó×ÓÊ÷
}
}
}
t->done[where] = true;
}

void Preorder(BiTree T)
{
printf("%d\n",T->data);
if(T->lchild)
{
printf("lchild=");
Preorder(T->lchild);
}
if(T->rchild)
{
printf("rchild=");
Preorder(T->rchild);
}
}
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("Êä³öÍê³É");
}




logiciel 2009-06-24
  • 打赏
  • 举报
回复
刚才漏了一段也要修改的代码:


for(int i = 0;i <20;i++)
{
t->nodes[i]=(PTNode*)malloc(sizeof(PTNode));
t->done[i] = false;
}
logiciel 2009-06-24
  • 打赏
  • 举报
回复
在ToBinaryTree运行过程中没有不必要的malloc,其次数与节点数相同。只有等打印输出后才能free。

我力求在原程序基础上做最少的修改。
comman_ndsc 2009-06-24
  • 打赏
  • 举报
回复
看你malloc了那么多memory,你怎么就不记得free一下呢?
logiciel 2009-06-24
  • 打赏
  • 举报
回复
LZ代码的主要问题出在递归返回时把已处理过的又处理了,这样就覆盖了先前的结果。因次在PTree中增加一组标志done。

输出结果是:

1
/
2
\
3
/ \
5 4
\6
/
7

typedef struct PTree 
{
PNode nodes[20];//½áµã×î´óÊýÁ¿Îª20
int n; // ½áµã¸öÊý
bool done[20];
}PTree,*Tree;
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->data=t->nodes[where]->data;//¸³Öµ¸ù½Úµã->
tree->lchild = NULL;
tree->rchild = NULL;

for(int j=i;j <t->n;j++)
{
if (t->done[j] == false)
{
if(t->nodes[j]->parent==pa&&con==1)//˵Ã÷ÓÐÐÖµÜ
{
tree->rchild=(BiTree)malloc(sizeof(NTree));
ToBinaryTree(t,tree->rchild,j);
con=0;
}

if(t->nodes[j]->parent==where)//Ñ°ÕÒ×ó
{
tree->lchild=(BiTree)malloc(sizeof(NTree));
ToBinaryTree(t,tree->lchild,j);//µÚÒ»¸ö×ÓÊ÷Ϊ×ó×ÓÊ÷
}
}
}
t->done[where] = true;
}

void Preorder(BiTree T)
{
printf("%d\n",T->data);
if(T->lchild)
{
printf("lchild=");
Preorder(T->lchild);
}
if(T->rchild)
{
printf("rchild=");
Preorder(T->rchild);
}
}


lz12366007 2009-06-24
  • 打赏
  • 举报
回复
没人会????
lz12366007 2009-06-24
  • 打赏
  • 举报
回复
遍历出来咋是这个结果
1
4
-842150451

69,373

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧