2017-10-16 153 views
-1

我將以我想要做的事開始我的問題。我有一個數組(名稱列表),在我的數組中我持有對象。這些對象由人的姓名(傑克,簡,詹姆斯,丹尼爾等)以及這些人相關的數組組成(傑克與丹尼爾等有關)。當然,一個人可以與多於一個人有關,但兩個孩子不能關聯。我想把它們留在一棵樹中,我希望這棵樹能夠根據關係。我想從與關係最密切的人開始(例如:丹尼爾與7人有關)。我知道可能有超過1人的關係最密切。但爲了簡單的問題,我問,讓我們只是說我知道它是誰,我會通過它作爲mostRelated。使用數組創建樹

This is just an example of what I want to do

這是我有這麼far.But我不知道如何進一步它。

//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 
this.name; 
this.relatedTo=[]; 
this.related=false; 
this.checked=false; 
+2

爲此使用樹不是正確的數據結構 - 如果兩個孩子也有關係?您需要使用圖表/地圖 – ControlAltDel

+0

您的設置已遍佈全球。你不是已經確定誰是誰的父母/孩子嗎?爲了在已經提供信息的地方繪製數據圖,您已經制作了一張圖。舉一個'nameList'的一個元素的例子。我可能會誤解你的目標。 – Andrew

+0

@ControlAltDel,感謝您的建議。但我可以讓2個孩子不相關。我改變了這個問題。 – JJD

回答

0

像這樣的事情?你的問題從根本上要求一個圖形數據結構,但它巧合地看起來像一棵樹。

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

這很完美。不僅僅是我所要求的。謝謝 – JJD