简体   繁体   English

如何遍历具有多个分支的树

[英]How to traverse a tree with multiple branches

I'm working on an experimental TreeView where each TreeViewItem can either represent a condition, or a branch with an operator. 我正在研究一个实验性的TreeView,其中每个TreeViewItem既可以表示条件,也可以表示带有运算符的分支。 This is to be parsed into SQL. 这将被解析为SQL。

For example, the tree could have a branch with an "AND" or an "OR" operator, whose children would then be conditions. 例如,树可以有一个带有“AND”或“OR”运算符的分支,其子项就是条件。 This is used to as to be able to generate the WHERE segment of an SQL statement, for example ((Name = 'Matt' AND AGE > 20) OR (Name = 'John' AND Age = 15)) AND Job = 'Student' . 这用于能够生成SQL语句的WHERE段,例如((Name = 'Matt' AND AGE > 20) OR (Name = 'John' AND Age = 15)) AND Job = 'Student'

How can I go about constructing that? 我怎样才能构建它? What I've done so far is thought about placing a string,list<Condition> pair in a Tuple<> , where the string represents the branch operator (AND/OR), and the list represents the conditions contained within that branch. 到目前为止我所做的是考虑在Tuple<>放置一个string,list<Condition>对,其中字符串表示分支操作符(AND / OR),列表表示该分支中包含的条件。

However, since each branch can be split into a number of operator branches or conditions, it can get extremely complicated very quickly 但是,由于每个分支可以分成许多操作员分支或条件,因此可以非常快速地变得非常复杂

You can use recursive function to parse the treeview from top, so each root note of the treeview is one SQL statement: 您可以使用递归函数从顶部解析treeview ,因此treeview每个根音都是一个SQL语句:

eg: 例如:

在此输入图像描述

function code: 功能代码:

string getHead(TreeViewItem t)
            {
                string s = "";
                if (t.Items.Count == 0) //get the condition
                {
                    return s=t.Header.ToString(); //change this to your real getCondition function.
                }
                else
                {
                    for (int i = 0; i < t.Items.Count; i++ )
                    {
                        if(t.Items[i] is TreeViewItem) //Edit: only use treeviewitems not the button...
                        {
                          if (i == 0) // first note doesn't need the operator 
                          {
                            s += getHead(t.Items[0] as TreeViewItem);
                          }
                          else // only needs operator in between
                          {
                             s += (string.IsNullOrEmpty(getHead(t.Items[i] as TreeViewItem).Trim()) ? "" : (" " + t.Header + " " + getHead(t.Items[i] as TreeViewItem))); // only get real treeviewitem, not the one with two buttons and an empty header; change t.Header to your real getOperator function.

                          }
                        }                    
                    }
                    return string.Format("({0})",s); //group sub conditions
                }
            }

usage: 用法:

MessageBox.Show(getHead((treeView1.Items[0] as TreeViewItem)));

result: 结果:

在此输入图像描述

In your tree view, aren't the last AND and OR supposed to be interchanged? 在树视图中,不是最后的AND和OR应该互换? I cannot get to the same parsed string you specified below with that view. 我无法使用该视图访问您在下面指定的相同解析字符串。

AND
    AND
         Job='Student'
         Age > 20
    AND
         OR
            Name='John'
            Name='Matt'
         Sex='Male'   

The last AND ands another OR condition and a single statement. 最后一个AND和另一个OR条件和一个语句。

Not sure if this helps, but bison generates C code for bottom up parsing like this. 不确定这是否有帮助,但是bison生成C代码,用于自下而上解析。 You can give it a try. 你可以尝试一下。

e: conditional statement|
   e AND e|
   e OR e
;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM