数据结构-二叉树和堆栈

1.前期实验
失败原因:
(1)对于先序输入的式子理解不到位,导致在压栈错误
(2)&的用法????

#include 
#include 
using namespace std;
void d(stack&m)
{
    cout<<"fuck";
    cout< mystack;
    int sum (0);

    for (int i=1;i<=3;i++) mystack.push(i);

    d(mystack);
    cout<<"hh";
    cout<
void Vaculcate(Tree T,stack&smybol,stack&number)
{
    if(!T)
        return;
    Vaculcate(T->lchild,smybol,number);
    Vaculcate(T->rchild,smybol,number);
    TreeNode *p=T;
        if(p->data>='0'&&p->data<='9')
        {
            float m=(float)(p->data-'0');
            number.push(m);
            cout<<"number"<data);
            cout<<"char"<&smybol,stack&number)
{
    Vaculcate(T,smybol,number);
    stackanswer;
    while(!number.empty()) {
        cout<<"no empty";
        float m, n;
        m = number.top();
        cout<<"m:"<

3.计算表达式的值

#include 
#include 
#include 
using namespace std;
stacknumber;
stacksymbol;
int main() {
    char x[20];
    cin>>x;
    int i=0;
    int count=0;
    while(x[i-1]!='#') {
        if(count < 2) {
            if (x[i] >= '0' && x[i] <= '9') {
                count++;
                float m = (float) (x[i] - '0');
                number.push(m);
                cout << "number:" << m << endl;
            } else {
                symbol.push(x[i]);
                cout << "symbol:" << x[i] << endl;
            }
            i++;
        }
           else {
            count = 0;
            float term, a, b;
            a = number.top();
            number.pop();
            b = number.top();
            number.pop();
            char p;
            p = symbol.top();
            symbol.pop();
            cout << a << " " << b << " " << p << endl;
            switch (p) {
                case '+':
                    term = a + b;
                    number.push(term);
                    cout << term << endl;
                    break;
                case '-':
                    term = b -a;
                    number.push(term);
                    cout << term << endl;
                    break;
                case '*':
                    term = a * b;
                    number.push(term);
                    cout << term << endl;
                    break;
                case '/':
                    term = a / b;
                    number.push(term);
                    cout << term << endl;
                    break;
                default:
                    break;
            }
        }

    }
    cout<

4.中序添加括号输出
(1)函数头文件以及自定义函数定义以及实现

//
// Created by fattytian on 17-11-22.
//

#ifndef UNTITLED11_TREE_H
#define UNTITLED11_TREE_H

#include 
#include 
#include 
#include 
#include 
#include 
#define MAX 100
using namespace std;
typedef char TElmeType;
typedef struct TreeNode
{
    TElmeType data;//数据存储
    struct TreeNode *lchild,*rchild,*parent;//左右孩子,父节点
}TreeNode,*Tree;
//创建
void Inittree(Tree *T)
{
    *T=NULL;
}
//销毁
void Detree(Tree *T)
{
    if(*T) {
        if((*T)->lchild)
            Detree(&(*T)->lchild);
        if((*T)->rchild)
            Detree(&(*T)->rchild);
        free(*T);
        *T=NULL;
    }
}
//创建一个表达式的树
void Build(Tree &T,TElmeType x) {
    TreeNode *newnode = new TreeNode;
    newnode->data = x;
    newnode->rchild = NULL;
    newnode->lchild = NULL;
    TreeNode *p;
    if (T == NULL) {
        T = newnode;
        newnode->parent = T;
        p = newnode;
    } else {
        if (x >= '0' && x <= '9') {
            if (p->lchild == NULL) {
                p->lchild = newnode;
                newnode->parent = p;
            } else {
                p->rchild = newnode;
                newnode->parent = p;
                p = p->parent;
            }
        } else {
            if (p->lchild != NULL) {
                p->rchild = newnode;
                newnode->parent = p;
                p = newnode;
            } else {
                p->lchild = newnode;
                newnode->parent = p;
                p = newnode;
            }

        }
    }
}
//前缀输出
void Expression(Tree T)
{
   // cout<<"中序输出:";
    if(!T)
        return;
    Expression(T->lchild);
    cout<data;
    Expression(T->rchild);
}
//表达式
void Priexpression(Tree T)
{
    if(!T)
        return;
    else {
        TreeNode *p;
        p = T;
        cout << "完整表达式:"<lchild) {
            p = p->lchild;
        }

        while (p != T) {
            cout << "(";
            cout << p->data;
            p = p->parent;
            cout << p->data;
            cout << p->rchild->data;
            cout << ")";
            p = p->parent;
        }
        cout << p->data;
        p = p->rchild;
        while (p->lchild)
            p = p->lchild;
        while (p != T) {
            cout << "(";
            cout << p->data;
            p = p->parent;
            cout << p->data;
            cout << p->rchild->data;
            cout << ")";
            p = p->parent;
        }
    }
}

#endif //UNTITLED11_TREE_H

(2)main 函数

//
// Created by fattytian on 17-11-28.
//

#include "Tree.h"
stacksymbol;
stacknumber;
int main()
{
    cout<<"please write your expression:";
    Tree tree;
    Inittree(&tree);
    TElmeType x[20];
    cin>>x;
    int i=0;
    int count=0;
    while(x[i]!='#')
    {
        Build(tree,x[i]);
        i++;
    }
    cout<<"表达式:"<

5.晚安,我会想到好的方法简化代码的。

你可能感兴趣的:(数据结构-二叉树和堆栈)