美文网首页
数据结构-二叉树和堆栈

数据结构-二叉树和堆栈

作者: Fattyu | 来源:发表于2017-12-05 00:54 被阅读0次

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

#include <iostream>
#include <stack>
using namespace std;
void d(stack<int>&m)
{
    cout<<"fuck";
    cout<<m.top();
    m.pop();
}
int main ()
{
    stack<int> mystack;
    int sum (0);

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

    d(mystack);
    cout<<"hh";
    cout<<mystack.top();
    return 0;
}
void Vaculcate(Tree T,stack<char>&smybol,stack<float>&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"<<number.top()<<endl;
        }
        else
        {
            smybol.push(p->data);
            cout<<"char"<<smybol.top()<<endl;


        }
}
void Value(Tree T,stack<char>&smybol,stack<float>&number)
{
    Vaculcate(T,smybol,number);
    stack<float>answer;
    while(!number.empty()) {
        cout<<"no empty";
        float m, n;
        m = number.top();
        cout<<"m:"<<m<<endl;
        number.pop();
        n = number.top();
        cout<<"n:"<<n<<endl;
        number.pop();
        char x = smybol.top();
        cout<<"symbol:"<<x<<endl;
        smybol.pop();
        float term;
        switch (x) {
            case '+':
                term = m + n;
                cout << "answer:"<<term << endl;
                answer.push(term);
                break;
            case '-':
                term = n-m;
                cout << "answer:"<<term << endl;
                answer.push(term);
                break;
            case '*':
                term = m * n;
                cout <<"answer:"<< term << endl;
                answer.push(term);
                break;
            case '/':
                term = n/m;
                cout << "answer:"<<term << endl;
                answer.push(term);
                break;
            default:
                cout << "error";
        }
    }
}

3.计算表达式的值

#include <stdio.h>
#include <stack>
#include <iostream>
using namespace std;
stack<float>number;
stack<char>symbol;
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<<endl;
    cout<<"最终运算:"<<endl;
        symbol.pop();
        char d = symbol.top();
        float a, b;
        a = number.top();
        b = number.top();
        cout<<a<<" "<<b<<endl;
        float term;
        switch (d) {
            case '+':
                term = a + b;
                number.push(term);
                cout <<"answer:"<< term << endl;
                break;
            case '-':
                term = a - b;
                number.push(term);
                cout << "answer:"<<term << endl;
                break;
            case '*':
                term = a * b;
                number.push(term);
                cout <<"answer:"<< term << endl;
                break;
            case '/':
                term = a / b;
                number.push(term);
                cout <<"answer:"<< term << endl;
                break;
            default:
                break;
        }
   return 0;
}

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

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

#ifndef UNTITLED11_TREE_H
#define UNTITLED11_TREE_H

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <iostream>
#include <stack>
#include <math.h>
#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<<T->data;
    Expression(T->rchild);
}
//表达式
void Priexpression(Tree T)
{
    if(!T)
        return;
    else {
        TreeNode *p;
        p = T;
        cout << "完整表达式:"<<endl;
        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;
        }
        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"
stack<char>symbol;
stack<float>number;
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<<"表达式:"<<endl;
    Expression(tree);
    cout<<"\n";
    Priexpression(tree);
    return 0;
}

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

相关文章

  • 通俗易懂:C语言中内存堆和栈的区别

    数据结构的栈和堆 首先在数据结构上要知道堆栈,尽管我们这么称呼它,但实际上堆栈是两种数据结构:堆和栈。 堆和栈都是...

  • Go 堆栈的理解

    在讲Go的堆栈之前,先温习一下堆栈基础知识。 什么是堆栈?在计算机中堆栈的概念分为:数据结构的堆栈和内存分配中堆栈...

  • 在Python中实现两个堆栈的队列

    在Python中实现两个堆栈的队列。数据结构了解堆栈和队列。然后用两个堆栈实现一个队列。堆栈和队列都是列表。但它们...

  • 数据结构-二叉树和堆栈

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

  • Swift 算法俱乐部:堆栈

    通过本教程,你将学习怎样用swift实现堆栈数据结构。作为基础数据结构,堆栈能解决很多程序中的问题。 开始吧 堆栈...

  • 关于函数递归和迭代的转化, 及尾递归相关知识的接触和思考

    javascript实现数据结构: 树和二叉树,二叉树的遍历和基本操作 js 二叉树 【数据结构与算法】深入浅出递...

  • 内存中的堆栈和数据结构堆栈区别

    内存中的堆栈和数据结构堆栈不是一个概念,可以说内存中的堆栈是真实存在的物理区,数据结构中的堆栈是抽象的数据存储结构...

  • Java内存中的数据存储结构

    内存中的堆栈和数据结构堆栈不是一个概念,可以说内存中的堆栈是真实存在的物理区,数据结构中的堆栈是抽象的数据存储结构...

  • 数据结构导读目录

    数据结构(1)-常见数据结构数据结构(2)-栈和队列和Hash表数据结构(3)-树和二叉树的遍历数据结构(4)-二...

  • 数据结构和算法(三) - 栈

    堆栈数据结构在概念上与物理的堆栈相同。将元素添加到堆栈时,将其放在堆栈顶部。从堆栈中删除元素时,始终会删除最顶层的...

网友评论

      本文标题:数据结构-二叉树和堆栈

      本文链接:https://www.haomeiwen.com/subject/gtafixtx.html