Skip to content

Commit

Permalink
feat(TreeData): 优化遍历算法
Browse files Browse the repository at this point in the history
  • Loading branch information
fjc0k committed Dec 12, 2020
1 parent 7c009f3 commit 954af64
Show file tree
Hide file tree
Showing 2 changed files with 332 additions and 49 deletions.
81 changes: 32 additions & 49 deletions src/utils/TreeData.ts
Original file line number Diff line number Diff line change
Expand Up @@ -107,23 +107,29 @@ export class TreeData<TNode extends TreeDataNode> {
depth: number,
path: TNode[],
) {
if (searchMethod === 'DFS') {
const postActions: Array<() => void> = []
for (const node of data) {
let isRemove = false
let isExit = false
fn({
node: node,
parentNode: parentNode,
depth: depth,
path: path,
removeNode: () => {
isRemove = true
postActions.push(() => data.splice(data.indexOf(node), 1))
},
exit: () => (isExit = true),
})
if (isExit) return
const removeNodeIndexes: number[] = []

for (let i = 0, len = data.length; i < len; i++) {
const node = data[i]

let isRemove = false
let isExit = false

fn({
node: node,
parentNode: parentNode,
depth: depth,
path: path,
removeNode: () => {
isRemove = true
removeNodeIndexes.push(i)
},
exit: () => (isExit = true),
})

if (isExit) return

if (searchMethod === 'DFS') {
if (
!isRemove &&
node[childrenPropName] &&
Expand All @@ -140,36 +146,16 @@ export class TreeData<TNode extends TreeDataNode> {
)
}
}
for (const action of postActions) {
action()
}
} else {
const removeNodes: TNode[] = []
const postActions: Array<() => void> = []
for (const node of data) {
let isExit = false
fn({
node: node,
parentNode: parentNode,
depth: depth,
path: path,
removeNode: () => {
removeNodes.push(node)
postActions.push(() => data.splice(data.indexOf(node), 1))
},
exit: () => (isExit = true),
})
for (const action of postActions) {
action()
}
if (isExit) return
}
}

// 删除节点
for (let i = removeNodeIndexes.length - 1; i >= 0; i--) {
data.splice(removeNodeIndexes[i], 1)
}

if (searchMethod === 'BFS') {
for (const node of data) {
if (
removeNodes.indexOf(node) !== -1 &&
node[childrenPropName] &&
Array.isArray(node[childrenPropName])
) {
if (node[childrenPropName] && Array.isArray(node[childrenPropName])) {
TreeData.traverse(
node[childrenPropName],
childrenPropName,
Expand All @@ -181,9 +167,6 @@ export class TreeData<TNode extends TreeDataNode> {
)
}
}
for (const action of postActions) {
action()
}
}
}

Expand Down
Loading

0 comments on commit 954af64

Please sign in to comment.