|
@ -1,3 +1,6 @@ |
|
|
|
|
|
import { Indexer, IndexerL2R } from "./IAdapter"; |
|
|
|
|
|
|
|
|
|
|
|
export type NodeCallback<T> = (node: TreeNode<T>) => void; |
|
|
export class TreeNode<T> { |
|
|
export class TreeNode<T> { |
|
|
private _parent: TreeNode<T> | undefined; |
|
|
private _parent: TreeNode<T> | undefined; |
|
|
private _children: Array<TreeNode<T>> | undefined; |
|
|
private _children: Array<TreeNode<T>> | undefined; |
|
@ -160,4 +163,23 @@ export class TreeNode<T> { |
|
|
} |
|
|
} |
|
|
return total; |
|
|
return total; |
|
|
} |
|
|
} |
|
|
|
|
|
public visit(preOrderFunc: NodeCallback<T> | null = null, postOrderFunc: NodeCallback<T> | null = null, indexFunc: Indexer = IndexerL2R): void { |
|
|
|
|
|
if (preOrderFunc !== null) { |
|
|
|
|
|
preOrderFunc(this); |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
let arr: Array<TreeNode<T>> | undefined = this._children; |
|
|
|
|
|
if (arr !== undefined) { |
|
|
|
|
|
for (let i: number = 0; i < arr.length; i++) { |
|
|
|
|
|
let child: TreeNode<T> | undefined = this.getChildAt(indexFunc(arr.length, i)); |
|
|
|
|
|
if(child !== undefined){ |
|
|
|
|
|
child.visit(preOrderFunc, postOrderFunc, indexFunc); |
|
|
|
|
|
} |
|
|
|
|
|
} |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
if (postOrderFunc !== null) { |
|
|
|
|
|
postOrderFunc(this); |
|
|
|
|
|
} |
|
|
|
|
|
} |
|
|
} |
|
|
} |