151 lines
5.4 KiB
Java
151 lines
5.4 KiB
Java
/**
|
|
* Created by azael on 20/03/17.
|
|
*/
|
|
public class Tree {
|
|
|
|
private Node root;
|
|
private String exp;
|
|
|
|
Tree (String exp){
|
|
this.exp = exp;
|
|
root = tree(0);
|
|
display();
|
|
findPatern(root);
|
|
}
|
|
|
|
private Node tree(int pos) {
|
|
char item = exp.charAt(pos);
|
|
|
|
if(Expression.isOperator(item)) {
|
|
if (item == '!'){
|
|
Node fils = tree(pos+1);
|
|
Node f = new Node(null, fils, null, item, 1+fils.getLvl());
|
|
fils.setPos(1);
|
|
fils.setFather(f);
|
|
return f;
|
|
} else {
|
|
Node filsG = tree(pos+1);
|
|
Node filsD = tree(pos+filsG.getLvl()+1);
|
|
Node f = new Node(null, filsG, filsD, item, filsG.getLvl() + filsD.getLvl()+1);
|
|
filsG.setPos(1);
|
|
filsD.setPos(2);
|
|
filsG.setFather(f);
|
|
filsD.setFather(f);
|
|
return f;
|
|
}
|
|
} else {
|
|
return new Node(null, null, null, item, 1);
|
|
}
|
|
}
|
|
|
|
public void display () {
|
|
displayNode(root,0);
|
|
}
|
|
|
|
private void displayNode(Node n, int lvl) {
|
|
for (int i = 0; i < lvl; i++)
|
|
System.out.print("-");
|
|
|
|
n.display();
|
|
if (n.getLeftChild() != null)
|
|
displayNode(n.getLeftChild(),lvl+1);
|
|
if (n.getRigthChild() != null)
|
|
displayNode(n.getRigthChild(),lvl+1);
|
|
}
|
|
|
|
private void findPatern (Node node) {
|
|
if (replacePatern(node)){
|
|
if (node.getFather() == null)
|
|
findPatern(root);
|
|
else
|
|
findPatern(node.getFather());
|
|
} else {
|
|
if( node.getLeftChild() != null)
|
|
findPatern(node.getLeftChild());
|
|
if( node.getRigthChild() != null)
|
|
findPatern(node.getRigthChild());
|
|
}
|
|
}
|
|
|
|
private boolean replacePatern (Node father) {
|
|
Node leftChild = father.getLeftChild();
|
|
Node rightChild = father.getRigthChild();
|
|
switch (father.getNode()) {
|
|
case '!' :
|
|
Node child = leftChild.getLeftChild();
|
|
switch (leftChild.getNode()){
|
|
case '!':
|
|
|
|
appliedGreatFather(father, child);
|
|
break;
|
|
case '|' :
|
|
Node lc = child.getLeftChild();
|
|
Node rc = child.getRigthChild();
|
|
|
|
Node notlc = new Node(null,lc,null,'!', lc.getLvl() +1);
|
|
Node notrc = new Node(null,rc,null,'!', rc.getLvl() +1);
|
|
lc.setFather(notlc);
|
|
rc.setFather(notrc);
|
|
|
|
Node greatfather = new Node(father.getFather(),notlc,notrc,'&',notlc.getLvl() + notrc.getLvl() +1);
|
|
notlc.setFather(greatfather);
|
|
notrc.setFather(greatfather);
|
|
appliedGreatFather(father, greatfather);
|
|
break;
|
|
case '&' :
|
|
Node lc_2 = child.getLeftChild();
|
|
Node rc_2 = child.getRigthChild();
|
|
|
|
Node notlc_2 = new Node(null,lc_2,null,'!', lc_2.getLvl() +1);
|
|
Node notrc_2 = new Node(null,rc_2,null,'!', rc_2.getLvl() +1);
|
|
lc_2.setFather(notlc_2);
|
|
rc_2.setFather(notrc_2);
|
|
|
|
Node greatfather_2 = new Node(father.getFather(),notlc_2,notrc_2,'|',notlc_2.getLvl() + notrc_2.getLvl() +1);
|
|
notlc_2.setFather(greatfather_2);
|
|
notrc_2.setFather(greatfather_2);
|
|
appliedGreatFather(father, greatfather_2);
|
|
break;
|
|
case '>' :
|
|
Node lc_3 = child.getLeftChild();
|
|
Node rc_3 = child.getRigthChild();
|
|
|
|
Node notrc_3 = new Node(null,rc_3,null,'!', rc_3.getLvl() +1);
|
|
rc_3.setFather(notrc_3);
|
|
|
|
Node greatfather_3 = new Node(father.getFather(),lc_3,notrc_3,'&',lc_3.getLvl() + notrc_3.getLvl() +1);
|
|
lc_3.setFather(greatfather_3);
|
|
notrc_3.setFather(greatfather_3);
|
|
|
|
appliedGreatFather(father, greatfather_3);
|
|
break;
|
|
default:
|
|
return false;
|
|
}
|
|
break;
|
|
case '>' :
|
|
Node notlc = new Node(null,leftChild,null,'!', leftChild.getLvl() +1);
|
|
leftChild.setFather(notlc);
|
|
|
|
Node greatfather = new Node(father.getFather(),notlc,rightChild,'|',notlc.getLvl() + rightChild.getLvl() +1);
|
|
notlc.setFather(greatfather);
|
|
rightChild.setFather(greatfather);
|
|
appliedGreatFather(father, greatfather);
|
|
break;
|
|
default:
|
|
return false;
|
|
}
|
|
return true;
|
|
}
|
|
|
|
private void appliedGreatFather (Node father, Node greatFather) {
|
|
if (father.getPos() == 1) {
|
|
father.getFather().setLeftChild(greatFather);
|
|
greatFather.setFather(father);
|
|
} else if (father.getPos() == 2) {
|
|
father.getFather().setRigthChild(greatFather);
|
|
greatFather.setFather(father);
|
|
} else
|
|
root = greatFather;
|
|
}
|
|
} |