Account Options

  1. Sign in
The old Google Groups will be going away soon, but your browser is incompatible with the new version.
Google Groups Home
« Groups Home
Tree Grafts/Transplants
There are currently too many topics in this group that display first. To make this topic appear first, remove this option from another topic.
There was an error processing your request. Please try again.
flag
  4 messages - Collapse all  -  Translate all to Translated (View all originals)
The group you are posting to is a Usenet group. Messages posted to this group will make your email address visible to anyone on the Internet.
Your reply message has not been sent.
Your post was successful
 
From:
To:
Cc:
Followup To:
Add Cc | Add Followup-to | Edit Subject
Subject:
Validation:
For verification purposes please type the characters you see in the picture below or the numbers you hear by clicking the accessibility icon. Listen and type the numbers you hear
 
Ryan Watts  
View profile  
 More options Oct 18 2012, 3:28 pm
From: Ryan Watts <rtwatt...@gmail.com>
Date: Thu, 18 Oct 2012 12:28:37 -0700 (PDT)
Local: Thurs, Oct 18 2012 3:28 pm
Subject: Tree Grafts/Transplants

I would think this has been done before but I've been looking around for a
straightforward solution and haven't come across anything yet.

The problem is this:

I have a Tree[A] and I want to take all the children of a node designated
by a certain path P1 and move them to another node designated by path P2.
 I want this to be recursive so if the children have children, they are
moved as well.

Can anyone point me in the right direction?


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Tony Morris  
View profile  
 More options Oct 18 2012, 8:58 pm
From: Tony Morris <tonymor...@gmail.com>
Date: Fri, 19 Oct 2012 10:58:01 +1000
Local: Thurs, Oct 18 2012 8:58 pm
Subject: Re: [scalaz] Tree Grafts/Transplants

TreeLoc#cobind? Or something like it, would have to do some twiddling to
figure it out for sure, but I would start there.
 On Oct 19, 2012 10:56 AM, "Ryan Watts" <rtwatt...@gmail.com> wrote:


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Runar Bjarnason  
View profile  
 More options Oct 18 2012, 9:21 pm
From: Runar Bjarnason <runaror...@gmail.com>
Date: Thu, 18 Oct 2012 21:21:06 -0400
Local: Thurs, Oct 18 2012 9:21 pm
Subject: Re: [scalaz] Tree Grafts/Transplants
TreeLoc will definitely do this for you. You can use findChild
repeatedly to walk into the tree along a path. Do this with both
paths, then use setTree to replace one with the other.

def walkPath[A](t: Tree[A], path: List[A]): TreeLoc[A] =
  path.foldLeft(tree.loc)((t, e) => t.findChild(_.rootLabel == e))

def graft[A](t: Tree[A], p1: List[A], p2: List[A]): Tree[A] =
  walkPath(t, p1).setTree(walkPath(t, p2).tree).root

Something like that.

Runar


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Ryan Watts  
View profile  
 More options Oct 22 2012, 2:04 pm
From: Ryan Watts <rtwatt...@gmail.com>
Date: Mon, 22 Oct 2012 11:04:13 -0700 (PDT)
Local: Mon, Oct 22 2012 2:04 pm
Subject: Re: [scalaz] Tree Grafts/Transplants

Thanks for the help.  I ended up using a slight variation on your code:

def walkPath[A](t: Tree[A], path: List[A]): TreeLoc[A] =
    path.tail.foldLeft(t.loc)((t, e) => t.findChild(_.rootLabel ==
e).getOrElse(t))

  def graft[A](t: Tree[A], to: List[A], from: List[A]): Tree[A] =
    walkPath(t, to).insertDownLast(walkPath(t, from).tree).root.tree

Basically the same except for the getOrElse and turning the graft result
into a tree.  In order to get the "transplant" behavior I was hoping for --
i.e. "take this branch and move it here" I had to add one additional method:

def prune[A](t: Tree[A], path: List[A]): Tree[A] =
    walkPath(t, path).delete.map(_.root.tree).getOrElse(t)

By combining graft and prune I was able to create my transplant method:

def transplant[A](t: Tree[A], to: List[A], from: List[A]): Tree[A] =
    prune(graft(t, to, from), from)

I will probably change the syntax around a little bit, but that's basically
the functionality I was looking for.

Thanks again for the help!


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
End of messages
« Back to Discussions « Newer topic     Older topic »