Javascript 使用数组创建树
我将从我想做的事情开始我的问题。我有一个数组(名字列表),在我的数组中我持有对象。这些对象由人名(杰克、简、詹姆斯、丹尼尔等)和这些人的亲属(杰克与简、丹尼尔等有亲属关系)组成。当然,一个人可以与不止一个人有亲属关系,但两个孩子不能有亲属关系。我想把它们放在一棵树上,我想让这棵树符合这种关系。我想从关系最密切的人开始(例如:丹尼尔与7个人有亲戚关系)。我知道可能会有不止一个人拥有最多的关系。但为了简单起见,我要问的问题是,让我们说我知道是谁,我会把它作为最相关的信息传递给大家 这是我到目前为止所拥有的,但我不知道如何进一步发展Javascript 使用数组创建树,javascript,arrays,tree,binary-search-tree,Javascript,Arrays,Tree,Binary Search Tree,我将从我想做的事情开始我的问题。我有一个数组(名字列表),在我的数组中我持有对象。这些对象由人名(杰克、简、詹姆斯、丹尼尔等)和这些人的亲属(杰克与简、丹尼尔等有亲属关系)组成。当然,一个人可以与不止一个人有亲属关系,但两个孩子不能有亲属关系。我想把它们放在一棵树上,我想让这棵树符合这种关系。我想从关系最密切的人开始(例如:丹尼尔与7个人有亲戚关系)。我知道可能会有不止一个人拥有最多的关系。但为了简单起见,我要问的问题是,让我们说我知道是谁,我会把它作为最相关的信息传递给大家 这是我到目前为止
//my array of names is nameList
//to check who they are related to nameList.relatedTo
function Node(names) {
this.data = names;
this.parent = null;
this.children = [];
}
function CreateTree(nameList, mostRelated)
{
this._root=mostLinked;
for(var i=0; i < nameList[i].length;i++)
{
node= new Node(nameList[i]);
if(nameList[i].isChecked!)//if already add to the tree
{
if(nameList[i].isRelated)//to check if they have any relation
{
for(var j=0; i < nameList[i].relatedTo[j].length;j++)
{
if(nameList[i].relatedTo.isChecked!)
{
nameList[i]=Node.parent;
Node.children.push(nameList[i].relatedTo[j]);
nameList[i].isChecked=true;
}
}
}
}
}
}
像这样的?您的问题基本上是要求一个图形数据结构,但它可能看起来像一棵树
function Person(name) {
this.name = name
this.relatedTo = []
}
function Graph(familyArr) {
this._familyArr = nameList.sort((a, b) => {
return b.relatedTo.length - a.relatedTo.length //reverse sort by relatedTo.length
})
const familyObj = {}
this._familyArr.forEach(({name}) => {
familyObj[name] = new Person(name) //build object of name-person object key-val pairs
})
this.head = familyObj[this._familyArr[0].name] //graphs don't have heads, but they can by 'coincidence'
this.family = familyObj // actual tree
this._children = Object.assign({}, this.family) //copies of children to keep proper track of them while building this.family
}
Graph.prototype.addRelative = function(parent, child) {
this.family[parent].relatedTo.push(this._children[child])
// console.log(this.family);
return this
}
Graph.prototype.buildGraph = function() {
this._familyArr.forEach(parent => {
parent.relatedTo.forEach(child => {
this.addRelative(parent.name, child)
})
})
}
Graph.prototype.find = function(name) {
return this.family[name]
}
const john = {name: 'john', relatedTo: ['jane']}
const jane = {name: 'jane', relatedTo: ['john']}
const jack = {name: 'jack', relatedTo: ['andrew', 'jane']}
const andrew = {name: 'andrew', relatedTo: ['jane', 'john']}
const nameList = [john, jane, jack, andrew]
const graph = new Graph(nameList)
graph.buildGraph()
console.log(graph.find('john'))
// Person {
// name: 'john',
// relatedTo: [ Person { name: 'jane', relatedTo: [Object] } ] }
console.log(graph.find('andrew'));
// Person {
// name: 'andrew',
// relatedTo:
// [ Person { name: 'jane', relatedTo: [Object] },
// Person { name: 'john', relatedTo: [Object] } ] }
console.log(graph.head)
// Person {
// name: 'jack',
// relatedTo:
// [ Person { name: 'andrew', relatedTo: [Object] },
// Person { name: 'jane', relatedTo: [Object] } ] }
为此使用树不是正确的数据结构-如果两个子项也相关怎么办?你需要使用图形/地图你的设置到处都是。你不是已经确定谁是谁的父母/孩子了吗?为了在已经给出信息的地方绘制数据图表,您已经绘制了图表。请举例说明
名称列表
的一个元素是什么样的。我可能误解了你的目标。@ControlAltDel,谢谢你的建议。但我可以让两个孩子没有亲戚关系。我已经修改了问题。@安德鲁,我已经编辑了我的问题,这太完美了。比我要的还多。非常感谢。
function Person(name) {
this.name = name
this.relatedTo = []
}
function Graph(familyArr) {
this._familyArr = nameList.sort((a, b) => {
return b.relatedTo.length - a.relatedTo.length //reverse sort by relatedTo.length
})
const familyObj = {}
this._familyArr.forEach(({name}) => {
familyObj[name] = new Person(name) //build object of name-person object key-val pairs
})
this.head = familyObj[this._familyArr[0].name] //graphs don't have heads, but they can by 'coincidence'
this.family = familyObj // actual tree
this._children = Object.assign({}, this.family) //copies of children to keep proper track of them while building this.family
}
Graph.prototype.addRelative = function(parent, child) {
this.family[parent].relatedTo.push(this._children[child])
// console.log(this.family);
return this
}
Graph.prototype.buildGraph = function() {
this._familyArr.forEach(parent => {
parent.relatedTo.forEach(child => {
this.addRelative(parent.name, child)
})
})
}
Graph.prototype.find = function(name) {
return this.family[name]
}
const john = {name: 'john', relatedTo: ['jane']}
const jane = {name: 'jane', relatedTo: ['john']}
const jack = {name: 'jack', relatedTo: ['andrew', 'jane']}
const andrew = {name: 'andrew', relatedTo: ['jane', 'john']}
const nameList = [john, jane, jack, andrew]
const graph = new Graph(nameList)
graph.buildGraph()
console.log(graph.find('john'))
// Person {
// name: 'john',
// relatedTo: [ Person { name: 'jane', relatedTo: [Object] } ] }
console.log(graph.find('andrew'));
// Person {
// name: 'andrew',
// relatedTo:
// [ Person { name: 'jane', relatedTo: [Object] },
// Person { name: 'john', relatedTo: [Object] } ] }
console.log(graph.head)
// Person {
// name: 'jack',
// relatedTo:
// [ Person { name: 'andrew', relatedTo: [Object] },
// Person { name: 'jane', relatedTo: [Object] } ] }