Need guidance towards evaluative boolean logic tree

前端 未结 5 1352
没有蜡笔的小新
没有蜡笔的小新 2021-02-04 16:29

I can\'t seem to find a pointer in the right direction, I am not even sure what the terms are that I should be researching but countless hours of googling seem to be spinning me

5条回答
  •  臣服心动
    2021-02-04 17:04

    Your parsing of the expression ((Sex == Male AND Age == 25) OR (Sex == Female AND Status == Single)) AND IQ > 120 looks odd. I would parse it as:

    * And
        * Or
            * And
                * ==
                    * Sex
                    * Male
                * ==
                    * Eyes
                    * Blue
            * And
                * ==
                    * Sex
                    * Female
                * ==
                    * Status
                    * Single
        * >
            * IQ
            * 120
    

    The tree type would be :

    Node
    {
        bool evaluate ()
    }
    
    AndNode : Node
    {
        Node left
        Node right
    
        bool evaluate ()
        {
            return left.evaluate () && right.evaluate ()
        }
    }
    
    // OrNode is similar
    
    EqualsNode : Node
    {
        Field field
        Value value
    
        bool evaluate ()
        {
            return field.value () == value
        }
    }
    
    // Likewise for <, >, etc
    

提交回复
热议问题