-
Notifications
You must be signed in to change notification settings - Fork 0
/
64-tree-directory.js
25 lines (22 loc) · 1.03 KB
/
64-tree-directory.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
/*
Your task, should you choose to accept it, is to create two functions:
one that traverses a tree and returns a directory of that tree and one
that traverses a binary search tree (BST) and returns a directory of that
BST. The directory should list each node and its location in the tree;
the order of the nodes listed in the directory does not matter.
Input: tree or binary search tree (BST)
Output: object (a directory)
Constraints: 1 <= number of nodes in tree <= 250
Edge Cases: Don't worry about dealing with empty trees, balancing, or duplicate values
*/
const createTreeDirectory = (tree, location = []) => (!tree.children.length ?
{ [tree.value]: location } :
{
[tree.value]: location,
...tree.children.reduce((acc, node, index) =>
({ ...acc, ...createTreeDirectory(node, [...location, index]) }), {}),
});
const createBSTDirectory = (bst, location = []) =>
['left', 'right'].reduce((acc, side) => (bst[side] ?
{ ...acc, ...createBSTDirectory(bst[side], [...location, side]) } :
acc), { [bst.value]: location });