This is the 7th day of my participation in Gwen Challenge

2 d array

The essence of a two-dimensional array is that every element in the array is an array. Var arr = [[],[]]; This is just a two-dimensional array.

As shown here, how do we declare a two-dimensional array with four rows and eight columns?

Code implementation

var arr = new Array(4);
for (var i = 0; i < arr.length; i++){
    arr[i] = new Array(8);
}
Copy the code

Two-dimensional topology (graph)

Topology is a science that does not study size and length, but only relationships.

A graph is a collection of interconnected nodes.

The code implements the graph structure

fuction Node(value){
    this.value = value;
    this.neighbor = [];
}

var a = new Node("a");
var b = new Node("b");
var c = new Node("c");
var d = new Node("d");
var e = new Node("e");
var f = new Node("f");

a.neighbor.push(b);
a.neighbor.push(c);
a.neighbor.push(f);

b.neighbor.push(a);
b.neighbor.push(d);
b.neighbor.push(e);

c.neighbor.push(a);

d.neighbor.push(b);

e.neighbor.push(b);
Copy the code

That’s how the graph is constructed.

A tree structure

The tree structure is just like the folders that we normally use, you open up c and there’s a bunch of folders, and then you open up another bunch of folders, and then you keep going down, and then you open up another bunch of folders. We can see that the tree opens down gradually. The tree structure has a root node. Tree structure is no loop, tree is a kind of graph, tree is also called directed acyclic graph.

As shown in the figure above, the root node of the tree is A.

Leaf node: There are no other nodes below. As shown in the figure above, the leaf nodes of this tree are C,F,D, and E

Node: A normal node that is neither root nor leaf.

Degree of tree: The degree of tree is as many forks as the node with the most crosses. As shown in the figure above, the node with the most crosses in the tree is A, and A has three crosses, so the degree of the tree is 3.

Tree depth: The depth of a tree is the deepest level. As shown in the figure above, the tree has 3 layers, so the depth of the tree is 3.