From: Jim Pryor Date: Tue, 30 Nov 2010 15:53:56 +0000 (-0500) Subject: week11 tweaks X-Git-Url: http://lambda.jimpryor.net/git/gitweb.cgi?p=lambda.git;a=commitdiff_plain;h=e3955b2351ea8fa5ea3bb09e59223806157d2368 week11 tweaks Signed-off-by: Jim Pryor --- diff --git a/week11.mdwn b/week11.mdwn index 79543c8a..56a064bb 100644 --- a/week11.mdwn +++ b/week11.mdwn @@ -93,6 +93,8 @@ to represent a list zipper where the break is at position 3, and the element occ Now how could we translate a zipper-like structure over to trees? What we're aiming for is a way to keep track of where we are in a tree, in the same way that the "broken" lists let us keep track of where we are in the base list. +We're understanding the `20` here in `node 20` to just be a metalanguage marker to help us theorists keep track of which node we're referring to. We're supposing the tree structure itself doesn't associate any informative labelling information with those nodes. It only associates informative labels with the tree leafs. (We haven't represented any such labels in our diagrams.) + It's important to set some ground rules for what will follow. If you don't understand these ground rules you will get confused. First off, for many uses of trees one wants some of the nodes or leafs in the tree to be *labeled* with additional information. It's important not to conflate the label with the node itself. Numerically one and the same piece of information---for example, the same `int`---could label two nodes of the tree without those nodes thereby being identical, as here: root @@ -134,21 +136,21 @@ This is a leaf-labeled tree whose labels aren't displayed. The `9200` and so on Suppose we want to represent that we're *at* the node marked `50`. We might use the following metalanguage notation to specify this: - {parent = ...; siblings = [node 20; *; node 80]}, * filled by node 50 + {parent = ...; siblings = [subtree 20; *; subtree 80]}, * filled by subtree 50 -This is modeled on the notation suggested above for list zippers. Here `node 20` refers not to a `int` label associated with that node, but rather to the whole subtree rooted at that node: +This is modeled on the notation suggested above for list zippers. Here `subtree 20` refers to the whole subtree rooted at node `20`: 20 / | \ 1 2 3 -Similarly for `node 50` and `node 80`. We haven't said yet what goes in the `parent = ...` slot. Well, the parent of a subtree targetted on `node 50` should intuitively be a tree targetted on `node 500`: +Similarly for `subtree 50` and `subtree 80`. We haven't said yet what goes in the `parent = ...` slot. Well, the parent of a subtree targetted on `node 50` should intuitively be a tree targetted on `node 500`: - {parent = ...; siblings = [*; node 920; node 950]}, * filled by node 500 + {parent = ...; siblings = [*; subtree 920; subtree 950]}, * filled by subtree 500 And the parent of that targetted subtree should intuitively be a tree targetted on `node 9200`: - {parent = None; siblings = [*]}, * filled by node 9200 + {parent = None; siblings = [*]}, * filled by subtree 9200 This tree has no parents because it's the root of the base tree. Fully spelled out, then, our tree targetted on `node 50` would be: @@ -157,26 +159,13 @@ This tree has no parents because it's the root of the base tree. Fully spelled o parent = { parent = None; siblings = [*] - }, * filled by node 9200; - siblings = [*; node 920; node 950] - }, * filled by node 500; - siblings = [node 20; *; node 80] - }, * filled by node 50 - -In fact, there's some redundancy in this structure, at the points where we have `* filled by node 9200` and `* filled by node 500`. Most of `node 9200`---with the exception of any label attached to node `9200` itself---is determined by the rest of this structure; and so too with `node 500`. So we could really work with: - - { - parent = { - parent = { - parent = None; - siblings = [*] - }, label for * position (at node 9200); - siblings = [*; node 920; node 950] - }, label for * position (at node 500); - siblings = [node 20; *; node 80] - }, * filled by node 50 + }, * filled by subtree 9200; + siblings = [*; subtree 920; subtree 950] + }, * filled by subtree 500; + siblings = [subtree 20; *; subtree 80] + }, * filled by subtree 50 -Or, if we only had labels on the leafs of our tree: +In fact, there's some redundancy in this structure, at the points where we have `* filled by subtree 9200` and `* filled by subtree 500`. Since node 9200 doesn't have any label attached to it, the subtree rooted in it is determined by the rest of this structure; and so too with `subtree 500`. So we could really work with: { parent = { @@ -184,47 +173,46 @@ Or, if we only had labels on the leafs of our tree: parent = None; siblings = [*] }, - siblings = [*; node 920; node 950] + siblings = [*; subtree 920; subtree 950] }, - siblings = [node 20; *; node 80] - }, * filled by node 50 + siblings = [subtree 20; *; subtree 80] + }, * filled by subtree 50 -We're understanding the `20` here in `node 20` to just be a metalanguage marker to help us theorists keep track of which node we're referring to. We're supposing the tree structure itself doesn't associate any informative labelling information with those nodes. It only associates informative labels with the tree leafs. (We haven't represented any such labels in our diagrams.) -We still do need to keep track of what fills the outermost targetted position---`* filled by node 50`---because that contain a subtree of arbitrary complexity, that is not determined by the rest of this data structure. +We still do need to keep track of what fills the outermost targetted position---`* filled by subtree 50`---because that contain a subtree of arbitrary complexity, that is not determined by the rest of this data structure. For simplicity, I'll continue to use the abbreviated form: - {parent = ...; siblings = [node 20; *; node 80]}, * filled by node 50 + {parent = ...; siblings = [subtree 20; *; subtree 80]}, * filled by subtree 50 But that should be understood as standing for the more fully-spelled-out structure. Structures of this sort are called **tree zippers**, for a reason that will emerge. They should already seem intuitively similar to list zippers, though, at least in what we're using them to represent. I think it may initially be more helpful to call these **targetted trees**, though, and so will be switching back and forth between this different terms. Moving left in our targetted tree that's targetted on `node 50` would be a matter of shifting the `*` leftwards: - {parent = ...; siblings = [*; node 50; node 80]}, * filled by node 20 + {parent = ...; siblings = [*; subtree 50; subtree 80]}, * filled by subtree 20 and similarly for moving right. If the sibling list is implemented as a list zipper, you should already know how to do that. If one were designing a tree zipper for a more restricted kind of tree, however, such as a binary tree, one would probably not represent siblings with a list zipper, but with something more special-purpose and economical. Moving downward in the tree would be a matter of constructing a tree targetted on some child of `node 20`, with the first part of the targetted tree above as its parent: { - parent = {parent = ...; siblings = [*; node 50; node 80]}; + parent = {parent = ...; siblings = [*; subtree 50; subtree 80]}; siblings = [*; leaf 2; leaf 3] }, * filled by leaf 1 -How would we move upward in a tree? Well, we'd build a regular, untargetted tree with a root node---let's call it `20`---and whose children are given by the outermost sibling list in the targetted tree above, after inserting the targetted subtree into the `*` position: +How would we move upward in a tree? Well, we'd build a regular, untargetted tree with a root node---let's call it `20'`---and whose children are given by the outermost sibling list in the targetted tree above, after inserting the targetted subtree into the `*` position: - node 20 + node 20' / | \ / | \ leaf 1 leaf 2 leaf 3 -We'll call this new untargetted tree `node 20`. The result of moving upward from our previous targetted tree, targetted on `leaf 1`, would be the outermost `parent` element of that targetted tree, with `node 20` being the subtree that fills that parent's target position `*`: +We'll call this new untargetted tree `subtree 20'`. The result of moving upward from our previous targetted tree, targetted on `leaf 1`, would be the outermost `parent` element of that targetted tree, with `subtree 20'` being the subtree that fills that parent's target position `*`: { parent = ...; - siblings = [*; node 50; node 80] - }, * filled by node 20 + siblings = [*; subtree 50; subtree 80] + }, * filled by subtree 20' Or, spelling that structure out fully: @@ -234,10 +222,10 @@ Or, spelling that structure out fully: parent = None; siblings = [*] }, - siblings = [*; node 920; node 950] + siblings = [*; subtree 920; subtree 950] }, - siblings = [*; node 50; node 80] - }, * filled by node 20 + siblings = [*; subtree 50; subtree 80] + }, * filled by subtree 20' Moving upwards yet again would get us: @@ -246,15 +234,15 @@ Moving upwards yet again would get us: parent = None; siblings = [*] }, - siblings = [*; node 920; node 950] - }, * filled by node 500 + siblings = [*; subtree 920; subtree 950] + }, * filled by subtree 500' -where `node 500` refers to a tree built from a root node whose children are given by the list `[*; node 50; node 80]`, with `node 20` inserted into the `*` position. Moving upwards yet again would get us: +where `subtree 500'` refers to a tree built from a root node whose children are given by the list `[*; subtree 50; subtree 80]`, with `subtree 20` inserted into the `*` position. Moving upwards yet again would get us: { parent = None; siblings = [*] - }, * filled by node 9200 + }, * filled by subtree 9200' where the targetted element is the root of our base tree. Like the "moving backward" operation for the list zipper, this "moving upward" operation is supposed to be reminiscent of closing a zipper, and that's why these data structures are called zippers.