Karim Manaouil
Karim Manaouil

Reputation: 1249

how to implement a tree structure in javascript in which a node contains 2 arrays inside an array?

So, the thing is I'm not really good at Javascript (I only know some C programming) & i want to implement a tree structure in which the node contains 2 arrays inside an array.

I could help you by giving structures in C:

struct IndexArray {
   void *DataArray;
   void *ChildsArray;
}

struct Node {
   struct IndexArray *ContainerArray;
}


In short words the node contains an array & each element of this array contains 2 sub arrays.

I searched for tree implementations in javascript & i found many examples but i couldn't find an implementation similar to this one.

Upvotes: 0

Views: 51

Answers (1)

guest271314
guest271314

Reputation: 1

You can create an Array in javascript using Array() constructor or array literal []

function _Node(a, b) {
  this.IndexArray = [[a], [b]];
}

var node = new _Node(1, 2);
var i = 1;
console.log(node.IndexArray[i]);

Alternatively

class _Node {
  constructor (a = 1, b = 2) {
    this.IndexArray = [[a], [b]];
  }
}

var node = new _Node(1);
var i = 1;
console.log(node.IndexArray[i]);

Upvotes: 1

Related Questions