Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.tree;
- import java.util.HashSet;
- import javax.swing.tree.DefaultMutableTreeNode;
- /*
- * To change this template, choose Tools | Templates
- * and open the template in the editor.
- */
- /**
- *
- * @author adrian
- */
- public class Tree {
- final String body;
- final char[] hierarchy = {'=', '+', '-', '*', '/', '^'};
- public Tree(String body) {
- this.body = body;
- }
- public DefaultMutableTreeNode makeTree() {
- DefaultMutableTreeNode father = new DefaultMutableTreeNode(this.body);
- divideTree(father);
- return father;
- }
- public void divideTree(DefaultMutableTreeNode father) {
- DefaultMutableTreeNode leftSon, rightSon;
- String fatherBody, leftSonBody, rightSonBody;
- int position;
- fatherBody = father.toString();
- position = calculatePosition(fatherBody);
- leftSonBody = fatherBody.substring(0, position);
- rightSonBody = fatherBody.substring(position+1);
- if (haveBorders(leftSonBody)) {
- leftSonBody = removeBorders(leftSonBody);
- }
- if (haveBorders(rightSonBody)) {
- rightSonBody = removeBorders(rightSonBody);
- }
- leftSon = new DefaultMutableTreeNode(leftSonBody);
- rightSon = new DefaultMutableTreeNode(rightSonBody);
- father.add(leftSon);
- father.add(rightSon);
- if (haveSigns(leftSon.toString())) {
- divideTree(leftSon);
- }
- if (haveSigns(rightSon.toString())) {
- divideTree(rightSon);
- }
- }
- public boolean haveSigns(String body) {
- for (int i = 0; i < hierarchy.length; i++) {
- if (body.indexOf(hierarchy[i]) != -1) {
- return true;
- }
- }
- return false;
- }
- public boolean haveBorders(String body) {
- if (calculateInvalidPositions(body).size() == body.length()) {
- return true;
- }
- return false;
- }
- public int calculatePosition(String body) {
- HashSet<Integer> invalidPositions = calculateInvalidPositions(body);
- int position = 0;
- for (int i = 0; i < hierarchy.length; i++) {
- for (int j = 0; j < body.length(); j++) {
- if (body.charAt(j) == hierarchy[i]) {
- position = j;
- }
- if (invalidPositions.contains(position)==false && position!=0) {
- return position;
- }
- }
- }
- return position;
- }
- public HashSet calculateInvalidPositions(String body) {
- HashSet<Integer> invalids = new HashSet<Integer>();
- char[] openHierarchy = {'{', '[', '('};
- char[] closeHierarchy = {'}', ']', ')'};
- boolean activation = false;
- for (int i = 0; i < openHierarchy.length; i++, activation = false) {
- for (int j = 0; j < body.length(); j++) {
- if (body.charAt(j) == openHierarchy[i]) {
- activation = true;
- }
- if (activation) {
- invalids.add(j);
- }
- if (body.charAt(j) == closeHierarchy[i]) {
- activation = false;
- }
- }
- }
- return invalids;
- }
- public String removeBorders(String body) {
- int f = (body.length() - 1);
- if ((body.charAt(0) == '{') && (body.charAt(f) == '}')) {
- body = body.substring(1, (f));
- return body;
- }
- if ((body.charAt(0) == '[') && (body.charAt(f) == ']')) {
- body = body.substring(1, (f));
- return body;
- }
- if ((body.charAt(0) == '(') && (body.charAt(f) == ')')) {
- body = body.substring(1, (f));
- return body;
- }
- return body;
- }
- }
Add Comment
Please, Sign In to add comment