Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- private static Node root;
- public static void makeTree(String input) {
- Node newNode = new Node();
- newNode.data = input.charAt(0);
- for (int i = 0; i < input.length(); i++) {
- if (root == null && (input.charAt(i) == '*' || input.charAt(i) == '/' || input.charAt(i) == '%')) {
- root.data = input.charAt(i);
- root.leftChild.data = input.charAt(i - 2);
- root.rightChild.data = input.charAt(i - 1);
- removeCharAt(input, i);
- removeCharAt(input, i - 1);
- removeCharAt(input, i - 1);
- }
- if (input.charAt(i) == '*' || input.charAt(i) == '/' || input.charAt(i) == '%') {
- root.rightChild = root;
- root.data = input.charAt(i);
- root.leftChild.data = input.charAt(i - 1);
- removeCharAt(input, i);
- removeCharAt(input, i - 1);
- }
- if (input.charAt(i) == '+' || input.charAt(i) == '-') {
- root.rightChild.data = root.data;
- root.data = input.charAt(i);
- root.leftChild.data = input.charAt(0);
- removeCharAt(input, i);
- removeCharAt(input, 0);
- }
- }
- }
- public class Node {
- int data;
- Node leftChild;
- Node rightChild;
- }
Add Comment
Please, Sign In to add comment