第十周项目三 后序遍历二叉树

  1. 烟台大学计算机学院  
  2.     
  3. 作者:王雪行
  4.   
  5. 问题描述:用二叉树来表示代数表达式 
  6.   
  7. 输入描述:无 
  8.   
  9. 输出描述:后序遍历结果以及算式结果 
  10.  
  11. 用到btree2.h算法库 
  12. */   
  13.   
  14.   
  15.   
  16. #include <stdio.h>  
  17. #include <string.h>  
  18. #include <malloc.h>  
  19. #include "../btree2.h"  
  20.   
  21. //用s[i]到s[j]之间的字符串,构造二叉树的表示形式  
  22. BTNode *CRTree(char s[],int i,int j)  
  23. {  
  24.     BTNode *p;  
  25.     int k,plus=0,posi;  
  26.     if (i==j)    //i和j相同,意味着只有一个字符,构造的是一个叶子节点  
  27.     {  
  28.         p=(BTNode *)malloc(sizeof(BTNode));   //分配存储空间  
  29.         p->data=s[i];                         //值为s[i]  
  30.         p->lchild=NULL;  
  31.         p->rchild=NULL;  
  32.         return p;  
  33.     }  
  34.     //以下为i!=j的情况  
  35.     for (k=i; k<=j; k++)  
  36.         if (s[k]=='+' || s[k]=='-')  
  37.         {  
  38.             plus++;  
  39.             posi=k;              //最后一个+或-的位置  
  40.         }  
  41.     if (plus==0)                 //没有+或-的情况(因为若有+、-,前面必会执行plus++)  
  42.         for (k=i; k<=j; k++)  
  43.             if (s[k]=='*' || s[k]=='/')  
  44.             {  
  45.                 plus++;  
  46.                 posi=k;  
  47.             }  
  48.     //以上的处理考虑了优先将+、-放到二叉树较高的层次上  
  49.     //由于将来计算时,运用的是后序遍历的思路  
  50.     //处于较低层的乘除会优先运算  
  51.     //从而体现了“先乘除后加减”的运算法则  
  52.     //创建一个分支节点,用检测到的运算符作为节点值  
  53.     if (plus!=0)  
  54.     {  
  55.         p=(BTNode *)malloc(sizeof(BTNode));  
  56.         p->data=s[posi];                //节点值是s[posi]  
  57.         p->lchild=CRTree(s,i,posi-1);   //左子树由s[i]至s[posi-1]构成  
  58.         p->rchild=CRTree(s,posi+1,j);   //右子树由s[poso+1]到s[j]构成  
  59.         return p;  
  60.     }  
  61.     else       //若没有任何运算符,返回NULL  
  62.         return NULL;  
  63. }  
  64.   
  65. double Comp(BTNode *b)  
  66. {  
  67.     double v1,v2;  
  68.     if (b==NULL)  
  69.         return 0;  
  70.     if (b->lchild==NULL && b->rchild==NULL)  //叶子节点,应该是一个数字字符(本项目未考虑非法表达式)  
  71.         return b->data-'0';    //叶子节点直接返回节点值,结点中保存的数字用的是字符形式,所以要-'0'  
  72.     v1=Comp(b->lchild); //先计算左子树  
  73.     v2=Comp(b->rchild); //再计算右子树  
  74.     switch(b->data)     //将左、右子树运算的结果再进行运算,运用的是后序遍历的思路  
  75.     {  
  76.     case '+':  
  77.         return v1+v2;  
  78.     case '-':  
  79.         return v1-v2;  
  80.     case '*':  
  81.         return v1*v2;  
  82.     case '/':  
  83.         if (v2!=0)  
  84.             return v1/v2;  
  85.         else  
  86.             abort();  
  87.     }  
  88. }  
  89.   
  90. int main()  
  91. {  
  92.     BTNode *b;  
  93.     char s[MaxSize]="1+2*3-4/5";  
  94.     printf("代数表达式%s\n",s);  
  95.     b=CRTree(s,0,strlen(s)-1);  
  96.     printf("对应二叉树:");  
  97.     DispBTNode(b);  
  98.     printf("\n表达式的值:%g\n",Comp(b));  
  99.     DestroyBTNode(b);  
  100.     return 0;  
  101. }  

运行结果:


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,我已经记录下来了。下面是对您提出的问题的回答: 1、先序遍历二叉树的递归算法: ```c++ void preOrder(TreeNode* root) { if (root == nullptr) { return; } cout << root->val << " "; preOrder(root->left); preOrder(root->right); } ``` 2、序遍历二叉树的递归算法: ```c++ void inOrder(TreeNode* root) { if (root == nullptr) { return; } inOrder(root->left); cout << root->val << " "; inOrder(root->right); } ``` 3、后序遍历二叉树的递归算法: ```c++ void postOrder(TreeNode* root) { if (root == nullptr) { return; } postOrder(root->left); postOrder(root->right); cout << root->val << " "; } ``` 4、计算二叉树的深度的递归算法: ```c++ int depth(TreeNode* root) { if (root == nullptr) { return 0; } int leftDepth = depth(root->left); int rightDepth = depth(root->right); return max(leftDepth, rightDepth) + 1; } ``` 5、统计二叉树的结点个数的递归算法: ```c++ int countNodes(TreeNode* root) { if (root == nullptr) { return 0; } int leftCount = countNodes(root->left); int rightCount = countNodes(root->right); return leftCount + rightCount + 1; } ``` 6、统计二叉树的叶子结点个数的递归算法: ```c++ int countLeaves(TreeNode* root) { if (root == nullptr) { return 0; } if (root->left == nullptr && root->right == nullptr) { return 1; } int leftLeaves = countLeaves(root->left); int rightLeaves = countLeaves(root->right); return leftLeaves + rightLeaves; } ``` 7、设计该二叉树第K层的结点个数: ```c++ int countNodesKthLevel(TreeNode* root, int k) { if (root == nullptr) { return 0; } if (k == 1) { return 1; } int leftCount = countNodesKthLevel(root->left, k - 1); int rightCount = countNodesKthLevel(root->right, k - 1); return leftCount + rightCount; } ``` 8、求该二叉树所有结点值最大的元素: ```c++ int findMax(TreeNode* root) { if (root == nullptr) { return INT_MIN; } int leftMax = findMax(root->left); int rightMax = findMax(root->right); return max(max(leftMax, rightMax), root->val); } ``` 9、打印二叉树的叶子结点数的递归算法: ```c++ void printLeaves(TreeNode* root) { if (root == nullptr) { return; } if (root->left == nullptr && root->right == nullptr) { cout << root->val << " "; return; } printLeaves(root->left); printLeaves(root->right); } ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值