-
Notifications
You must be signed in to change notification settings - Fork 11
Implemented tree using classes, additional functionality #8
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
'use strict'; | ||
|
||
class Node { | ||
|
||
constructor(parent, name, data) { | ||
this.name = name; | ||
this.data = data; | ||
this.childs = []; | ||
if (parent) { | ||
this.parent = parent; | ||
parent.childs.push(this); | ||
} | ||
} | ||
|
||
findAll(name) { // Find all nodes with specified name | ||
let arr = []; | ||
if (this.name === name) arr.push(this); | ||
for (let n of this.childs) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Тут ходить по массиву обычным циклом, for..of пока медленный |
||
arr = arr.concat(n.findAll(name)); | ||
} | ||
|
||
return arr; | ||
} | ||
|
||
findFirst(name) { //Find first instance | ||
if (this.name === name) return this; | ||
for (let n of this.childs) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. тоже без for..of лучше избавиться, а переменную содавать до цикла:
это работает быстрее |
||
return n.findFirst(name); | ||
} | ||
} | ||
|
||
find(name, callback) { //Call function for each found node | ||
const nodes = this.findAll(name); | ||
if (nodes.length > 0) | ||
for (let n of this.findAll(name)) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. со скобками {} будет лучше и if и for |
||
callback(n); | ||
} | ||
|
||
setParent(newParent) { // Sets new parent for this node | ||
this.parent.childs.pop(this.parent.childs.indexOf(this)); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Это таб? |
||
this.parent = newParent; | ||
this.parent.childs.push(this); | ||
} | ||
} | ||
|
||
const root = new Node(null, 'root', {}); | ||
const n1 = new Node(root, 'searched', {}); | ||
const n2 = new Node(root, 'n2', {}); | ||
const n12 = new Node(n1, 'searched', {}); | ||
|
||
|
||
console.log('findAll("searched") -', root.findAll('searched')); | ||
console.log('findFirst("searched") -', root.findFirst('searched')); | ||
root.find('n2', n => { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Вот тут {} можно опустить |
||
console.log('parent - ', n.parent); | ||
}); | ||
n12.setParent(root); | ||
console.log('n12 new parent -', n12.parent); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
const