We closed this forum 18 June 2010. It has served us well since 2005 as the ALPHA forum did before it from 2002 to 2005. New discussions are ongoing at the new URL http://forum.processing.org. You'll need to sign up and get a new user account. We're sorry about that inconvenience, but we think it's better in the long run. The content on this forum will remain online.
IndexProgramming Questions & HelpPrograms › Help with tree data structure
Page Index Toggle Pages: 1
Help with tree data structure (Read 743 times)
Help with tree data structure
Feb 16th, 2010, 8:34am
 
Hi,
I would like to add function which counts number of tree nodes and leaves.
This is my implementation of quadtree structure:

Code:

class Node {
public int x,y,size;
public Node[] child = new Node[4];
}

public Node createTree() {
return _createTree(0, 0, 0);
}

private Node _createTree(int x, int y, int level) {
int size = img.width >> level;

Node node = new Node();
node.x = x;
node.y = y;
node.size = size;

if (size > 1) {
size = img.width >> ++level;
node.child[0] = _createTree(x, y, level);
node.child[1] = _createTree(x+size, y, level);
node.child[2] = _createTree(x, y+size, level);
node.child[3] = _createTree(x+size, y+size, level);
}

return node;
}


please leave any examples of method counting nodes of that tree.
thx
Re: Help with tree data structure
Reply #1 - Feb 16th, 2010, 9:53am
 
Add a counter to increment after Node node = new Node();
Re: Help with tree data structure
Reply #2 - Feb 16th, 2010, 11:44am
 
Yes i know, but i would like to have a method inside Node class which returns noumber of tree nodes.
Is this possible ? or better create this function outside class
hmm?
Re: Help with tree data structure
Reply #3 - Feb 16th, 2010, 12:31pm
 
The Node class isn't a good place for this, since the tree is made of Nodes, they don't have to know they are part of a tree...
You should make a Tree class which would hold all the nodes and keep track of their number.
Page Index Toggle Pages: 1