急急急!!!怎么用C语言输出一个完全二叉树!!!就不是C++也不是JAVA,要C编的代码,在线等QAQ
急急急!!!怎么用C语言输出一个完全二叉树!!!就不是C++也不是JAVA,要C编的代码,在线等QAQ求完整代码,不是思路提示...
急急急!!!怎么用C语言输出一个完全二叉树!!!就不是C++也不是JAVA,要C编的代码,在线等QAQ
求完整代码,不是思路提示 展开
求完整代码,不是思路提示 展开
3个回答
展开全部
/*
2013年3月21日 17:37:19
目的:实现"静态"链式二叉表的操作(此处的静态是指已知二叉树的结构)
*/
#include <stdio.h>
#include <malloc.h>
typedef struct TreeNode
{
int Data;
struct TreeNode * pLchild;
struct TreeNode * pRchild;
}TN, *pTN;
pTN CreatTree(void);
void PreTraverse(pTN);/*先序编历*/
void InTraverse(pTN);/*中序遍历*/
void PostTraverse(pTN);/*后序遍历*/
int main(void)
{
pTN Root;
Root = CreatTree();
PreTraverse(Root);
puts("\n");
InTraverse(Root);
puts("\n");
PostTraverse(Root);
puts("\n");
return 0;
}
pTN CreatTree(void)
{
pTN A = (pTN)malloc(sizeof(TN));
pTN B = (pTN)malloc(sizeof(TN));
pTN C = (pTN)malloc(sizeof(TN));
pTN D = (pTN)malloc(sizeof(TN));
pTN E = (pTN)malloc(sizeof(TN));
pTN F = (pTN)malloc(sizeof(TN));
A->Data = 'A';
B->Data = 'B';
C->Data = 'C';
D->Data = 'D';
E->Data = 'E';
F->Data = 'F';
A->pLchild = B;
B->pLchild = D;
B->pRchild = E;
D->pLchild = D->pRchild = NULL;
E->pLchild = E->pRchild = NULL;
A->pRchild = C;
C->pLchild = NULL;
C->pRchild = F;
F->pLchild = F->pRchild = NULL;
return A;
}
void PreTraverse(pTN Root)
{
if (NULL != Root)
{
printf("%c ", Root->Data);
}
if (NULL != Root->pLchild)
{
PreTraverse(Root->pLchild);
}
if (NULL != Root->pRchild)
{
PreTraverse(Root->pRchild);
}
return;
}
void InTraverse(pTN Root)
{
if (NULL != Root->pLchild)
{
InTraverse(Root->pLchild);
}
printf("%c ", Root->Data);
if (NULL != Root)
{
}
if (NULL != Root->pRchild)
{
InTraverse(Root->pRchild);
}
return;
}
void PostTraverse(pTN Root)
{
if (NULL != Root->pLchild)
{
PostTraverse(Root->pLchild);
}
if (NULL != Root->pRchild)
{
PostTraverse(Root->pRchild);
}
printf("%c ", Root->Data);
if (NULL != Root)
{}
return;
}
2013年3月21日 17:37:19
目的:实现"静态"链式二叉表的操作(此处的静态是指已知二叉树的结构)
*/
#include <stdio.h>
#include <malloc.h>
typedef struct TreeNode
{
int Data;
struct TreeNode * pLchild;
struct TreeNode * pRchild;
}TN, *pTN;
pTN CreatTree(void);
void PreTraverse(pTN);/*先序编历*/
void InTraverse(pTN);/*中序遍历*/
void PostTraverse(pTN);/*后序遍历*/
int main(void)
{
pTN Root;
Root = CreatTree();
PreTraverse(Root);
puts("\n");
InTraverse(Root);
puts("\n");
PostTraverse(Root);
puts("\n");
return 0;
}
pTN CreatTree(void)
{
pTN A = (pTN)malloc(sizeof(TN));
pTN B = (pTN)malloc(sizeof(TN));
pTN C = (pTN)malloc(sizeof(TN));
pTN D = (pTN)malloc(sizeof(TN));
pTN E = (pTN)malloc(sizeof(TN));
pTN F = (pTN)malloc(sizeof(TN));
A->Data = 'A';
B->Data = 'B';
C->Data = 'C';
D->Data = 'D';
E->Data = 'E';
F->Data = 'F';
A->pLchild = B;
B->pLchild = D;
B->pRchild = E;
D->pLchild = D->pRchild = NULL;
E->pLchild = E->pRchild = NULL;
A->pRchild = C;
C->pLchild = NULL;
C->pRchild = F;
F->pLchild = F->pRchild = NULL;
return A;
}
void PreTraverse(pTN Root)
{
if (NULL != Root)
{
printf("%c ", Root->Data);
}
if (NULL != Root->pLchild)
{
PreTraverse(Root->pLchild);
}
if (NULL != Root->pRchild)
{
PreTraverse(Root->pRchild);
}
return;
}
void InTraverse(pTN Root)
{
if (NULL != Root->pLchild)
{
InTraverse(Root->pLchild);
}
printf("%c ", Root->Data);
if (NULL != Root)
{
}
if (NULL != Root->pRchild)
{
InTraverse(Root->pRchild);
}
return;
}
void PostTraverse(pTN Root)
{
if (NULL != Root->pLchild)
{
PostTraverse(Root->pLchild);
}
if (NULL != Root->pRchild)
{
PostTraverse(Root->pRchild);
}
printf("%c ", Root->Data);
if (NULL != Root)
{}
return;
}
追问
好多错啊亲...........
追答
不会啊输出结果:
A B D E C F
D B E A C F
D E B F C A
Press any key to continue
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询