• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 2032
  • Last Modified:

How do you create a JSON family tree

Hi All,
I would be very grateful if you could show how to create a JSON style data structure with the following attributes;

The structure would be a recursive structure like this

family ={
 name : "name of person",
 children : [{
    name : "name of person",
    children: [{
      name:
      children:[ ]
   }]
}

 specifically i would like to call an initator function that takes as arguments
the "name of the person" and the family_tree as an object;

so lets say the family tree is something like this
Micheal (Grand Father)
Joseph  (Father)
Junior   (Son)

the function would work something like this
var myfamily_tree = new family_tree()

add_node(myfamilytree,"Micheal");
add_node(myfamilytree,"Joseph");
add_node(myfamilytree,"Junior");

then could i access the tree something like this
myfamilytree.pop() = "Michael"
myfamilytree.pop() = "Joseph"
myfamilytree.pop() = "Junior"

Thanks in advance,
All help is much appreciated
0
JITchallenger
Asked:
JITchallenger
1 Solution
 
leakim971PluritechnicianCommented:
To "start" : http://jsfiddle.net/ZDQef/
    // http://genealogygems.tv/Images/arbol_genealogico_simpsons.png

    function family_tree(name) {
        
        this.createFamily = function(name) {
            this.name = name;
            this.childrens = [];
        }
        
        this.add_node = function(name) {
            return this.childrens[this.childrens.push(new family_tree(name))-1];
        }
        
        this.createFamily(name);
    }

    var f = new family_tree("Simpson");
    var Abraham_ft = f.add_node("Abraham");
    var Homer_ft = Abraham_ft.add_node("Homer");
    Homer_ft.add_node("Bart");
    Homer_ft.add_node("Lisa");
    Homer_ft.add_node("Maggie");

// checking :
    alert( JSON.stringify(f) );

Open in new window

tree.jpg
0
 
JITchallengerAuthor Commented:
Thanks leakim971 for that excellent code
Your definitely the champ here
<a href="http://www.youtube.com/watch?v=h0ivw7jrSm4"> Hey Ha Hoo Hey</a>
0
 
ProculopsisCommented:

// try this which includes your pop functionality:

function familyMember( name ) {

  this.name = name;

  this.children = [];

  this.addChild = function( member ) {
    this.children.push( member );
  };

  this.pop = function( name ) {
    if ( name == this.name ) return this;
    for ( var index = 0; index < this.children.length; index++ ) {
      var child = this.children[index];
      if ( name == child.pop( name ).name ) return child.pop( name );
    }
  };

  this.toString = function() { return JSON.stringify( this ); };

}

var familyTree = new familyMember( "Michael" );
var joseph = new familyMember( "Joseph" );
familyTree.addChild( joseph );

var junior = new familyMember( "Junior" );
joseph.addChild( junior );

alert( familyTree.pop("Junior").toString() );
0
 
leakim971PluritechnicianCommented:
You're welcome!
Have a nice sunday and Happy New Year!

http://jsfiddle.net/ZDQef/1/
this.pop = function() {
            return this.childrens.pop().name;
        }

Open in new window

0
 
GopinathPandianCommented:
Nice
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Get expert help—faster!

Need expert help—fast? Use the Help Bell for personalized assistance getting answers to your important questions.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now