recursive data structure
in
Programming Questions
•
2 years ago
hi all,
i'm working on creating a Node object that stores data of its parent as well as its children (which is a flexible amount, from 0 to X). using a recursive method data is categorized into the Nodes. for this initial test i'm working with the most basic concept, a family tree.
marcus (child)
now i really don't want to make a family tree (actually trying to show the relationship/break down of a website). but for simplicity's sake i'm starting off with this example. once i figure out how to structure the data i want to give Nodes PVector behaviors -- these Nodes can be drawn into a typical tree diagram format OR change their display according to commonalities (such as representing a hereditary trait -- Nodes that have such trait move to the center of the screen, while relatives reshape around them).
i've made some serious progress with the insertNode() recursive method -- but am running into some problems.
1. insertNode() skips over "christian", "rosemary", "beatrice" from the 2nd line of input data (i believe this is because depth is set to 3 after running through kimi in the previous input line). so it goes straight to depth 3 in the second line (ie "marcus"). if i reset d to 0, insertNode() runs through everyone again and makes 2 christians, versus verifying he's already there and moving on.
2. there's is something really strange going on after "marcus" is put in (you can see from the printlns)
i think i'm getting closer, but this recursion is very tricky to wrap my head around.
if anyone has any suggestions i'd be very grateful.
thank you!
i'm working on creating a Node object that stores data of its parent as well as its children (which is a flexible amount, from 0 to X). using a recursive method data is categorized into the Nodes. for this initial test i'm working with the most basic concept, a family tree.
christian (great grandparent)
dori (grandparent) rosemary (grandparent)
greg (parent) beatrice (parent)
kimi (child)
now i really don't want to make a family tree (actually trying to show the relationship/break down of a website). but for simplicity's sake i'm starting off with this example. once i figure out how to structure the data i want to give Nodes PVector behaviors -- these Nodes can be drawn into a typical tree diagram format OR change their display according to commonalities (such as representing a hereditary trait -- Nodes that have such trait move to the center of the screen, while relatives reshape around them).
i've made some serious progress with the insertNode() recursive method -- but am running into some problems.
1. insertNode() skips over "christian", "rosemary", "beatrice" from the 2nd line of input data (i believe this is because depth is set to 3 after running through kimi in the previous input line). so it goes straight to depth 3 in the second line (ie "marcus"). if i reset d to 0, insertNode() runs through everyone again and makes 2 christians, versus verifying he's already there and moving on.
2. there's is something really strange going on after "marcus" is put in (you can see from the printlns)
i think i'm getting closer, but this recursion is very tricky to wrap my head around.
if anyone has any suggestions i'd be very grateful.
thank you!
- // each Node knows that its name and who its children are
// test data for Node class (real data will be parsed from .csv file)
PVector randomVec = new PVector (random (400), random (400));
// initialize depth value
int d = 0;
// this will later be pulled from the .csv file
String[] testLine = {
"christian", "dori", "greg", "kimi",
};
String[] testLine1 = {
"christian", "rosemary", "beatrice", "marcus"
};
Tree tree;
Node node;
// test root node
String[] rootA = {
"root"
};
PFont font;
void setup () {
size (400, 400);
background (0);
//font = loadFont ("SansSerif-14.vlw");
smooth();
// tree = new Tree();
node = new Node (rootA, randomVec);
node.insertNode(testLine);
// d = 0;
node.insertNode(testLine1);
}
void draw () {
}
- // Node class keeps track of
// 1. its parent
// 2. its children
class Node {
PVector location;
String label;
int depth = 0;
ArrayList <Node> children;
Node (String[] label_, PVector loc) {
label = label_[d];
location = loc;
// its depth value
depth = d;
// a particular Node's children
children = new ArrayList <Node> ();
}
void insertNode (String[] testN)
{
println("DEPTH:: " + d);
Node newNode = new Node(testN, randomVec);
if (children.size() == 0) {
println("New Node:: " + newNode.label); // AT A GIVEN DEPTH, D=0 AT FIRST
children.add(newNode);
println (children.get(0).label);
// run through the children array
for (int j = 0; j < children.size(); j++)
{
if (children.get(j).label == newNode.label)
{
if (d < testN.length-1) {
d++;
children.get(j).insertNode(testN); // inserts the children into a parent
}
}
}
}
// otherwise (if there is something in the children array)
else {
for (int j = 0; j < children.size(); j++)
{
// PROBLEM IS HERE
println ("TEST CHILDREN ARRAY:::::: " + children.get(j).label);
println ("NEW LABEL::::::: " + newNode.label);
println ("NEW DEPTH::::::::::: " + d); // depth = 3 (has been pushed up from previous loop)
if (children.get(j).label == newNode.label)
{
println (children.get(j).label + " HIHO");
// children.get(j).add(newNode);
if (d < testN.length-1 )
{
children.get(j).insertNode(testN);
}
}
else {
println(newNode.label + " HI");
children.add(newNode);
for (int k = 0; k < children.size(); k++)
{
println ("test");
if (children.get(k).label == newNode.label)
{
if (d < testN.length-1 )
{
children.get(k).insertNode(testN);
}
}
}
}
}
}
}
}
- class Tree {
Node root;
color col = color (0, 255, 0);
Tree () {
root = null;
}
void insertNode (String[] data) {
// root.insertNode(data, 0);
}
}
1