Java Stack Usage getPostOrder(List inOrderList)

Here you can find the source of getPostOrder(List inOrderList)

Description

get Post Order

License

Apache License

Declaration

private static List<String> getPostOrder(List<String> inOrderList) 

Method Source Code


//package com.java2s;
/*/*from   w  w w. ja v a  2  s . c o m*/
 * Copyright (C) 2017 Baifendian Corporation
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *          http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

import java.util.ArrayList;
import java.util.List;
import java.util.Stack;

public class Main {

    private static List<String> getPostOrder(List<String> inOrderList) {
        List<String> result = new ArrayList<>();
        Stack<String> stack = new Stack<>();

        for (int i = 0; i < inOrderList.size(); i++) {
            if (Character.isDigit(inOrderList.get(i).charAt(0))) {
                result.add(inOrderList.get(i));
            } else {
                switch (inOrderList.get(i).charAt(0)) {
                case '(':
                    stack.push(inOrderList.get(i));
                    break;
                case ')':
                    while (!stack.peek().equals("(")) {
                        result.add(stack.pop());
                    }
                    stack.pop();
                    break;
                default:
                    while (!stack.isEmpty() && compare(stack.peek(), inOrderList.get(i))) {
                        result.add(stack.pop());
                    }
                    stack.push(inOrderList.get(i));
                    break;
                }
            }
        }

        while (!stack.isEmpty()) {
            result.add(stack.pop());
        }

        return result;
    }

    private static boolean compare(String peek, String cur) {
        if ("*".equals(peek) && ("/".equals(cur) || "*".equals(cur) || "+".equals(cur) || "-".equals(cur))) {
            return true;
        } else if ("/".equals(peek) && ("/".equals(cur) || "*".equals(cur) || "+".equals(cur) || "-".equals(cur))) {
            return true;
        } else if ("+".equals(peek) && ("+".equals(cur) || "-".equals(cur))) {
            return true;
        } else if ("-".equals(peek) && ("+".equals(cur) || "-".equals(cur))) {
            return true;
        }

        return false;
    }
}

Related

  1. endsWithIgnoreWhiteSpace(String decl, String token)
  2. getBinary(long input)
  3. getOperands(Stack stack, int nOperands)
  4. getPermutationsRec(List permutations, byte[] order, List> remaining, int index)
  5. getRelativePath(Stack pathStack)
  6. getSyllables(String pinyin)
  7. isDoubleQuote(Stack bufStack)
  8. normalizeAbsolutePath(String curDir)