【问题标题】:Tree creation using an array使用数组创建树
【发布时间】:2017-10-16 01:30:47
【问题描述】:

我将从我正在尝试做的事情开始我的问题。我有一个数组(nameList),在我的数组中我持有对象。这些对象由人名(Jack、Jane、James、Daniel 等)和这些人与谁相关的数组(Jack 与 Jane 和 Daniel 等相关)组成。当然,一个人可以与多个人有关系,但两个孩子不能有关系。我想把它们放在一棵树上,我希望这棵树符合这种关系。我想从关系最多的人开始(例如:Daniel 与 7 人有关)。我知道关系最多的人可能不止 1 个。但为了简单起见,我只想说我知道它是谁,我会将它作为 mostRelated 传递。

This is just an example of what I want to do

这是我目前所拥有的。但我不知道如何进一步。

//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;
                  }
            }
        }
    }
  }
}

nameList 看起来像这样

nameList
this.name;
this.relatedTo=[];
this.related=false;
this.checked=false;

【问题讨论】:

  • 为此使用树不是正确的数据结构——如果两个孩子也相关怎么办?您需要使用图表/地图
  • 您的设置无处不在。你不是已经确定谁是谁的父母/孩子吗?为了在已经提供信息的地方制作数据图表,您已经制作了图表。举例说明nameList 的一个元素是什么样的。我可能误解了你的目标。
  • @ControlAltDel,感谢您的建议。但我可以做到让两个孩子没有血缘关系。我已经改变了问题。
  • @Andrew 我已经编辑了我的问题

标签: javascript arrays tree binary-search-tree


【解决方案1】:

这样的?您的问题基本上是要求图形数据结构,但它可能看起来像一棵巧合的树。

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] } ] }

【讨论】:

  • 这是完美的。超过了我的要求。谢谢
猜你喜欢
  • 1970-01-01
  • 2011-05-10
  • 2011-01-08
  • 2015-11-30
  • 2015-08-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-08-22
相关资源
最近更新 更多