【问题标题】:Decision tree with recursive sort algorithm具有递归排序算法的决策树
【发布时间】:2011-10-02 02:55:09
【问题描述】:

我想知道是否有人可以帮助我了解如何为递归排序创建决策树。我了解如何使用冒泡排序或插入排序来做到这一点。但是,当涉及到递归排序时,我无法想象。如果伪代码是这样的:

if length == 1
    return;
else
    int elem = head of array;
    array tail = tail of array;
    recursive sort;
    if (elem <= head of sorted list)
        add elem to the beginning of sorted list
    else
        swap elem and first element of sorted list
        sort smaller list again
        add elem to the beginning of sorted list
return

我最初的想法是决策树应该如下所示:

                               A, B, C
                           yes /     \ no  is length <= 1?
                              /       \
                                      remove head
                                        /   \
                                       A    B, C
                                        yes /   \ no  is length <= 1?
                                           /     \
                                                remove head
                                                  /   \
                                                  B   C
                                                 yes /   \ no   is length <= 1?
                                                    /     \
                                                 B:C
                                                /   \
                                              B,C   C,B
                                             |         |
                                          A:B,C       A:C,B
                                         /   \        /   \
                                     A,B,C   B:A,C  A,C,B  C:A,B
                                             /  \          /   \
                                        B,A,C   A:B,C   C,A,B  A:C,B

我显然在某个地方出错了,我只是不太确定在哪里。我在正确的轨道上吗?

感谢您给我的任何指点。

【问题讨论】:

    标签: algorithm decision-tree


    【解决方案1】:

    (这是作业吗?)

    再次查看您的代码!您目前在 if-then-else 构造中进行双向分支。解决这个问题,您应该会得到一个正确的结果。

    此外,您正在那里展开调用堆栈,因此返回将“更正确”。 Wikipedia 可能会让您了解其工作原理。

    祝你好运!

    【讨论】:

    • 感谢您的快速回复。这不是家庭作业;我一直在努力更好地理解算法,而我正在阅读的书目前正在讨论决策树。找到了这个例子并试图理解它:)
    【解决方案2】:

    按照您的表述,结果将是这样的:

                                A, B, C
                           yes /     \ no  is length <= 1?
                              /       \
                                      remove head
                                        /   \
                                       A    B, C
                                        yes /   \ no  is length <= 1?
                                           /     \
                                                remove head
                                                  /   \
                                                  B   C
                                                 yes /   \ no   is length <= 1?
                                                    /     \
                                                 B:C
                                                /   \
                                              B,C   C,B
                                             |         |
                                          A:B,C       A:C,B
                                         /   \        /   \
                                     A,B,C   B:A,C  A,C,B  C:A,B
                                             /  \          /   \
                                        B,A,C   **B,C,A**   C,A,B  **C,B,A**
    

    在最后一步中,您决定是需要交换还是对左侧的两个进行排序。如果是,则无需继续排序,因为右侧已排序,如果不是,则先交换最左侧的元素,然后将右侧的两个元素排序。

    例如,B:A,C --swap-> A:B,C --sort-> A,B,C 或 A,C,B。

    希望对你有帮助。

    【讨论】:

      猜你喜欢
      • 2019-10-30
      • 2015-10-24
      • 2012-10-18
      • 2021-03-17
      • 2017-03-18
      • 1970-01-01
      • 2022-01-13
      • 1970-01-01
      • 2017-01-14
      相关资源
      最近更新 更多