在本地编辑纯功能树

时间:2012-01-26 23:11:26

标签: tree immutability purely-functional zipper

让我们定义一个树T:

    A
   / \
  B   C
 / \
D   E

假设有一个新节点被添加到E,产生T':

    A
   / \
  B   C
 / \
D   E
     \
      G

在可变语言中,这是一项简单的任务 - 只需更新E的孩子,我们就完成了。然而,在一个不可变的世界中,有必要先知道E的路径,然后从E +新孩子中导出E',然后导出B',最后得到A'(= T')。

这很麻烦;理想情况下,会有一些函数可以获取E和G的值(可能还有T)并产生T',而不提供E的路径。

我看到两种可能的方法来解决这个问题:

  • 父引用 - 这样,每个节点都能够派生到根路径。两个问题:创建两个相互引用的节点(即父< - >子)是纯功能语言中的问题(任何简单的解决方案?);每当E - > E'是派生的,E'的所有的孩子也需要新派生,因为他们现在存储旧的E值而不是E'。
  • zippers - 每个节点在创建时存储拉链,从其父拉链派生。相互引用的问题消失了,但是,当E - > E'衍生出来,所有E'的儿童拉链也需要衍生出来,因为它们现在指向旧树E..

在考虑到合理的表现时,我的愿望是否可能?非常感谢任何意见!

1 个答案:

答案 0 :(得分:1)

另一种选择, 基于做懒惰替换。 如果它对性能至关重要并且会对它做出很多改变,我会建议对它进行基准测试。

我已经在F#中实现了它,但我不认为除了我的打印功能之外我还没有使用任何“inpure”。

这是一个文字墙, 基本原则是保持树懒, 通过替换返回节点的函数来替换节点。

诀窍是你需要一些方法来识别一个节点,这不是它自己的引用/名称,而不是按值。 识别必须可以复制到ReplacementNodes上 在这种情况下,我使用了System.Object,因为它们是各自不同的。

type TreeNode<'a> = {
    getChildren: unit -> seq<TreeNode<'a>>;
    value: 'a;
    originalRefId: System.Object; //This is set when the onject is created,
                                  // so we can identify any nodes that are effectivly this one 
    }


let BasicTreeNode : 'a ->seq<TreeNode<'a>>-> TreeNode<'a>  = fun nodeValue -> fun children ->
    {value = nodeValue; originalRefId = System.Object(); getChildren = fun () -> children;}


let rec ReplacementNode : TreeNode<'a> -> TreeNode<'a> -> TreeNode<'a> -> TreeNode<'a>  = 
    fun nodeToReplace -> fun newNode -> fun baseNode ->
        if (System.Object.ReferenceEquals(baseNode.originalRefId, nodeToReplace.originalRefId)) then
            //If it has the same Oringal 
            newNode //replace the node
        else
            //Just another pass on node, tranform its children, keep orignial reference
            {value = baseNode.value; 
             originalRefId = baseNode.originalRefId;
             getChildren = fun () -> 
                baseNode.getChildren() |> Seq.map(ReplacementNode nodeToReplace newNode); }


type TreeType<'a> = {
    Print: unit -> unit; 
    ReplaceNode: TreeNode<'a> -> TreeNode<'a> -> TreeType<'a>;
    //Put all the other tree methods, like Traversals, searches etc in this type
    }

let rec Tree  = fun rootNode ->
    {
        Print = fun () -> 
            let rec printNode = fun node -> fun depth -> 
                printf "%s %A\n" (String.replicate depth " - ")  node.value
                for n in node.getChildren() do printNode n (depth + 1)
            printNode rootNode 0
            ;
        ReplaceNode = fun oldNode -> fun newNode ->
            Tree (ReplacementNode oldNode newNode rootNode)



    }

测试用例/示例:

let e = BasicTreeNode "E" Seq.empty
let d = BasicTreeNode "D" Seq.empty
let c = BasicTreeNode "C" Seq.empty
let b = BasicTreeNode "B" [d;e]
let a = BasicTreeNode "A" [b;c]
let originalTree = Tree a
printf "The Original Tree:\n"
originalTree.Print()

let g = BasicTreeNode "G" Seq.empty
let newE = BasicTreeNode "E" [g]

let newTree = originalTree.ReplaceNode e newE
printf "\n\nThe Tree with a Local Change: \n"
newTree.Print()

printf "\n\nThe Original Tree is Unchanged: \n"
originalTree.Print()


printf "\n\nThe Tree with a Second Local Change: \n"
let h = BasicTreeNode "H" Seq.empty
let newC = BasicTreeNode "C" [h]
let newTree2 = newTree.ReplaceNode c newC
newTree2.Print()



printf "\n\nTrying to Change a node that has been replaced doesn't work \n"
let i = BasicTreeNode "i" Seq.empty
let newnewC = BasicTreeNode "C" [h; i]
let newTree3 = newTree.ReplaceNode c newC //newTree.ReplaceNode newc newnewC would work
newTree3.Print()

我们在测试结束时看到,对于要替换的对象使用旧节点名称(/ reference)不起作用。 可以选择创建具有另一个节点的引用ID的新类型:

//Like a basicTreeNode, but reuses an existing ID, so they can be replaced for oneanother
let EdittedTreeNode = fun orignalNode -> fun nodeValue -> fun children -> 
    {value = nodeValue; originalRefId = orignalNode.originalRefId; getChildren = fun () -> children;}

您还可以编辑ReplacementNode定义,以便保留其替换的节点的ID。 (不仅仅是返回newNode,而是返回另一个新节点,其中包含value的{​​{1}}和getChildren,但newNode的{​​{1}} {1}})