【问题标题】:prolog - bst tree to listprolog - 要列出的 bst 树
【发布时间】:2016-05-22 18:32:01
【问题描述】:

我正在练习 dcg 技术,但我有一个问题。我将实现将 BST 树转换为列表(每个可能)并在反面 - 列表 ot bst(每个可能)。 (此处的列表表示真实设置)。
然而,
这个语法应该没问题,但它会产生很多重复。我知道原因 (nil),但我无法处理。

 bstToList(nil) --> [].
    bstToList(t(Root, L, R)) --> [Root], bstToList(L), bstToList(R).
    bstToList(t(Root, L, R)) --> [Root], bstToList(R), bstToList(L).
    bstToList(t(Root, L, R)) -->  bstToList(R), [Root], bstToList(L).
    bstToList(t(Root, L, R)) -->  bstToList(L), [Root], bstToList(R).
    bstToList(t(Root, L, R)) --> bstToList(L), bstToList(R), [Root].
    bstToList(t(Root, L, R)) --> bstToList(R), bstToList(L), [Root].

你能帮帮我吗?

【问题讨论】:

    标签: prolog dcg


    【解决方案1】:

    您如何实现它的问题在于您在每个遍历步骤中混合了每个遍历方法。换句话说,在每个遍历步骤中,您的谓词都会探索每个遍历方法。

    你需要在更高的层次上分解它们:

    bst_to_list(BST, Ls) -->
        bst_to_list_pre_LR(BST, Ls, _) |
        bst_to_list_pre_RL(BST, Ls, _) |
        bst_to_list_in_LR(BST, Ls, _) |
        bst_to_list_in_RL(BST, Ls, _) |
        bst_to_list_post_LR(BST, Ls, _) |
        bst_to_list_post_RL(BST, Ls, _).
    

    然后分别实现每种类型:

    % Pre-order, left-to-right
    bst_to_list_pre_LR(nil, Es, Es) --> [].
    bst_to_list_pre_LR(t(Root, L, R), [_|Es0], Es) -->
        [Root],
        bst_to_list_pre_LR(L, Es0, Es1),
        bst_to_list_pre_LR(R, Es1, Es).
    
    % Pre-order right-to-left
    bst_to_list_pre_RL(nil, Es, Es) --> [].
    bst_to_list_pre_RL(t(Root, L, R), [_|Es0], Es) -->
        [Root],
        bst_to_list_pre_RL(R, Es0, Es1),
        bst_to_list_pre_RL(L, Es1, Es).
    
    ... % Etc...
    

    另外,请再次仔细阅读@mat 对您先前问题的回答Prolog, reconstruct BST trees from inorder list,因为那里提供了有用的技术,可以为每种情况提供更强大的解决方案。我已经在上面的代码中加入了这种技术。

    请注意,您仍然可能会得到重复,因为某些树在以不同方式遍历时会产生相同的结果。


    根据 cmets,似乎寻求一种解决方案来定义 BST 和节点列表之间的关系,以便 BST 中的顺序可以是任意的。由于排序可以是任何东西,因此它不适合 DCG。这是一个可能的非 DCG 解决方案:
    bst_list_all(BST, L) :-
        tree_list_bound(BST, L, _),
        bst_list_all_(BST, L).
    
    bst_list_all_(nil, []).
    bst_list_all_(t(Root, L, R), Es) :-
        select(Root, Es, Es1),
        append(Ls, Rs, Es1),
        bst_list_all_(L, Ls),
        bst_list_all_(R, Rs).
    
    tree_list_bound(nil, [], []).
    tree_list_bound(t(_, L, R), [_|Es0], Es2) :-
        tree_list_bound(L, Es0, Es1),
        tree_list_bound(R, Es1, Es2).
    tree_list_bound(t(_, L, R), [_|Es0], Es2) :-
        Es0 = [_|_],
        tree_list_bound(R, Es0, Es1),
        tree_list_bound(L, Es1, Es2).
    

    谓词tree_list_bound 为解决方案建立了一个有界框架以避免终止问题。它“感觉”这个解决方案可以被浓缩,但我还没有找到一种方法来做到这一点。但是,它似乎确实为所揭示的问题提供了解决方案,而不会产生重复的解决方案。

    ?- bst_list_all(t(1,nil,t(2,t(3,nil,nil),nil)), L).
    L = [1, 2, 3] ;
    L = [1, 3, 2] ;
    L = [2, 1, 3] ;
    L = [3, 1, 2] ;
    L = [2, 3, 1] ;
    L = [3, 2, 1] ;
    false.
    
    ?- bst_list_all(BST, [1,2,3]).
    BST = t(1, t(2, t(3, nil, nil), nil), nil) ;
    BST = t(1, t(3, t(2, nil, nil), nil), nil) ;
    BST = t(2, t(1, t(3, nil, nil), nil), nil) ;
    BST = t(2, t(3, t(1, nil, nil), nil), nil) ;
    BST = t(3, t(1, t(2, nil, nil), nil), nil) ;
    BST = t(3, t(2, t(1, nil, nil), nil), nil) ;
    BST = t(1, t(2, nil, t(3, nil, nil)), nil) ;
    BST = t(1, t(3, nil, t(2, nil, nil)), nil) ;
    BST = t(2, t(1, nil, t(3, nil, nil)), nil) ;
    BST = t(2, t(3, nil, t(1, nil, nil)), nil) ;
    BST = t(3, t(1, nil, t(2, nil, nil)), nil) ;
    BST = t(3, t(2, nil, t(1, nil, nil)), nil) ;
    BST = t(1, nil, t(2, t(3, nil, nil), nil)) ;
    BST = t(1, nil, t(3, t(2, nil, nil), nil)) ;
    BST = t(2, nil, t(1, t(3, nil, nil), nil)) ;
    BST = t(2, nil, t(3, t(1, nil, nil), nil)) ;
    BST = t(3, nil, t(1, t(2, nil, nil), nil)) ;
    BST = t(3, nil, t(2, t(1, nil, nil), nil)) ;
    BST = t(1, nil, t(2, nil, t(3, nil, nil))) ;
    BST = t(1, nil, t(3, nil, t(2, nil, nil))) ;
    BST = t(2, nil, t(1, nil, t(3, nil, nil))) ;
    BST = t(2, nil, t(3, nil, t(1, nil, nil))) ;
    BST = t(3, nil, t(1, nil, t(2, nil, nil))) ;
    BST = t(3, nil, t(2, nil, t(1, nil, nil))) ;
    false.
    
    ?-
    

    【讨论】:

    • @HaskellFun 我不认为[1,3,4,5,6] 是一个有效的案例,除非你真的真的想在每次迭代中“混合和匹配”遍历方法。在这种情况下,是的,您将根据原始问题得到重复项。
    • @HaskellFun 我在我修复的答案中的示例中有一个印刷错误。而且我不知道你实现了什么,但是当我根据我的答案尝试一个完整的解决方案时,我能够从你的示例查询中得到一个解决方案。
    • @HaskellFun 如果您使用我在答案中显示的模式实现完整谓词,您应该获得Ls = [1,3,4,5,6], phrase(bst_to_list(X, Ls), Ls). 的多个解决方案(请注意,使用@mat 之前的答案可以防止谓词出现终止问题)。
    • @HaskellFun 当我用我的代码运行该查询时,我得到了包括[6,4,3] 在内的所有结果,因此您在遵循我的答案时一定有误。
    • @HaskellFun 如果您真的想要每个列表,那么您实际上只是在要求所有可能访问的排列。如果我正在编写代码来生成它,我只需遍历树一次以收集节点,然后运行置换算法。您对问题的最初尝试表明您正在寻找“正常”遍历方法。
    猜你喜欢
    • 1970-01-01
    • 2021-01-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-02-28
    • 1970-01-01
    相关资源
    最近更新 更多