Question: PLEASE CONVERT THE FOLLOWING C++ CODE to JAVA : #include #include #include using namespace std; bool parseInputs(string, map>&, map&, char&); void lexiSExpressionPrint(map>, char); int main()

PLEASE CONVERT THE FOLLOWING C++ CODE to JAVA:

#include #include #include using namespace std; bool parseInputs(string, map>&, map&, char&); void lexiSExpressionPrint(map>, char); int main() { string input; getline(cin, input); char root; map> adjList; map numParents; if (!parseInputs(input, adjList, numParents, root)) { return 0; } lexiSExpressionPrint(adjList, root); return 0; } bool parseInputs(string input, map>& adjList, map& numParents, char& root) { char parent; int index = 0; bool E5Error = false; for (int i = 0; i < input.length(); i++) { if (input[i] != '(' && input[i] != ')' && input[i] != ',' && input[i] != ' ') { index++; if (index % 2 == 1) { if (i - 1 >= 0 && input[i-1] != '(') { cout << "E1 "; return false; } else if (i + 1 < input.length() && input[i+1] != ',') { cout << "E1 "; return false; } parent = input[i]; } else { if (i - 1 >= 0 && input[i-1] != ',') { cout << "E1 "; return false; } else if (i + 1 < input.length() && input[i+1] != ')') { cout << "E1 "; return false; } for (int j = 0; j < adjList[parent].size(); j++) { if (adjList[parent][j] == input[i]) { cout << "E2 "; return false; } } if (adjList[parent].size() == 2) { cout << "E3 "; return false; } numParents[input[i]]++; numParents[parent]; if (numParents[input[i]] == 2) { E5Error = true; } adjList[parent].push_back(input[i]); index = 0; } } else if (i - 1 <= 0 && input[i] == ' ') { if (input[i] == ' ') { cout << "E1 "; return false; } } } int numRoots = 0; for (auto kv : numParents) { if (kv.second == 0) { root = kv.first; numRoots++; if (numRoots == 2) { cout << "E4 "; return false; } } } if (E5Error == true) { cout << "E5 "; return false; } return true; } void lexiSExpressionPrint(map> adjList, char current) { cout << "(" << current; int numChild = adjList[current].size(); if (numChild == 1) { lexiSExpressionPrint(adjList, adjList[current][0]); } else if (numChild == 2) { if (adjList[current][0] < adjList[current][1]) { lexiSExpressionPrint(adjList, adjList[current][0]); lexiSExpressionPrint(adjList, adjList[current][1]); } else { lexiSExpressionPrint(adjList, adjList[current][1]); lexiSExpressionPrint(adjList, adjList[current][0]); } } cout << ")"; }

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!