-
Notifications
You must be signed in to change notification settings - Fork 11
Additional functions. #10
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,78 @@ | ||
| 'use strict'; | ||
|
|
||
| function Node(parent, name, data) { | ||
| this.name = name; | ||
| this.data = data; | ||
| if (parent) { | ||
| this.parent = parent; | ||
| parent[name] = this; | ||
| } | ||
| this.child = []; | ||
| } | ||
|
|
||
| function Tree(name, data) { | ||
| this.root = new Node(null, name, data); | ||
| } | ||
|
|
||
| Tree.prototype.visitDepth = function(callback) { | ||
|
|
||
| (function recursive(currNode) { | ||
|
|
||
| let len = currNode.child.length, | ||
|
||
| num; | ||
|
||
| for (num = 0; num < len; ++num) { | ||
| recursive(currNode.child[num]); | ||
| } | ||
|
|
||
| callback(currNode); | ||
| })(this.root); | ||
| }; | ||
|
|
||
| Tree.prototype.isHave = function(callback) { | ||
|
Member
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.visitDepth.call(this, callback); | ||
| }; | ||
|
|
||
| Tree.prototype.addData = function(name, data, whereAdd) { | ||
| let parent = null, | ||
| callback = function(n) { | ||
|
||
| if (n.name === whereAdd) { | ||
| parent = n; | ||
| } | ||
| }; | ||
| this.isHave(callback); | ||
|
|
||
| let node = new Node(parent, name, data); | ||
| if (parent) { | ||
| parent.child.push(node); | ||
| node.parent = parent; | ||
| } else { | ||
| throw new Error('Parent not exist!'); | ||
| } | ||
| }; | ||
|
|
||
|
|
||
| let tree = new Tree('one', 1); | ||
|
|
||
| tree.root.child.push(new Node(tree, 'two', 2)); | ||
|
|
||
| tree.root.child.push(new Node(tree, 'three', 3)); | ||
|
|
||
| tree.root.child.push(new Node(tree, 'four', 4)); | ||
|
|
||
| tree.root.child[0].child.push(new Node(tree.root.child[0], 'five', 5)); | ||
|
|
||
| tree.root.child[0].child.push(new Node(tree.root.child[0], 'six', 6)); | ||
|
|
||
| tree.root.child[2].child.push(new Node(tree.root.child[2], 'seven', 7)); | ||
|
|
||
|
|
||
| tree.isHave(n => { | ||
| if (n.name === 'five') { | ||
| console.dir(n); | ||
| } | ||
| }); | ||
|
|
||
| tree.visitDepth(n => console.dir(n)); | ||
|
|
||
| tree.addData('qwe', 456, 'two'); | ||
| console.dir(tree); | ||
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.
What is a purpose of this IIFE (immediately-invoked function expression) ?
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.
На парі показали такий варіант реалізації - вирішила спробувати, не більше того.