【问题标题】:Infix to Binary Expression Tree二进制表达式树的中缀
【发布时间】:2016-05-26 19:48:42
【问题描述】:

我有像这样的中缀表达式:

((source = id)AND (target= id) AND ( NOT( color != blue) OR ( age<= 23 )))

如何转换成二叉树,如下图所示。我刚开始用javascript编写代码,但不知道如何编写插入操作,尤其是像NOT这样的一元运算符。如何解决这个问题。

var Node = function(data,left,right){
    this.data = data;
    this.left = left;
    this.right = right;
};


var BinaryTree = function(){
    this.head = new Node(null,null,null);
};

BinaryTree.prototype.insert = function(data){
    if(this.head.data == null){
        this.head = new Node(data,null,null);
    } else{
        var current = this.head;
        insert(current,data);
    }
};

var insert = function(current,data){
    // How to insert ?
};

【问题讨论】:

  • 嗯,你需要不同类型的节点,只有一些是二进制的。你有值节点,代表/持有一个值,根本没有运算符,然后你有像!, +, -, ~ 这样的一元节点,你的二元节点,甚至可能添加三元运算符。看看this post,我在那里实现了这样一个解析器,并构建了一种这样的树,虽然不是作为节点,而是作为闭包
  • 刚刚看到:它是unary 运算符,而不是urinary。和那个体液没有关系。
  • opps .感谢您指出@Thomas
  • 我看了你的帖子,是否可以构造表达式树,我应该实现什么逻辑。你能帮我开始吗? @托马斯
  • 嗯,首先你需要为你需要的不同类型的节点实现类:“值、属性、一元和二元”

标签: javascript java algorithm tree binary-tree


【解决方案1】:

正如昨晚所说,我要说的重点是:

图形节点:

//abstract base-class
class GraphNode {
    constructor(){
        Object.defineProperty(this, "parent", {
            writable: true,
            //enumerable: false,    //so it doesn't show up in JSON
            value: null
        })
    }
    compute(ctx){ throw new Error("not implemented") }
    toString(){ throw new Error("not implemented") }
}

//leaf-nodes
class ValueNode extends GraphNode{
    constructor(value){ 
        super();
        this.value = value; 
    }
    compute(){ return this.value; }
    toString(){ return JSON.stringify(this.value); }
}

class PropertyNode extends GraphNode{
    constructor(property){
        super();
        this.property = property;
    }
    compute(ctx){ return ctx[this.property]; }
    toString(){ return String(this.property); }
}

//tree-nodes
class UnaryNode extends GraphNode{
    constructor(op, node){
        if(!(node instanceof GraphNode)){
            throw new Error("invalid node passed")
        }
        super();
        this.op = op;
        this.node = node;
        node.parent = this;
    }
    compute(ctx){
        var v = this.node.compute(ctx);
        switch(this.op){
            case "NOT": return !v;
        }
        throw new Error("operator not implemented '"+this.op+"'");
    }
    toString(){
        return  "( " + this.op + " " + this.node.toString() + " )";
    }   
}
UnaryNode.operators = ["NOT"];


class BinaryNode extends GraphNode{
    constructor(op, l, r){
        if(!(l instanceof GraphNode && r instanceof GraphNode)){
            throw new Error("invalid node passed")
        }
        super();
        this.op = op;
        this.left = l;
        this.right = r;
        l.parent = this;
        r.parent = this;
    }
    compute(ctx){
        var l = this.left.compute(ctx);
        var r = this.right.compute(ctx);
        switch(this.op){
            //logic operators
            case "AND": return l && r;
            case "OR": return l || r;

            //comparison-operators
            case "=": return l === r;
            case "<=": return l <= r;
            case ">=": return l >= r;
            case "!=": return l != r;
            case ">": return l > r;
            case "<": return l < r;

            //computational operators
            case "+": return l + r;
            case "-": return l - r;
            case "*": return l * r;
            case "/": return l / r;
        }
        throw new Error("operator not implemented '"+this.op+"'");
    }

    toString(){
        return "( " + this.left.toString() + " " + this.op + " " + this.right.toString() + " )";
    }
}
//also defines precendence
BinaryNode.operators = [
    "*","/","+","-",
    ">","<","<=",">=","!=","=",
    "AND","OR",
]

//dot is kind of special:
class DotNode extends BinaryNode{
    constructor(l, r){
        /*
        if(!(l instanceof PropertyNode || l instanceof DotNode)){
            throw new Error("invalid left node")
        }
        */
        if(!(r instanceof PropertyNode)){
            throw new Error("invalid right node")
        }
        super(".", l, r);
    }

    compute(ctx){
        //especially because of this composition:
        //fetch the right property in the context of the left result
        return this.right.compute( this.left.compute(ctx) );
    }
    toString(){
        return this.left.toString() + "." + this.right.toString();
    }
}

解析器:

function escapeForRegex(str){
    return String(str).replace(/[.*+?^=!:${}()|[\]\/\\]/g, '\\$&');
}

//dynamically build my parsing regex:
var tokenParser = new RegExp([
        //numbers
        /\d+(?:\.\d*)?|\.\d+/.source,

        //string-literal
        //  /["](?:\\[\s\S]|[^"])+["]|['](?:\\[\s\S]|[^'])+[']/.source,

        //booleans
        //"true|false",

        //operators
        [".", "(", ")"].concat(UnaryNode.operators, BinaryNode.operators)
            .sort((a,b) => b.length-a.length) //so that ">=" is added before "=" and ">", for example
            .map(escapeForRegex)
            .join("|"),

        //properties
        //has to be after the operators
        /[a-zA-Z$_][a-zA-Z0-9$_]*/.source,

        //remaining (non-whitespace-)chars, just in case
        //has to be at the end
        /\S/.source
    ].map(s => "("+ s +")").join("|"), "g");

function parse(str){
    var tokens = [];
    //abusing str.replace() as a RegExp.forEach
    str.replace(tokenParser, function(token, number, op, property){
        if(number){
            token = new ValueNode(+number);
        //}else if(string){
        //  token = new ValueNode(JSON.parse(string));      
        //}else if(bool){
        //  token = new ValueNode(bool === "true");
        }else if(property){
            token = new PropertyNode(property);
        }else if(!op){
            throw new Error("unexpected token '"+token+"'");
        }
        tokens.push(token);
    });

    for(var i; (i=tokens.indexOf(".")) > -1; ){
        tokens.splice(i-1, 3, new DotNode(tokens[i-1], tokens[i+1]))
    }

    for(var i,j; (i=tokens.lastIndexOf("(")) > -1 && (j=tokens.indexOf(")", i)) > -1;){
        tokens.splice(i, j+1-i, process(tokens.slice(i+1, j)));
    }
    if(~tokens.indexOf("(") || ~tokens.indexOf(")")){
        throw new Error("mismatching brackets");
    }

    return process(tokens);
}

function process(tokens){
    UnaryNode.operators.forEach(token => {
        for(var i=-i; (i=tokens.indexOf(token, i+1)) > -1;){
            tokens.splice(i, 2, new UnaryNode(token, tokens[i+1]));
        }
    })

    BinaryNode.operators.forEach(token => {
        for(var i=1; (i=tokens.indexOf(token, i-1)) > -1;){
            tokens.splice(i-1, 3, new BinaryNode(token, tokens[i-1], tokens[i+1]));
        }
    });

    if(tokens.length !== 1){
        console.log("error: ", tokens.slice());
        throw new Error("something went wrong");
    }
    return tokens[0];
}

它的用法:

var tree = parse("((a.source = id)AND (target= id) AND ( NOT( color != blue) OR ( age<= 23 )))")
//var tree = parse("1=1=age+10>30"); //to test operator precedence

var data = {
    id: 12345,

    a: { source: 12345 },
    target: 12345,

    color: "#FF0",
    blue: "#00F",

    age: 20
}

console.log(tree.compute(data));
console.log(tree.toString());
console.log(JSON.stringify(tree, null, 2));

【讨论】:

  • 你是最好的,想成为像你一样的@Thomas
  • 真心希望你能从中吸取教训;理解和扩展代码。否则只会浪费时间,只有我在做你的工作。
  • 如果表达式 (foo=12 AND bar=24 NOT(sed=34)) 形成的最后一个进程令牌不是它的两个 [BinaryNode , UnaryNode] 之一。树的优先顺序应该是什么? @托马斯
  • 如果&gt;= 在正则表达式中领先于&gt;=,它会将它们识别为两个单独的运算符,而不是作为组合运算符。 check that。因此,按长度降序排序是确保组合运算符领先于“简单”运算符的一种简单方法,因此将被识别。
  • 如果第一个条件也与第二个条件的值匹配,则它与从未达到的第二个条件类似,例如:if(val &gt; 10){ ... } else if(val &gt; 20){ ... }。你必须先测试更严格的东西,只有当它失败时才会考虑另一个。
猜你喜欢
  • 2014-05-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多