Add Tree building
This commit is contained in:
@@ -10,13 +10,16 @@ import java.util.Stack;
|
|||||||
*/
|
*/
|
||||||
public class Expression {
|
public class Expression {
|
||||||
private String expression;
|
private String expression;
|
||||||
|
private String expressionPrefixe;
|
||||||
|
|
||||||
public Expression(){
|
public Expression(){
|
||||||
readExpression();
|
readExpression();
|
||||||
parse();
|
parse();
|
||||||
|
negExpressionPrefixe();
|
||||||
}
|
}
|
||||||
|
|
||||||
public void readExpression(){
|
public void readExpression(){
|
||||||
|
System.out.println("Entrer Expression Infixe : ");
|
||||||
try {
|
try {
|
||||||
BufferedReader buff = new BufferedReader(new InputStreamReader(System.in));
|
BufferedReader buff = new BufferedReader(new InputStreamReader(System.in));
|
||||||
expression = buff.readLine();
|
expression = buff.readLine();
|
||||||
@@ -50,7 +53,7 @@ public class Expression {
|
|||||||
return c == '(' || c == ')';
|
return c == '(' || c == ')';
|
||||||
}
|
}
|
||||||
|
|
||||||
private boolean isOperator(char c) {
|
public static boolean isOperator(char c) {
|
||||||
for (int i = 0; i < Operator.nbOperators; i++) {
|
for (int i = 0; i < Operator.nbOperators; i++) {
|
||||||
if(Operator.operators[i] == c)
|
if(Operator.operators[i] == c)
|
||||||
return true;
|
return true;
|
||||||
@@ -89,8 +92,6 @@ public class Expression {
|
|||||||
stack.addLast(String.valueOf(c));
|
stack.addLast(String.valueOf(c));
|
||||||
} else if(c == ')') {
|
} else if(c == ')') {
|
||||||
while (!stack.peekLast().equals("(")) {
|
while (!stack.peekLast().equals("(")) {
|
||||||
System.out.println(reversePrefix);
|
|
||||||
System.out.println(stack.peekLast());
|
|
||||||
reversePrefix += stack.pollLast();
|
reversePrefix += stack.pollLast();
|
||||||
}
|
}
|
||||||
stack.pollLast();
|
stack.pollLast();
|
||||||
@@ -100,10 +101,26 @@ public class Expression {
|
|||||||
}
|
}
|
||||||
while (!stack.isEmpty())
|
while (!stack.isEmpty())
|
||||||
reversePrefix += stack.pollLast();
|
reversePrefix += stack.pollLast();
|
||||||
System.out.println(reverseExpression(reversePrefix));
|
|
||||||
|
expressionPrefixe = reverseExpression(reversePrefix);
|
||||||
|
System.out.println(expressionPrefixe);
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getExpression() {
|
||||||
|
return expression;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getExpressionPrefixe() {
|
||||||
|
return expressionPrefixe;
|
||||||
|
}
|
||||||
|
|
||||||
|
private void negExpressionPrefixe (){
|
||||||
|
expressionPrefixe = "!" + expressionPrefixe;
|
||||||
}
|
}
|
||||||
|
|
||||||
public static void main(String[] args) {
|
public static void main(String[] args) {
|
||||||
Expression e = new Expression();
|
Expression e = new Expression();
|
||||||
|
Tree t = new Tree(e.getExpressionPrefixe());
|
||||||
|
t.display();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
43
src/Node.java
Normal file
43
src/Node.java
Normal file
@@ -0,0 +1,43 @@
|
|||||||
|
/**
|
||||||
|
* Created by azael on 20/03/17.
|
||||||
|
*/
|
||||||
|
public class Node {
|
||||||
|
|
||||||
|
private char node;
|
||||||
|
|
||||||
|
private Node father;
|
||||||
|
private Node leftChild;
|
||||||
|
private Node rigthChild;
|
||||||
|
private int lvl;
|
||||||
|
|
||||||
|
Node(Node father, Node leftChild, Node rightChild, char name, int lvl){
|
||||||
|
this.node = name;
|
||||||
|
this.father = father;
|
||||||
|
this.leftChild = leftChild;
|
||||||
|
this.rigthChild = rightChild;
|
||||||
|
this.lvl = lvl;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setLeftChild(Node leftChild) { this.leftChild = leftChild; }
|
||||||
|
public void setRigthChild(Node rigthChild) { this.rigthChild = rigthChild; }
|
||||||
|
|
||||||
|
public void setFather(Node father) {
|
||||||
|
this.father = father;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void display() {
|
||||||
|
System.out.println(node);
|
||||||
|
}
|
||||||
|
|
||||||
|
public int getLvl() {
|
||||||
|
return lvl;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Node getLeftChild() {
|
||||||
|
return leftChild;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Node getRigthChild() {
|
||||||
|
return rigthChild;
|
||||||
|
}
|
||||||
|
}
|
||||||
49
src/Tree.java
Normal file
49
src/Tree.java
Normal file
@@ -0,0 +1,49 @@
|
|||||||
|
/**
|
||||||
|
* 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);
|
||||||
|
}
|
||||||
|
|
||||||
|
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.setFather(f);
|
||||||
|
return f;
|
||||||
|
}
|
||||||
|
|
||||||
|
Node filsG = tree(pos+1);
|
||||||
|
Node filsD = tree(pos+filsG.getLvl());
|
||||||
|
Node f = new Node(null, filsG, filsD, item, filsG.getLvl() + filsD.getLvl()+1);
|
||||||
|
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);
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user