学数据结构时的上机作业,花了半天时间写的。
问题描述:
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