如何在javascript中实现决策树.想找个比我丑的更好的办法



我正在寻找一种更好的方法来实现javascript中的决策树。作为一个编程新手,我的工具箱里只有非常有限的工具。我所知道的唯一方法是:.有一个巨大的丑陋的难以维护和遵循的if else if语句我可以使用一个switch/case语句来做一个状态机类型的事情。

欢迎提出建议和理论。此外,一些小代码示例也会很有帮助。谢谢你看一看。

戴尔

如果它是一个非常大的树,特别是如果它是由数据生成的,那么您可以使用函数方法将决策函数视为数据。例如:

var decisionTree = 
    new Case( true, Array(
                  new Case ( function(n){ return n < 0; }, Math.sin ),
                  new Case ( function(n){ return n < 2; }, "0<= n < 2" ),
                  new Case ( true, "Greater than two " )));
decisionTree.evaluate(1); // evaluates to string "0<= n < 2"
decisionTree.evaluate(-Math.PI/2); // evaluates to -1
decisionTree.evaluate(5); // evaluates to string "Greater than two"

使用这个实现,你可以任意嵌套你的树:

// Represents a predicate and corresponding action to take if predicate is a
// match.
//
// predicate : true or Function( object ) returning a boolean.
//
// action : One of Function, Case, Array of Cases or other value (see
//          Case.evaluate as to how each is applied)
//
//
Case = function (predicate, action) {  
    this.predicate = predicate;
    this.action = action;
};

Case.prototype = {
    nomatch : { match : false },
    match : function (v) { return { match : true, result :v }; },

    // Recursively test Cases and applies corresponding action on
    // `object`.
    //
    // The action applied depends on the datatype of `action`:
    //
    // - Function : evaluates to `action( object )`
    // 
    // - Case : A subsequent test is performed.  Evaluates to whatever
    //          the Cases action evaluates to.
    //          
    // - Array of Cases : Subsequent tests are performed.  Evaluates to whatever
    //          the action of the first matching Case evaluates to.
    //
    // - Any other Value : Evaluates to itself
    // 
    // returns object containing fields:
    //
    //     match:  boolean, indicates if Case was a match
    //
    //     result:  result of action applied
    // 
    evaluate : function( object ) {
        var match = this.predicate;
        if ( match instanceof Function )
            match = match( object );
        if ( match ) {
            if (this.action instanceof Function )
                return this.match( this.action(object) );
            if ( this.action instanceof Case )
                return this.action.evaluate( object );
            if ( this.action instanceof Array ) {
                var decision;
                var result;
                for (var c = 0; c < this.action.length; c++ ) {
                    decision = this.action[c];
                    if ( decision instanceof Case )  {
                        result = decision.evaluate( object );
                        if (result.match)
                            return result;
                    } else throw("Array of Case expected");
                }
                return this.nomatch;
            }
            return this.match(this.action);
        } 
        return this.nomatch;
    }
};

这类事情的最佳实践是以一种有意义的方式嵌套if-then语句,然后将它们放在它们自己的函数体中。一个函数不应该有超过2个嵌套的if;之后,在命名和实现良好的函数中进一步嵌套if是可以接受的,这样可以抽象程序的复杂性,同时保留其对程序员的意义,他们将在你离开后阅读你的代码。:)

相关内容

最新更新