表达式二叉树(C++实现,实现了通过前缀,中缀,后缀表达式构建二叉树)

这是一篇关于数据结构与算法的博客,详细介绍了如何使用C++实现从前缀、中缀、后缀表达式构建表达式二叉树,并能根据二叉树输出对应的表达式形式。博客内容包括问题描述和代码实现,是作者在学习数据结构时的上机作业。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

学数据结构时的上机作业,花了半天时间写的。

问题描述:

1、对于任意给出的前缀表达式(不带括号)、中缀表达式(可以带括号)或后缀表达式(不带括号),能够在计算机内部构造出一棵表达式二叉树,并且图示出来(图形的形式)。

2、对于构造好的内部表达式二叉树,按照用户的要求输出相应的前缀表达式(不带括号)、中缀表达式(可以带括号,但不允许冗余括)或后缀表达式(不带括号)。

 代码

展开
  1 //表达式二叉树
  2 //作者:王锦
  3 //邮箱:jinksw@vip.qq.com
  4 
  5 #include "stdafx.h"
  6 #include <stack>
  7 #include <utility>
  8 #include <iostream>
  9 #include <sstream>
 10 #include <queue>
 11 #include <cmath>
 12 #include <list>
 13 using namespace std;
 14 
 15 
 16 //二叉树结点类
 17 template<class T>
 18 class BinaryTreeNode
 19 {
 20 private:
 21     T value;
 22     BinaryTreeNode<T> *leftChild;
 23     BinaryTreeNode<T> *rightChild;
 24 public:
 25     BinaryTreeNode(){leftChild = NULL,rightChild = NULL;};
 26     BinaryTreeNode(const T&mValue);
 27     BinaryTreeNode(const T&mValue,BinaryTreeNode<T> *mLeftChild,BinaryTreeNode *mRightChild);
 28     
 29     void setValue(const T&mValue);
 30     T getValue()const;
 31     
 32     BinaryTreeNode<T> * getLeftChild()const;
 33     BinaryTreeNode<T> * getRightChild()const;
 34     void setLeftChild(BinaryTreeNode<T> *leftChild);
 35     void setRightChild(BinaryTreeNode<T> *rightChild);
 36 
 37     bool isLeaf()const;
 38 };
 39 template<class T>
 40 BinaryTreeNode<T>::BinaryTreeNode(const T&mValue)
 41 {
 42     value = mValue;
 43     leftChild = rightChild = NULL;
 44 }
 45 
 46 template<class T>
 47 BinaryTreeNode<T>::BinaryTreeNode(const T&mValue,BinaryTreeNode<T> *mLeftChild,BinaryTreeNode *mRightChild)
 48 {
 49     value = mValue;
 50     leftChild = mLeftChild;
 51     rightChild = mRightChild;
 52 }
 53 
 54 template<class T>
 55 T BinaryTreeNode<T>::getValue()const//得到该结点的值
 56 {
 57     return value;
 58 }
 59 
 60 template<class T>
 61 BinaryTreeNode<T>* BinaryTreeNode<T>::getLeftChild()const//得到左子结点
 62 {
 63     return leftChild;
 64 }
 65 
 66 template<class T>
 67 BinaryTreeNode<T>* BinaryTreeNode<T>::getRightChild()const//得到右子结点
 68 {
 69     return rightChild;
 70 }
 71 
 72 template<class T>
 73 void BinaryTreeNode<T>::setLeftChild(BinaryTreeNode<T> *leftChild)//设置左子结点
 74 {
 75     this->leftChild = leftChild;
 76 }
 77 
 78 template<class T>
 79 void BinaryTreeNode<T>::setRightChild(BinaryTreeNode<T> *rightChild)//设置右子结点
 80 {
 81     this->rightChild = rightChild;
 82 }
 83 
 84 template<class T>
 85 void BinaryTreeNode<T>::setValue(const T&mValue)//设置该结点的值
 86 {
 87     value = mValue;
 88 }
 89 
 90 template<class T>
 91 bool BinaryTreeNode<T>::isLeaf()const//是否为树叶
 92 {
 93     return leftChild == NULL && rightChild == NULL;
 94 }
 95 
 96 class ExpressionBinaryTree
 97 {
 98 private:
 99     BinaryTreeNode<string> *root;
100     void clear()
101     {
102         if(root == NULL) 
103             return;
104         recursionDeleteAll(root);
105     };//调用内部递归函数清空二叉树,以及释放空间
106     bool aIsGreaterOrEqualThanB(char a,char b);//工具方法,用于比较a,b优先级
107     int getHeight(BinaryTreeNode<string> *root);//求树的高度
108     void printBlank(int n);//工具方法,打印n个空格
109     void printInRightFormat(string value);//按照格式打印,以便对齐
110     void recursionPrintPreffixE(BinaryTreeNode<string> *root);//递归调用打印前缀表达式
111     void recursionPrintSuffixE(BinaryTreeNode<string> *root);//递归调用打印后缀表达式
112     bool shouldPrintLeftBracket(const stack<BinaryTreeNode<string>*> &nodeStack ,BinaryTreeNode<string> *pointer,int leftOrRight);//用于输出中缀表达式时判断是否需要输出左括号
113     void recursionDeleteAll(BinaryTreeNode<string> *root);
114 public:
115     ExpressionBinaryTree(){root = NULL;};
116     ~ExpressionBinaryTree(){clear();};
117     void buildBTreeByPreffixE();//以前缀表达式构建二叉树
118     void buildBTreeByInfixE();//以中缀表达式构建二叉树
119     void buildBTreeBySuffixE();//以后缀表达式构建二叉树
120     void printEBTree();//打印二叉树字符图
121     void printPreffixE(){recursionPrintPreffixE(root); cout << endl;}//将递归调用封装,只留给用户打印接口
122     void printSuffixE(){recursionPrintSuffixE(root); cout << endl;}//将递归调用封装,只留给用户打印接口
123     
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值