tree diagram
in
Programming Questions
•
2 years ago
new to processing and would really appreciate any help
i'm stumped on the logic of a sketch i'm trying to do -- basically a simple data viz of a family tree. i'm creating a Node class that should recognize its parent and its children -- basically the point i'm confused about is how a Node recognizes who its parent is as well as its children.
one idea is using depth values to categorize the level of the Node in the family tree, another is to create nested HashMaps with each parent as a key to the children (i feel like this is a clunky way to go).
ultimately, i want each Node to be a discrete particle with PVector aspects so i can inform it where to go on the screen (but it always know its order in the family hierarchy).
if you have any suggestions re how a Node can distinguish itself from its children and parent i would be very appreciative.
thank you again for any time you can spare!
(also attached in the sketch is a sample .csv file i will use to parse the data from, which i have some code for the parseData() method but am manually implementing test data points in the sketch while i figure out this Node class problem)
- // each category is a Node object
- // 1. great grandparent
- // 2. grandparent
- // 3. parent
- // 4. child
- // each Node knows that its parent and its children
- // do we need to use hashMaps?
- // should each Node store it's own data class?
- // how?
- /*
- HashMap <String, ArrayList <String>> greatGPhash = new HashMap <String, ArrayList <String>> ();
- HashMap <String, ArrayList <String>> GPhash = new HashMap <String, ArrayList <String>> ();
- HashMap <String, ArrayList <String>> parenthash = new HashMap <String, ArrayList <String>> ();
- ArrayList <Node> childArray = childArray = new ArrayList ();
- */
- // Table dataTable;
- // int rowCount;
- Tree tree;
- // this will later be an ArrayList pulled from parseData(familyTree.csv)
- Node root, grandparent, parent, parent1, child, child1, child2, grandparent2, parent2;
- // all data points
- ArrayList <Node> data = new ArrayList <Node> ();
- // all Nodes
- ArrayList <Node> allNodes = new ArrayList <Node> ();
- void setup () {
- size (400, 400);
- background (0);
- // dataTable = new Table ("familyTree.csv");
- // rowCount = dataTable.getRowCount();
- // parseData();
- tree = new Tree();
- // test data for Node class (real data will be parsed from .csv file)
- PVector randomVec = new PVector (random (width), random (height));
- PVector randomVec2 = new PVector (random (10), random (10));
- PVector randomVec3 = new PVector (random (300), random (300));
- PVector randomVec4 = new PVector (random (400), random (400));
- PVector randomVec5 = new PVector (random (200), random (200));
- PVector randomVec6 = new PVector (random (350), random (280));
- PVector randomVec7 = new PVector (random (400), random (100));
- PVector randomVec8 = new PVector (random (100), random (400));
- // Node (String name, PVector location, int depth);
- // is manually keeping track of a depth in the way to do this?
- // or should the Nodes be keeping track of their own children?
- root = new Node ("Christian", randomVec, 0);
- data.add( grandparent = new Node ("Dori", randomVec2, 1));
- grandparent2 = new Node ("Rosemary", randomVec2, 1);
- parent = new Node ("Greg", randomVec3, 2);
- parent1 = new Node ("Donald", randomVec4, 2);
- parent2 = new Node ("Beatrice", randomVec5, 2);
- child = new Node ("Allen", randomVec6, 3);
- child1 = new Node ("Kimi", randomVec7, 3);
- child2 = new Node ("James", randomVec8, 3);
- // test insertNode()
- // this would later by an ArrayList of data (Nodes) being passed into the tree
- /*
- for (Node n: data) {
- tree.insertNode (n);
- }
- */
- tree.insertNode (root);
- tree.insertNode (grandparent);
- tree.insertNode (grandparent2);
- tree.insertNode (parent);
- tree.insertNode (parent1);
- tree.insertNode (parent2);
- tree.insertNode (child);
- tree.insertNode (child1);
- tree.insertNode (child2);
- }
- void draw () {
- for (Node n: allNodes) {
- n.display();
- }
- tree.displayRoot();
- }
- // Node class keeps track of
- // 1. its parent
- // 2. its children
- class Node {
- PVector location;
- String label;
- // keep track of depth?
- int depth;
- // set to red, later colors will be determined by Node type/level
- color col = color (255, 0, 0);
- // a local ArrayList of a particular Node's children
- ArrayList <Node> children;
- Node (String label_, PVector loc, int d) {
- location = loc;
- label = label_;
- depth = d;
- children = new ArrayList <Node> ();
- }
- void display () {
- noStroke ();
- fill (col);
- ellipse (location.x, location.y, 5, 5);
- // draw a line between the parent and its child nodes (is this right?)
- for (int i = 0; i < children.size(); i++) {
- line (location.x, location.y, children.get(i).location.x, children.get(i).location.y);
- }
- }
- void insertNode (Node n) {
- // inserted Node is nested in a parent node
- // and has child nodes
- // HOW?
- // need to compare it to the previous and last nodes... of the children array?
- // every inserted Node is added to the global allNodes ArrayList
- allNodes.add (n);
- // nodes distinguished by depth value (this the right way to do it?)
- if (n.depth == 1) {
- println (n.label);
- }
- }
- }
- class Tree {
- Node root;
- color col = color (0, 255, 0);
- Tree () {
- root = null;
- }
- void insertNode (Node n) {
- Node newNode = n;
- if (root == null) {
- root = newNode;
- // println (root.label);
- }
- else {
- root.insertNode (newNode);
- }
- }
- // display root in green at center of screen
- void displayRoot () {
- noStroke ();
- fill (col);
- ellipse (width/2, height/2, 5, 5);
- }
- }
#great grandparent,grandparent,parent,child
Christian,Dori,Greg,Kimi
Christian,Dori,Greg,Allen
Christian,Dori,Donald,James
Christian,Rosemary,Beatrice,
1