-
Notifications
You must be signed in to change notification settings - Fork 4
/
Tree2.js
180 lines (167 loc) · 7.33 KB
/
Tree2.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
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
// NOTE: the search func can be used as walk/traverse, just not return true during search
// NOTE: the `initialNode` will not appear in search, only below node will
const SEARCH_END = Symbol('TREE:SEARCH-END') // end and return current node, all support: DFS/BFS/BUS
const SEARCH_SKIP = Symbol('TREE:SEARCH-SKIP') // skip branch below current node, support: DFS/BFS, not support: BUS
const createTree2DepthFirstSearch = (getSubNodeListFunc) => { // ([ node, level, ... ], extra) => [ [ node, level + 1, ... ] ] // or return undefined to end branch
const unshiftStack = (stack, node, extra) => {
const nodeList = getSubNodeListFunc(node, extra)
Array.isArray(nodeList) && stack.unshift(...nodeList)
}
return (
initialNode, // [ initialNode, 0 ]
func, // (node) => true/false // return true to end search
extra // optional, some tree-like structure need to get subNodeList from extra outer data
) => {
const stack = [] // [ node, node, ... ]
unshiftStack(stack, initialNode, extra)
let node
while ((node = stack.shift())) {
const output = func(node)
if (output === SEARCH_END) return node
if (output === SEARCH_SKIP) continue
unshiftStack(stack, node, extra)
}
}
}
const createTree2DepthFirstSearchAsync = (getSubNodeListFunc) => { // async ([ node, level, ... ], extra) => [ [ node, level + 1, ... ] ] // or return undefined to end branch
const unshiftStackAsync = async (stack, node, extra) => {
const nodeList = await getSubNodeListFunc(node, extra)
Array.isArray(nodeList) && stack.unshift(...nodeList)
}
return async (
initialNode, // [ initialNode, 0 ]
func, // async (node) => true/false // return true to end search
extra // optional, some tree-like structure need to get subNodeList from extra outer data
) => {
const stack = [] // [ node, node, ... ]
await unshiftStackAsync(stack, initialNode, extra)
let node
while ((node = stack.shift())) {
const output = await func(node)
if (output === SEARCH_END) return node
if (output === SEARCH_SKIP) continue
await unshiftStackAsync(stack, node, extra)
}
}
}
const createTree2BreadthFirstSearch = (getSubNodeListFunc) => { // ([ node, level, ... ], extra) => [ [ node, level + 1, ... ] ] // or return undefined to end branch
const pushStack = (stack, node, extra) => {
const nodeList = getSubNodeListFunc(node, extra)
Array.isArray(nodeList) && stack.push(...nodeList)
}
return (
initialNode, // [ initialNode, 0 ]
func, // (node) => true/false // return true to end search
extra // optional, some tree-like structure need to get subNodeList from extra outer data
) => {
const stack = []
pushStack(stack, initialNode, extra)
let node
while ((node = stack.shift())) {
const output = func(node)
if (output === SEARCH_END) return node
if (output === SEARCH_SKIP) continue
pushStack(stack, node, extra)
}
}
}
const createTree2BreadthFirstSearchAsync = (getSubNodeListFunc) => { // async ([ node, level, ... ], extra) => [ [ node, level + 1, ... ] ] // or return undefined to end branch
const pushStackAsync = async (stack, node, extra) => {
const nodeList = await getSubNodeListFunc(node, extra)
Array.isArray(nodeList) && stack.push(...nodeList)
}
return async (
initialNode, // [ initialNode, 0 ]
func, // async (node) => true/false // return true to end search
extra // optional, some tree-like structure need to get subNodeList from extra outer data
) => {
const stack = [] // [ node, node, ... ]
await pushStackAsync(stack, initialNode, extra)
let node
while ((node = stack.shift())) {
const output = await func(node)
if (output === SEARCH_END) return node
if (output === SEARCH_SKIP) continue
await pushStackAsync(stack, node, extra)
}
}
}
const createTree2BottomUpSearch = (getSubNodeListFunc) => { // ([ node, level, ... ], extra) => [ [ node, level + 1, ... ] ] // or return undefined to end branch
const pushPairStack = (pairStack, node, extra) => {
const nodeList = getSubNodeListFunc(node, extra)
return Boolean(Array.isArray(nodeList) && pairStack.push([ node, nodeList ])) // has subNodeList
}
return (
initialNode, // [ initialNode, 0 ]
func, // (node) => true/false // return true to end search
extra // optional, some tree-like structure need to get subNodeList from extra outer data
) => {
const pairStack = [] // [ [ node, subNodeList ], [ node, subNodeList ], ... ]
pushPairStack(pairStack, initialNode, extra)
while (pairStack.length !== 0) {
const [ node, subNodeList ] = pairStack[ pairStack.length - 1 ]
if (subNodeList.length === 0) {
pairStack.pop()
if (pairStack.length !== 0 && (func(node) === SEARCH_END)) return node // skip initial node
} else {
const subNode = subNodeList.shift()
const hasSubNodeList = pushPairStack(pairStack, subNode, extra)
if (!hasSubNodeList && (func(subNode) === SEARCH_END)) return subNode
}
}
}
}
const createTree2BottomUpSearchAsync = (getSubNodeListFunc) => { // async ([ node, level, ... ], extra) => [ [ node, level + 1, ... ] ] // or return undefined to end branch
const pushPairStackAsync = async (pairStack, node, extra) => {
const nodeList = await getSubNodeListFunc(node, extra)
return Boolean(Array.isArray(nodeList) && pairStack.push([ node, nodeList ])) // has subNodeList
}
return async (
initialNode, // [ initialNode, 0 ]
func, // async (node) => true/false // return true to end search
extra // optional, some tree-like structure need to get subNodeList from extra outer data
) => {
const pairStack = [] // [ [ node, subNodeList ], [ node, subNodeList ], ... ]
await pushPairStackAsync(pairStack, initialNode, extra)
while (pairStack.length !== 0) {
const [ node, subNodeList ] = pairStack[ pairStack.length - 1 ]
if (subNodeList.length === 0) {
pairStack.pop()
if (pairStack.length !== 0 && (await func(node) === SEARCH_END)) return node // skip initial node
} else {
const subNode = subNodeList.shift()
const hasSubNodeList = await pushPairStackAsync(pairStack, subNode, extra)
if (!hasSubNodeList && (await func(subNode) === SEARCH_END)) return subNode
}
}
}
}
const prettyStringifyTree2Node = (
getSubNodeLevelHasMoreListFunc, // ([ node, level, hasMore ], extra) => [ [ subNode, level + 1, subIndex !== length - 1 ] ]
initialNode, // [ initialNode, -1, false ]
func, // (prefix, node) => resultList.push(`${prefix}${node}`)
extra // optional, some tree-like structure need to get subNodeList from extra outer data
) => {
const studList = []
createTree2DepthFirstSearch(getSubNodeLevelHasMoreListFunc)(
initialNode, // [ initialNode, -1, false ]
([ node, level, hasMore ]) => { // NOTE: hasMore = has more sibling-node
studList.length = level
if (level !== 0) studList[ level - 1 ] = hasMore ? '├─ ' : '└─ '
func(studList.join(''), node)
if (level !== 0) studList[ level - 1 ] = hasMore ? '│ ' : ' '
},
extra
)
}
export {
SEARCH_END,
SEARCH_SKIP,
createTree2DepthFirstSearch,
createTree2DepthFirstSearchAsync,
createTree2BreadthFirstSearch,
createTree2BreadthFirstSearchAsync,
createTree2BottomUpSearch,
createTree2BottomUpSearchAsync,
prettyStringifyTree2Node
}