implementation patern

This commit is contained in:
Azael
2017-03-28 14:25:50 +02:00
parent 4e5983b5ce
commit 74fc4cf821
3 changed files with 122 additions and 3 deletions

View File

@@ -9,6 +9,8 @@ public class Tree {
Tree (String exp){
this.exp = exp;
root = tree(0);
display();
findPatern(root);
}
private Node tree(int pos) {
@@ -18,12 +20,15 @@ public class Tree {
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;
@@ -37,7 +42,7 @@ public class Tree {
displayNode(root,0);
}
private void displayNode(Node n, int lvl){
private void displayNode(Node n, int lvl) {
for (int i = 0; i < lvl; i++)
System.out.print("-");
@@ -47,4 +52,100 @@ public class Tree {
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;
}
}