Datasets:

ArXiv:
License:
File size: 7,339 Bytes
c574d3a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
package torea;

public class RevPolToInfNot{

    public static void main(String[] args)
    {
        Kattio io = new Kattio(System.in, System.out);
        List.ArrayList<String> tokens = new List.ArrayList<String>();
        while(io.hasMoreTokens()){
            tokens.add(io.getWord());
        }

        String output = getInfixReverseGraph(tokens);
        io.println(output);
        io.flush();
        io.close();
    }

    public static String getInfixNotation(String query)
    {
        Stack.ListStack<String> stringStack = new Stack.ListStack<String>();

        for(String token: query.split(" ")){
            if(token.equals("+")||token.equals("-")||token.equals("*")||token.equals("/"))
            {
                String frst = stringStack.pop();
                String scnd = stringStack.pop();
                stringStack.push("("+scnd + token + frst+")");
            } else {
                stringStack.push(token);
            }
        }
        return stringStack.pop();
    }

    public static String getInfixNotationSB(String query)
    {
        Stack.ListStack<StringBuilder> stringStack = new Stack.ListStack<StringBuilder>();

        for(String token: query.split(" ")){
            if(token.equals("+")||token.equals("-")||token.equals("*")||token.equals("/"))
            {
                StringBuilder frst = stringStack.pop();
                StringBuilder scnd = stringStack.pop();
                stringStack.push(scnd.insert(0, "(").append(token).append(frst).append(")"));
            } else {
                stringStack.push(new StringBuilder(token));
            }
        }
        return stringStack.pop().toString();
    }

    public static boolean isOperator(String token){
        return token.equals("+")||token.equals("-")||token.equals("*")||token.equals("/");
    }

    public static boolean isOperator(Character token){
        return token.equals('+')||token.equals('-')||token.equals('*')||token.equals('/');
    }

    public static String getInfixReverseEvaluation(List.ArrayList<String> tokens)
    {

        // Idea use one stack.
        // Add to it.
        // When two values have been added do operation.
        // After doing operation use peek to check if two values are adjacent.
        // if so repeat.

        Stack.ListStack<String> combStack = new Stack.ListStack<String>();

        for( int i = tokens.size()-1; i>=0; i--){
            String token = tokens.get(i);
            if(isOperator(token))
            {
                combStack.push(token);
            } else {
                if(isOperator(combStack.peek())){
                    combStack.push(token);
                } else {
                    while(!combStack.isEmpty() && !isOperator(combStack.peek()))
                    {
                        String frst = combStack.pop();
                        String op = combStack.pop();
                        token = "("+token + op + frst+")";
                    }
                    combStack.push(token);
                }
            }
        }
        return combStack.pop();
    }

    public static String getInfixReverseEvaluationRecursive(List.ArrayList<String> tokens)
    {

        return getExpression(tokens, tokens.size()-1).expression.toString();
    }

    public static class ExpressionHolder{
        public StringBuilder expression;
        public int startIndex;
        ExpressionHolder(StringBuilder myExpression, int myStartIndex){
            expression = myExpression;
            startIndex = myStartIndex;
        }
    }

    public static ExpressionHolder getExpression(List.ArrayList<String> tokens, int end)
    {
        StringBuilder exp = new StringBuilder("(");
        String op = tokens.get(end);
        StringBuilder frst;
        int frstStart;
        StringBuilder scnd;
        int scndStart;
        if(!isOperator(tokens.get(end-1))){
            frst = new StringBuilder(tokens.get(end-1));
            frstStart = end-1;
        }
        else{
            ExpressionHolder firstArgumentEH = getExpression(tokens, end-1);
            frstStart = firstArgumentEH.startIndex;
            frst = firstArgumentEH.expression;
        }
        if(!isOperator(tokens.get(frstStart - 1))){
            scnd = new StringBuilder(tokens.get(frstStart - 1));
            scndStart = frstStart - 1;
        }
        else{
            ExpressionHolder secondArgumentEH = getExpression(tokens, frstStart - 1);
            scndStart = secondArgumentEH.startIndex;
            scnd = secondArgumentEH.expression;
        }

        return new ExpressionHolder(exp.append(scnd).append(op).append(frst).append(")"), scndStart);
    }

    public static class Node{
        public int tokenId;
        public int startIndex;
        public Node rightChild;
        public Node leftChild;

        Node(int id){
            tokenId = id;
        }
    }

    public static Node getNodes(List.ArrayList<String> tokens, int end)
    {
        Node opNode = new Node(end);
        
        if(!isOperator(tokens.get(end-1))){
            opNode.rightChild = new Node(end-1);
            opNode.rightChild.startIndex = end-1;
        }
        else{
            opNode.rightChild = getNodes(tokens, end-1);
        }

        if(!isOperator(tokens.get(opNode.rightChild.startIndex - 1))){
            opNode.leftChild = new Node(opNode.rightChild.startIndex - 1);
            opNode.leftChild.startIndex = opNode.rightChild.startIndex - 1;
        }
        else{
            opNode.leftChild = getNodes(tokens, opNode.rightChild.startIndex - 1);
        }

        opNode.startIndex = opNode.leftChild.startIndex;

        return opNode;
    }

    public static void traverseGraph(StringBuilder sb, Node node, List.ArrayList<String> tokens)
    {

        if(!isOperator(tokens.get(node.tokenId)))
        {
            sb.append(tokens.get(node.tokenId));
        }
        else
        {
            sb.append("(");
            traverseGraph(sb, node.leftChild, tokens);
            sb.append(tokens.get(node.tokenId));
            traverseGraph(sb, node.rightChild, tokens);
            sb.append(")");
        }
        // return sb;
    }

    public static void printGraph(Kattio io, Node node, List.ArrayList<String> tokens)
    {

        if(!isOperator(tokens.get(node.tokenId)))
        {
            io.print(tokens.get(node.tokenId));
        }
        else
        {
            io.print("(");
            printGraph(io, node.leftChild, tokens);
            io.print(tokens.get(node.tokenId));
            printGraph(io, node.rightChild, tokens);
            io.print(")");
        }
    }

    public static void printInfixReverseGraph(Kattio io,List.ArrayList<String> tokens)
    {
        Node rootNode = getNodes( tokens, tokens.size()-1);
        printGraph(io, rootNode, tokens);

    }

    public static String getInfixReverseGraph(List.ArrayList<String> tokens)
    {
        Node rootNode = getNodes( tokens, tokens.size()-1);
        StringBuilder sb = new StringBuilder();
        traverseGraph(sb, rootNode, tokens);
        return sb.toString();
    }
}