Last active
August 18, 2019 10:55
-
-
Save zzzze/575d8629e2cbb88062a52193ae25caaf to your computer and use it in GitHub Desktop.
traverseTree
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function traverseTree(tree, context, callback) { | |
callback(context, tree) | |
if (!tree.children) { | |
return 1 | |
} | |
let subtreeCount = 1 | |
tree.children.forEach(child => { | |
subtreeCount += traverseTree(child, context, callback) | |
}) | |
return subtreeCount | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
let tree = { | |
name: '1', | |
children: [ | |
{ | |
name: '1-1' | |
}, | |
{ | |
name: '1-2', | |
children: [ | |
{ | |
name: '1-2-1', | |
}, | |
{ | |
name: '1-2-2', | |
} | |
], | |
}, | |
{ | |
name: '1-3' | |
}, | |
], | |
} | |
function traverseTree(tree, context, callback) { | |
callback(context, tree) | |
if (!tree.children) { | |
return 1 | |
} | |
let subtreeCount = 1 | |
tree.children.forEach(child => { | |
subtreeCount += traverseTree(child, context, callback) | |
}) | |
return subtreeCount | |
} | |
function mapSingleChild(bookKeeping, tree) { | |
let {result, func} = bookKeeping | |
result.push(func(tree, bookKeeping.count++)) | |
} | |
function map(tree, callback) { | |
let result = [] | |
let context = { | |
result, | |
func: callback, | |
count: 0, | |
} | |
traverseTree(tree, context, mapSingleChild) | |
return result | |
} | |
function forEach(tree, callback) { | |
let context = { | |
result: null, | |
func: callback, | |
count: 0, | |
} | |
traverseTree(tree, context, (bookKeeping, tree) => bookKeeping.func(tree)) | |
} | |
function count(tree) { | |
let context = { | |
result: null, | |
func: () => null, | |
count: 0, | |
} | |
return traverseTree(tree, context, () => null) | |
} | |
function reduceSingleChild(bookKeeping, child) { | |
let {result, func} = bookKeeping | |
bookKeeping.result = func(result, child, bookKeeping.count++) | |
} | |
function reduce(tree, callback, initialValue) { | |
let context = { | |
result: initialValue, | |
func: callback, | |
count: 0, | |
} | |
traverseTree(tree, context, reduceSingleChild) | |
return context.result | |
} | |
console.log(count(tree)) | |
forEach(tree, item => { | |
console.log(item.name) | |
}) | |
console.log(map(tree, (item, index) => { | |
return `${item.name} --- ${index}` | |
})) | |
console.log(reduce(tree, (accumulator, currentValue, index) => { | |
if (!/-2/.test(currentValue.name)) { | |
accumulator.push(currentValue.name + 'xxx') | |
} | |
return accumulator | |
}, [])) | |
console.log(reduce(tree, (accumulator, currentValue, index) => { | |
accumulator[`item-${index}`] = currentValue.name | |
return accumulator | |
}, {})) | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function traverseTree(tree, context, callback) { | |
let queue = [tree] | |
while (queue.length) { | |
let currentNode = queue.shift() | |
context.count++ | |
callback(context, currentNode) | |
if (currentNode.children) { | |
queue = [].concat(currentNode.children, queue) | |
} | |
} | |
return context.count | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function traverseTree(tree, context, callback) { | |
let queue = [tree] | |
while (queue.length) { | |
let currentNode = queue.shift() | |
context.count++ | |
callback(context, currentNode) | |
if (currentNode.children) { | |
queue = queue.concat(currentNode.children | |
} | |
} | |
return context.count | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment