ZBLOG

#include #include //结构体定义 typedef struct Node { char data; struct Node *LChild; struct Node *RChild; }BinNode,*BinTree; //函数声明 int menu_select(); void CreateBinTree(BinTree *bt); void PreOrder(BinTree bt); void InOrder(Bi...

#include #include

//结构体定义 typedef struct Node {

  1. char data;
  2. struct Node *LChild;
  3. struct Node *RChild;

}BinNode,*BinTree;

//函数声明 int menu_select(); void CreateBinTree(BinTree *bt); void PreOrder(BinTree bt); void InOrder(BinTree bt); void PrintTree(BinTree bt,int nLayer);

// main()函数 int main() {

  1. BinTree bt = NULL;
  2. int nLayer=0;
  3. for (;;) {
  4. switch(menu_select()) {
  5. case 1:
  6. printf("1、建立二叉树\n");
  7. CreateBinTree(&bt);
  8. break;
  9. case 2:
  10. printf("2、先序遍历\n");
  11. PreOrder(bt);
  12. break;
  13. case 3:
  14. printf("3、中序遍历\n");
  15. InOrder(bt);
  16. break;
  17. case 4:
  18. printf("4、打印树形\n");
  19. PrintTree(bt,nLayer);
  20. break;
  21. case 0:
  22. printf("再见!!\n");
  23. free(bt);
  24. return 0;
  25. }
  26. }

}

//菜单驱动程序 int menu_select() {

  1. int sn;
  2. printf("=========功能菜单============\n");
  3. printf(" 1、建立二叉树\n");
  4. printf(" 2、先序遍历\n");
  5. printf(" 3、中序遍历\n");
  6. printf(" 4、打印树形\n");
  7. printf(" 0、退出程序\n");
  8. printf("==============================\n");
  9. printf(" 请选择0--4:");
  10. for (;;) {
  11. scanf("%d", &sn);
  12. getchar();
  13. if (sn < 0 || sn > 4)
  14. printf("\n输入选择错误,请重新选择 0--4:");
  15. else
  16. break;
  17. }
  18. return sn;

}

/* TODO: 功能描述:创建二叉树操作 1、按扩展的先序次序输入二叉树中结点的值,

  1. #字符表示空子树,创建二叉链表表示的二叉树
  2. (BinTree) malloc(sizeof(BinNode)),
  3. 将字符赋给节点中的data
  4. 将二叉树的左右子树置为NULL

例:

  1. 输入格式:AB##C##
  2. 二叉树:
  3. A
  4. B C

参数:(BinTree *bt) 树的指针的指针 返回值:void */ void CreateBinTree(BinTree *bt) {

  1. char c;
  2. scanf("%c", &c);
  3. if(c == '#') {
  4. *bt = NULL;
  5. } else {
  6. *bt = (BinTree) malloc(sizeof(BinNode));
  7. (*bt)->data = c;
  8. CreateBinTree(&((*bt)->LChild)); //递归创建左子树
  9. CreateBinTree(&((*bt)->RChild)); //递归创建右子树
  10. }

}

void Visit(BinTree bt) {

  1. if (bt != NULL)
  2. printf("%2c",bt->data);

}

/* TODO: 功能描述:先序遍历二叉树操作 1、先序访问根节点,遍历左子树,遍历右子树 2、打印数据调用函数 void Visit(BinTree bt) 参数:(BinTree bt) 树的指针 返回值:void */ void PreOrder(BinTree bt) {

  1. if (bt != NULL) {
  2. Visit(bt);
  3. PreOrder(bt->LChild);
  4. PreOrder(bt->RChild);
  5. }

}

/* TODO: 功能描述:中序遍历二叉树操作 1、中序遍历左子树,根节点,遍历右子树 2、打印数据调用函数 void Visit(BinTree bt) 参数:(BinTree bt) 树的指针 返回值:void */ void InOrder(BinTree bt) {

  1. if (bt != NULL) {
  2. InOrder(bt->LChild);
  3. Visit(bt);
  4. InOrder(bt->RChild);
  5. }

}

void Printdata(BinTree bt) {

  1. if (bt != NULL)
  2. printf("%2c\n",bt->data);

} void levelspace(int nLayer) {

  1. int i;
  2. for(i = 0; i < nLayer; i++)
  3. printf(" ");

}

/* TODO: 功能描述:横向打印树形操作 1、遍历右子树,根节点,遍历左子树 2、非同层节点调用 void levelspace(int nLayer) 3、打印数据调用函数 void Printdata(BinTree bt) 参数:(BinTree bt,int nLayer) 树的指针与节点层次 返回值:void */ void PrintTree(BinTree bt,int nLayer) {

  1. if (bt != NULL) {
  2. PrintTree(bt->RChild, nLayer+1);
  3. levelspace(nLayer);
  4. Printdata(bt);
  5. PrintTree(bt->LChild, nLayer+1);
  6. }

}

本站部分文章来源于网络,版权归原作者所有,如有侵权请联系站长删除。
转载请注明出处:https://golang.0voice.com/?id=1171

分享:
扫描分享到社交APP
上一篇
下一篇
发表列表
游客 游客
此处应有掌声~
评论列表

还没有评论,快来说点什么吧~

联系我们

在线咨询: 点击这里给我发消息

微信号:3007537140

上班时间: 10:30-22:30

关注我们
x

注册

已经有帐号?