簡體   English   中英

堆棧中的樹木

[英]Trees in a Stack

我的程序應該接收一個方程式(例如:x ^ 4 *(x + 3))並將其轉換為后置順序(或逆波蘭表示法),之后,我需要創建一棵需要放入堆棧的樹。 棘手的部分是閱讀后訂單方程。 在示例中,應為:

x 4 ^ x 3 + *

因此,有關造樹的規則是:

如果它是二進制運算(“ +”,“-”,“ ^”,“ /”,“ *”),則應采用堆棧的前兩個元素,創建一個以該運算為根的樹,以及數字作為其兒子,並將其推入堆棧。

如果它是一元運算符(“&”代表ln,“〜”代表負數(〜3)=(-3)),則應采用堆棧的第一個元素,並以該操作為根創建一棵樹,和數字作為它的兒子,然后將其推入堆棧。

如果是數字或字母,則應創建一個沒有子節點的節點,然后將其壓入堆棧。

我通過字符串檢測它是字母,二進制還是一元運算的算法是:(郵政命令方程式已經創建,它是由我的老師發送的,因此在此處無需編輯)

String aux="";
for (int i=0; i < nuevaF.length();i++){
    char c = nuevaF.charAt(i);
    if (c!=' '){
        aux=aux+c;  
        System.out.println(aux);
    }
    if (c==' '){
        System.out.println("space");
        Transformar(stack,aux);
        aux="";
    }
 }

然后創建堆棧:

public static void Transformar(PilaArreglo stack, String ecuacion){

        if (ecuacion=="+"||ecuacion=="-"||ecuacion=="*"||ecuacion=="/"||ecuacion=="^"){
            Nodo aux1 = stack.desapilar();
            Nodo aux2 = stack.desapilar();
            Nodo total = new Nodo(ecuacion,aux2, aux1);
            System.out.println("hole");
            stack.apilar(total);

        }
        else if (ecuacion=="&"||ecuacion=="~"){
            Nodo aux1 =stack.desapilar();
            Nodo total2 = new Nodo(ecuacion,aux1);
            System.out.println("holo");
            stack.apilar(total2);
        }
        else{
            Nodo total3 = new Nodo(ecuacion);
            System.out.print("hele");
            stack.apilar(total3);
        }


}

我的問題是,它沒有檢測到它是否是二進制操作。 它立即轉到其他。 我打印了孔,全息圖和底紋,以查看元素的去向,但是我得到的只是底紋。

 x
 hele4
 hele^
 helex
 hele3
 hele+
 hele*

我真的不知道為什么它是二進制運算或一元運算而跳過其他If。 以防萬一,這是Tree類

public class Nodo{
  Object element;
  Nodo izq;
  Nodo der;
  Nodo(String x, Nodo y, Nodo z){
     element = x;
     izq = y;
     der = z;
  }
  Nodo(String x, Nodo y){
     element = x;
     izq = y;
     der = null;
  }
  Nodo(String x){
     element = x;
     izq = null;
     der = null;
  }
}

和堆棧(假定是節點的堆棧)

  class PilaArreglo{
       private Nodo[] arreglo;
       private int tope;
       private int MAX_ELEM=100; // max numbers on stack

       public PilaArreglo(){
          arreglo=new Nodo[MAX_ELEM];
           tope=-1; // empty stack
       }

       public void apilar(Nodo x){
            if (tope+1<MAX_ELEM){ // if full, OVERFLOW
               tope++;
               arreglo[tope]=x;
            }
            else{
               MAX_ELEM=MAX_ELEM*2;
               Nodo[] nuevo_arreglo=new Nodo[MAX_ELEM];
               for (int i=0; i<arreglo.length; i++){
                    nuevo_arreglo[i]=arreglo[i];
               }
            tope++;
            nuevo_arreglo[tope]=x;
            arreglo=nuevo_arreglo;
            }
       }
       public Nodo desapilar(){
             if (!estaVacia()){ // si esta vacia se produce UNDERFLOW
               Nodo x=arreglo[tope];
               tope--;
               return x;
              }
              return null;
       }
       public Nodo tope(){
           if (!estaVacia()){ // si esta vacia es un error
              Nodo x=arreglo[tope];
              return x;
           }
           return null;
       }

       public boolean estaVacia(){
           if (tope==-1)
       {
              return true;
       }
           else
       {
              return false;
       }
 }

我會很感激您能給我的一切幫助。

您正在使用==來比較在檢測運算符時應使用.equals字符串。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM