implementation patern
This commit is contained in:
@@ -121,7 +121,7 @@ public class Expression {
|
|||||||
e.negExpressionPrefixe();
|
e.negExpressionPrefixe();
|
||||||
System.out.println(e.getExpressionPrefixe());
|
System.out.println(e.getExpressionPrefixe());
|
||||||
System.out.println();
|
System.out.println();
|
||||||
|
|
||||||
Tree t = new Tree(e.getExpressionPrefixe());
|
Tree t = new Tree(e.getExpressionPrefixe());
|
||||||
t.display();
|
t.display();
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -10,6 +10,8 @@ public class Node {
|
|||||||
private Node rigthChild;
|
private Node rigthChild;
|
||||||
private int lvl;
|
private int lvl;
|
||||||
|
|
||||||
|
private int pos = 0;
|
||||||
|
|
||||||
Node(Node father, Node leftChild, Node rightChild, char name, int lvl){
|
Node(Node father, Node leftChild, Node rightChild, char name, int lvl){
|
||||||
this.node = name;
|
this.node = name;
|
||||||
this.father = father;
|
this.father = father;
|
||||||
@@ -40,4 +42,20 @@ public class Node {
|
|||||||
public Node getRigthChild() {
|
public Node getRigthChild() {
|
||||||
return rigthChild;
|
return rigthChild;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public char getNode() {
|
||||||
|
return node;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Node getFather() {
|
||||||
|
return father;
|
||||||
|
}
|
||||||
|
|
||||||
|
public int getPos() {
|
||||||
|
return pos;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setPos(int pos) {
|
||||||
|
this.pos = pos;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
105
src/Tree.java
105
src/Tree.java
@@ -9,6 +9,8 @@ public class Tree {
|
|||||||
Tree (String exp){
|
Tree (String exp){
|
||||||
this.exp = exp;
|
this.exp = exp;
|
||||||
root = tree(0);
|
root = tree(0);
|
||||||
|
display();
|
||||||
|
findPatern(root);
|
||||||
}
|
}
|
||||||
|
|
||||||
private Node tree(int pos) {
|
private Node tree(int pos) {
|
||||||
@@ -18,12 +20,15 @@ public class Tree {
|
|||||||
if (item == '!'){
|
if (item == '!'){
|
||||||
Node fils = tree(pos+1);
|
Node fils = tree(pos+1);
|
||||||
Node f = new Node(null, fils, null, item, 1+fils.getLvl());
|
Node f = new Node(null, fils, null, item, 1+fils.getLvl());
|
||||||
|
fils.setPos(1);
|
||||||
fils.setFather(f);
|
fils.setFather(f);
|
||||||
return f;
|
return f;
|
||||||
} else {
|
} else {
|
||||||
Node filsG = tree(pos+1);
|
Node filsG = tree(pos+1);
|
||||||
Node filsD = tree(pos+filsG.getLvl()+1);
|
Node filsD = tree(pos+filsG.getLvl()+1);
|
||||||
Node f = new Node(null, filsG, filsD, item, filsG.getLvl() + filsD.getLvl()+1);
|
Node f = new Node(null, filsG, filsD, item, filsG.getLvl() + filsD.getLvl()+1);
|
||||||
|
filsG.setPos(1);
|
||||||
|
filsD.setPos(2);
|
||||||
filsG.setFather(f);
|
filsG.setFather(f);
|
||||||
filsD.setFather(f);
|
filsD.setFather(f);
|
||||||
return f;
|
return f;
|
||||||
@@ -37,7 +42,7 @@ public class Tree {
|
|||||||
displayNode(root,0);
|
displayNode(root,0);
|
||||||
}
|
}
|
||||||
|
|
||||||
private void displayNode(Node n, int lvl){
|
private void displayNode(Node n, int lvl) {
|
||||||
for (int i = 0; i < lvl; i++)
|
for (int i = 0; i < lvl; i++)
|
||||||
System.out.print("-");
|
System.out.print("-");
|
||||||
|
|
||||||
@@ -47,4 +52,100 @@ public class Tree {
|
|||||||
if (n.getRigthChild() != null)
|
if (n.getRigthChild() != null)
|
||||||
displayNode(n.getRigthChild(),lvl+1);
|
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;
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user