简体   繁体   English

解释递归树遍历的最佳UML图是什么?

[英]What is the best UML diagram for explaining recursive tree traversal?

I want to explain my C# code which traverses a parse tree for code analysis. 我想解释一下遍历解析树进行代码分析的C#代码。 It is similar to the following but longer: 它类似于以下但更长:

    private void traverse(ParseTreeNode node)
    {
        if (node.ChildNodes.Count == 0)
        {
            return;
        }

        switch (node.Term.Name.ToUpper())
        {
            case "FILE":
                traverse(node.ChildNodes[0]);
                return;

            case "PROGRAM":
                traverse(node.ChildNodes[0]);
                return;

            //etc.
        }
    }

What is the most appropriate UML diagram for showing this? 显示这个的最合适的UML图是什么? Thanks 谢谢

Since you are explaining behavior of the tree traversal, not structure of the tree, your choices are limited to Behavior Diagrams . 由于您正在解释树遍历的行为,而不是树的结构,因此您的选择仅限于行为图 Since you wish to show what happens inside a method, your choices are further narrowed down to Interaction Diagrams . 由于您希望显示方法内部发生的情况,因此您的选择将进一步缩小为交互图

At this point, you need to choose between a Sequence Diagram and a Collaboration Diagram . 此时,您需要在序列图协作图之间进行选择。 Both diagram types could be used to show a tree traversal. 两种图表类型都可用于显示树遍历。 The main difference is in how the method calls are going to be shown. 主要区别在于如何显示方法调用。 In the sequence diagram you would show invocations with horizontal lines going across the "swim lanes", while the collaboration diagram will distinguish the calls using numbers. 在序列图中,您将显示带有横跨“泳道”的水平线的调用,而协作图将使用数字区分呼叫。

Since this is recursion, you would be showing multiple calls to the method with the same name. 由于这是递归,因此您将显示具有相同名称的方法的多个调用。 This gives advantage to sequence diagrams, because the readers would not need to check the numbering to see whose traverse method you are calling. 这有利于序列图,因为读者不需要检查编号以查看您正在调用的traverse方法。 That is why I would use a sequence diagram.. 这就是我使用序列图的原因..

When I draw sequence diagrams showing recursive interaction, I build a small object graph on a piece of paper, and then draw out the interactions of the recursive method. 当我绘制显示递归交互的序列图时,我在一张纸上构建一个小对象图,然后绘制出递归方法的交互。

EDIT : Here is why I would not use an Activity Diagram * to show a sequence of recursive calls. 编辑:这就是为什么我不会使用活动图*来显示一系列递归调用。 Despite a note in the comment suggesting to use an Activity Diagram for this, I think this is not a good idea: activity diagrams are good at capturing algorithms happening within the same object, while here we deal with an algorithm that spawns multiple objects. 尽管评论中有一条说明建议使用活动图,但我认为这不是一个好主意:活动图擅长捕获同一对象中发生的算法,而在这里我们处理产生多个对象的算法。

* Which is a glorified flowchart, a notoriously poor mechanism for capturing the essence of recursive algorithms. *这是一个美化的流程图,一种众所周知的用于捕获递归算法本质的糟糕机制。

Continuing the discussion arisen in the comments after dasblinkenlight's answer, I suggest the following activity diagram as a solution: 在dasblinkenlight的回答之后的评论中继续讨论,我建议将以下活动图作为解决方案:

在此输入图像描述

Activity diagram puts an emphasis on the sequence of steps in a process/algorithm, decisions made on the way, manipulated data and calculations, eventually concurrent tasks, invocations, etc. 活动图强调流程/算法中的步骤顺序,在路上做出的决策,操纵数据和计算,最终并发任务,调用等。

There is always some kind of context to activity diagram. 活动图总是有某种上下文。 In this case it is a method traverse(). 在这种情况下,它是一个方法traverse()。

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

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