foreach DFS/BFS for tree data-structure?

rikki cattermole rikki at cattermole.co.nz
Thu Jun 14 11:37:53 UTC 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.


More information about the Digitalmars-d-learn mailing list