当前位置:   article > 正文

头歌数据结构实训参考---二叉树及其应用_实现二叉树的创建头歌

实现二叉树的创建头歌

第1关 实现二叉树的创建

  1. #include "binary_tree.h"
  2. BiTreeNode* CreatBiTree(char* s, int &i, int len)
  3. // 利用先序遍历创建二叉树
  4. // 参数:先序遍历字符串s,字符串初始下标i=0,字符串长度len。
  5. // 返回:二叉树
  6. {
  7. // 请在这里补充代码,完成本关任务
  8. /********** Begin *********/
  9. BiTreeNode *root;
  10. char chk;
  11. chk=s[i++];
  12. if(chk=='#')
  13. root=NULL;
  14. else
  15. {
  16. root=(BiTreeNode*)malloc(sizeof(BiTreeNode));
  17. root->data=chk;
  18. if(root!=NULL)
  19. {
  20. root->left=CreatBiTree(s,i,len);
  21. root->right=CreatBiTree(s,i,len);
  22. }
  23. }
  24. return root;
  25. /********** End **********/
  26. }
  27. void InOrder(BiTreeNode* root)
  28. // 二叉树的中序遍历
  29. // 参数:二叉树根节点root
  30. // 输出:中间没有空格,末尾不换行。
  31. {
  32. // 请在这里补充代码,完成本关任务
  33. /********** Begin *********/
  34. if(root)
  35. {
  36. InOrder(root->left);
  37. printf("%c",root->data);
  38. InOrder(root->right);
  39. }
  40. /********** End **********/
  41. }

第2关 计算二叉树的深度和节点个数

  1. #include "binary_tree.h"
  2. int sum=0;
  3. int leaf=0;
  4. int GetTreeDepth(BiTreeNode* root)
  5. // 计算该二叉树的深度
  6. // 参数:二叉树根节点root
  7. // 返回:二叉树的深度
  8. {
  9. // 请在这里补充代码,完成本关任务
  10. /********** Begin *********/
  11. int m=0,n=0,max=0;
  12. if(root)
  13. {
  14. m=GetTreeDepth(root->left);
  15. n=GetTreeDepth(root->right);
  16. (m>n)?max=(m+1):max=(n+1);
  17. }
  18. return max;
  19. /********** End **********/
  20. }
  21. int GetNodeNumber(BiTreeNode* root)
  22. // 计算该二叉树的总节点个数
  23. // 参数:二叉树根节点root
  24. // 返回:二叉树的总节点个数
  25. {
  26. // 请在这里补充代码,完成本关任务
  27. /********** Begin *********/
  28. if(root)
  29. {
  30. sum++;
  31. GetNodeNumber(root->left);
  32. GetNodeNumber(root->right);
  33. }
  34. return sum;
  35. /********** End **********/
  36. }
  37. int GetLeafNodeNumber(BiTreeNode* root)
  38. // 计算该二叉树的叶子节点个数
  39. // 参数:二叉树根节点root
  40. // 返回:二叉树的叶子节点个数
  41. {
  42. // 请在这里补充代码,完成本关任务
  43. /********** Begin *********/
  44. if(root)
  45. {
  46. if(root->left==root->right)
  47. leaf++;
  48. GetLeafNodeNumber(root->left);
  49. GetLeafNodeNumber(root->right);
  50. }
  51. return leaf;
  52. /********** End **********/
  53. }

第3关 递归实现二叉树左右子树交换

  1. #include "binary_tree.h"
  2. BiTreeNode* BiTreeChange(BiTreeNode* root)
  3. // 实现二叉树左右子树的交换(递归法)
  4. // 参数:二叉树根节点root
  5. // 返回:二叉树
  6. {
  7. // 请在这里补充代码,完成本关任务
  8. /********** Begin *********/
  9. if(root==NULL)return NULL;
  10. BiTreeNode* temp=root->left;
  11. root->left=root->right;
  12. root->right=temp;
  13. if(root->left!=NULL)
  14. BiTreeChange(root->left);
  15. if(root->right!=NULL)
  16. BiTreeChange(root->right);
  17. return root;
  18. /********** End **********/
  19. }
  20. void PreOrder(BiTreeNode* root)
  21. // 二叉树的前序遍历
  22. // 参数:二叉树根节点root
  23. // 输出:二叉树的前序遍历,中间没有空格,末尾不换行。
  24. {
  25. // 请在这里补充代码,完成本关任务
  26. /********** Begin *********/
  27. if(root)
  28. {
  29. printf("%c",root->data);
  30. PreOrder(root->left);
  31. PreOrder(root->right);
  32. }
  33. /********** End **********/
  34. }

第4关 非递归实现二叉树左右子树交换

  1. #include "binary_tree.h"
  2. BiTreeNode* BiTreeChangeStack(BiTreeNode* root)
  3. // 实现二叉树左右子树的交换(栈实现)
  4. // 参数:二叉树根节点root
  5. // 返回:二叉树
  6. {
  7. // 请在这里补充代码,完成本关任务
  8. /********** Begin *********/
  9. if(root==NULL)return NULL;
  10. stack<BiTreeNode*> swap;
  11. swap.push(root->left);
  12. swap.push(root->right);
  13. root->left=swap.top();
  14. swap.pop();
  15. root->right=swap.top();
  16. if(root->left!=NULL)
  17. BiTreeChangeStack(root->left);
  18. if(root->right!=NULL)
  19. BiTreeChangeStack(root->right);
  20. return root;
  21. /********** End **********/
  22. }
  23. void PostOrder(BiTreeNode* root)
  24. // 二叉树的后序遍历
  25. // 参数:二叉树根节点root
  26. // 输出:二叉树的后序遍历,中间没有空格,末尾不换行。
  27. {
  28. // 请在这里补充代码,完成本关任务
  29. /********** Begin *********/
  30. if(root)
  31. {
  32. PostOrder(root->left);
  33. PostOrder(root->right);
  34. printf("%c",root->data);
  35. }
  36. /********** End **********/
  37. }

第5关 层次遍历二叉树

  1. #include "binary_tree.h"
  2. void HierarchyOrder(BiTreeNode* root)
  3. // 二叉树的层次遍历(队列实现)
  4. // 参数:二叉树根节点root
  5. // 输出:二叉树的层次遍历,中间没有空格,末尾不换行。
  6. {
  7. // 请在这里补充代码,完成本关任务
  8. /********** Begin *********/
  9. queue<BiTreeNode*> t;
  10. if(root)
  11. t.push(root);
  12. while(!t.empty())
  13. {
  14. BiTreeNode* T=t.front();
  15. t.pop();
  16. printf("%c",T->data);
  17. if(T->left)
  18. t.push(T->left);
  19. if(T->right)
  20. t.push(T->right);
  21. }
  22. /********** End **********/
  23. }

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/人工智能uu/article/detail/737592
推荐阅读
相关标签
  

闽ICP备14008679号