ZBLOG

#include #include #include typedef char ElemType; //定义二叉树结点值的类型为字符型 int LEAFCOUNT=0; int NODECOUNT=0; typedef struct BiTNode { ///===============补充代码======================== }BiTNode, *BiTree; void Cr...

这段代码是一个二叉树的基本操作实现。以下是对应的补充代码:

  1. typedef struct BiTNode
  2. {
  3. ElemType data;
  4. struct BiTNode *lchild, *rchild;
  5. }BiTNode, *BiTree;
  6. void CreateBiTree(BiTree &T)
  7. {
  8. char ch;
  9. scanf("%c", &ch);
  10. if (ch == '#')
  11. T = NULL;
  12. else
  13. {
  14. T = (BiTree)malloc(sizeof(BiTNode));
  15. T->data = ch;
  16. CreateBiTree(T->lchild);
  17. CreateBiTree(T->rchild);
  18. }
  19. }
  20. void PreOrderTraverse(BiTree T)
  21. {
  22. if (T)
  23. {
  24. printf("%c ", T->data);
  25. PreOrderTraverse(T->lchild);
  26. PreOrderTraverse(T->rchild);
  27. }
  28. }
  29. void InOrderTraverse(BiTree T)
  30. {
  31. if (T)
  32. {
  33. InOrderTraverse(T->lchild);
  34. printf("%c ", T->data);
  35. InOrderTraverse(T->rchild);
  36. }
  37. }
  38. void PostOrderTraverse(BiTree T)
  39. {
  40. if (T)
  41. {
  42. PostOrderTraverse(T->lchild);
  43. PostOrderTraverse(T->rchild);
  44. printf("%c ", T->data);
  45. }
  46. }
  47. int BTDepth(BiTree T)
  48. {
  49. if (T == NULL)
  50. return 0;
  51. int leftDepth = BTDepth(T->lchild);
  52. int rightDepth = BTDepth(T->rchild);
  53. return leftDepth > rightDepth ? leftDepth + 1 : rightDepth + 1;
  54. }
  55. void Leaf(BiTree T)
  56. {
  57. if (T != NULL)
  58. {
  59. if (T->lchild == NULL && T->rchild == NULL)
  60. LEAFCOUNT++;
  61. Leaf(T->lchild);
  62. Leaf(T->rchild);
  63. }
  64. }
  65. void NodeCount(BiTree T)
  66. {
  67. if (T != NULL)
  68. {
  69. NODECOUNT++;
  70. NodeCount(T->lchild);
  71. NodeCount(T->rchild);
  72. }
  73. }

此外,还需要注意在main()函数中对应的调用和输入处理。


内容由零声教学AI助手提供,问题来源于学员提问

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

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

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

联系我们

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

微信号:3007537140

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

关注我们
x

注册

已经有帐号?