【算法】构建二维数据结构(二维数组和图)

84 阅读1分钟
// 二维数组(n行n列)
let arr = new Array(4);
for(let i = 0; i < arr.length; i ++) {
 	arr[i] = new Array(8);
}
console.log(arr) //创建了4行8列的二维数组


// 二维拓朴结构(离散数学中的图)
function Node(value) {
	this.value = value;
	this.neighbor = [];
}
const a = new Node("a");
const b = new Node("b");
const c = new Node("c");
const d = new Node("d");
const e = new Node("e");
const 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);

链表,一维数组,栈,队列都是线性的一维数据结构。二维数据结构有二维数组和二维拓扑结构,拓扑学是一种不研究大小和长度的学科,只考虑关系。

截屏2022-05-16 上午1.25.38.png