digitalmars.D.learn - foreach DFS/BFS for tree data-structure?
- =?iso-8859-1?Q?Robert_M._M=FCnch?= (13/13) Jun 14 2018 I have a simple tree C data-structure that looks like this:
- rikki cattermole (26/35) Jun 14 2018 Node* parent;
- Dennis (4/6) Jun 14 2018 You can also use opApply to iterate over a tree using foreach,
- =?iso-8859-1?Q?Robert_M._M=FCnch?= (6/12) Jun 14 2018 Ah... that looks very good. Need to dig a bit deeper on how to use it. T...
- Steven Schveighoffer (13/25) Jun 14 2018 Just to clarify, RBTree can easily do DFS without a real stack because
- Meta (5/15) Jun 14 2018 While it's possible to do with input ranges, it's not pretty and
- Timoses (5/15) Jun 16 2018 What I found really interesting when reading Ali Çehreli's book
I have a simple tree C data-structure that looks like this: node { node parent: vector[node] children; } I would like to create two foreach algorthims, one follwing the breadth first search pattern and one the depth first search pattern. Is this possible? I read about Inputranges, took a look at the RBTree code etc. but don't relly know/understand where to start. -- Robert M. Münch http://www.saphirion.com smarter | better | faster
Jun 14 2018
On 14/06/2018 11:31 PM, Robert M. Münch wrote:I have a simple tree C data-structure that looks like this: node {struct Node {    node parent:Node* parent;    vector[node] children;Node[] children;} I would like to create two foreach algorthims, one follwing the breadth first search pattern and one the depth first search pattern.Here is (very roughly breadth): auto search(Method method) { struct Voldermort { Node parent; size_t offset; property { Node front() { return parent.children[offset]; } bool empty() { return offset == parent.children.length; } } void popFront() { offset++; } } return Voldermort(this); } Depth will be a bit of a pain since you'll need to know where you have been at each set of children.
Jun 14 2018
On Thursday, 14 June 2018 at 11:31:50 UTC, Robert M. Münch wrote:Is this possible? I read about Inputranges, took a look at the RBTree code etc. but don't relly know/understand where to start.You can also use opApply to iterate over a tree using foreach, see: https://tour.dlang.org/tour/en/gems/opdispatch-opapply
Jun 14 2018
On 2018-06-14 11:46:04 +0000, Dennis said:On Thursday, 14 June 2018 at 11:31:50 UTC, Robert M. Münch wrote:Ah... that looks very good. Need to dig a bit deeper on how to use it. Thanks. -- Robert M. Münch http://www.saphirion.com smarter | better | fasterIs this possible? I read about Inputranges, took a look at the RBTree code etc. but don't relly know/understand where to start.You can also use opApply to iterate over a tree using foreach, see: https://tour.dlang.org/tour/en/gems/opdispatch-opapply
Jun 14 2018
On 6/14/18 8:35 AM, Robert M. Münch wrote:On 2018-06-14 11:46:04 +0000, Dennis said:Just to clarify, RBTree can easily do DFS without a real stack because there are a finite number of children (2) for each node, and it's an O(1) operation to figure out which child the current node is in relation to the parent (am I a left child or right child?). Now, with your C version, if your children are stored in the array itself, figuring out the "next" child is a matter of doing &this + 1. But if you are storing pointers instead, then figuring out the next child would be an O(n) operation. Using the stack to track where you are (via opApply) is a valid way as well. You could also unroll that into a malloc'd stack, but the code is not as pretty of course. -SteveOn Thursday, 14 June 2018 at 11:31:50 UTC, Robert M. Münch wrote:Ah... that looks very good. Need to dig a bit deeper on how to use it. Thanks.Is this possible? I read about Inputranges, took a look at the RBTree code etc. but don't relly know/understand where to start.You can also use opApply to iterate over a tree using foreach, see: https://tour.dlang.org/tour/en/gems/opdispatch-opapply
Jun 14 2018
On Thursday, 14 June 2018 at 11:31:50 UTC, Robert M. Münch wrote:I have a simple tree C data-structure that looks like this: node { node parent: vector[node] children; } I would like to create two foreach algorthims, one follwing the breadth first search pattern and one the depth first search pattern. Is this possible? I read about Inputranges, took a look at the RBTree code etc. but don't relly know/understand where to start.While it's possible to do with input ranges, it's not pretty and I'm not sure that it's as performant as the traditional method. I would recommend going with one of the other suggestions in this thread.
Jun 14 2018
On Thursday, 14 June 2018 at 11:31:50 UTC, Robert M. Münch wrote:I have a simple tree C data-structure that looks like this: node { node parent: vector[node] children; } I would like to create two foreach algorthims, one follwing the breadth first search pattern and one the depth first search pattern. Is this possible? I read about Inputranges, took a look at the RBTree code etc. but don't relly know/understand where to start.What I found really interesting when reading Ali Çehreli's book 'Programming in D' was using fibers for tree iteration. Check out http://ddili.org/ders/d.en/fibers.html and skip to the section "Fibers in range implementations"
Jun 16 2018