链式静态二叉树练习

来源:互联网 发布:美国退还庚子赔款知乎 编辑:程序博客网 时间:2024/06/12 02:43

# include <stdio.h>
# include <malloc.h>

typedef struct StaticTwoTreeNode
{
 char a;
 struct StaticTwoTreeNode * lpoint;
 struct StaticTwoTreeNode * rpoint;
}StaticTwoTreeNode,* pStaticTwoTreeNode;


pStaticTwoTreeNode CreatList();
void firstOlderTraversal(pStaticTwoTreeNode);
void centerOlderTraversal(pStaticTwoTreeNode);
void afterOlderTraversal(pStaticTwoTreeNode);

 

int main(void)
{
 pStaticTwoTreeNode pT = CreatList();
 firstOlderTraversal(pT);
 printf("/n");
 centerOlderTraversal(pT);
 printf("/n");
 afterOlderTraversal(pT);

 return 0;
}

 

pStaticTwoTreeNode CreatList(void)

 pStaticTwoTreeNode pTa = (pStaticTwoTreeNode)malloc(sizeof(StaticTwoTreeNode));
 pStaticTwoTreeNode pTb = (pStaticTwoTreeNode)malloc(sizeof(StaticTwoTreeNode));
 pStaticTwoTreeNode pTc = (pStaticTwoTreeNode)malloc(sizeof(StaticTwoTreeNode));
 pStaticTwoTreeNode pTd = (pStaticTwoTreeNode)malloc(sizeof(StaticTwoTreeNode));
 pStaticTwoTreeNode pTe = (pStaticTwoTreeNode)malloc(sizeof(StaticTwoTreeNode));

 pTa->a = 'A';
 pTa->lpoint = pTb;
 pTa->rpoint = pTc;

 pTb->a = 'B';
 pTb->lpoint = NULL;
 pTb->rpoint =NULL;

 pTc->a = 'C';
 pTc->lpoint = NULL;
 pTc->rpoint = pTd;

 pTd->a = 'D';
 pTd->lpoint = pTe;
 pTd->rpoint =NULL;

 pTe->a = 'E';
 pTe->lpoint = pTe->rpoint = NULL;

 return pTa;

}

void firstOlderTraversal(pStaticTwoTreeNode pT)
{

 if (pT != NULL)
 {
  printf("%c", pT->a);
  if (pT->lpoint !=NULL)
  {
   firstOlderTraversal( pT->lpoint);
  }
  if (pT->rpoint != NULL)
  {
   firstOlderTraversal( pT->rpoint);
  }
 }
}

void centerOlderTraversal(pStaticTwoTreeNode pT)
{

 if (pT != NULL)
 {
  if (pT->lpoint !=NULL)
  {
   centerOlderTraversal( pT->lpoint);
  }
  printf("%c", pT->a);
  if (pT->rpoint != NULL)
  {
   centerOlderTraversal( pT->rpoint);
  }
 }
}

void afterOlderTraversal(pStaticTwoTreeNode pT)
{

 if (pT != NULL)
 {
  if (pT->lpoint !=NULL)
  {
   afterOlderTraversal( pT->lpoint);
  }
  if (pT->rpoint != NULL)
  {
   afterOlderTraversal( pT->rpoint);
  }
  printf("%c", pT->a);
 }
}

原创粉丝点击