|
@ -1,7 +1,10 @@ |
|
|
|
|
|
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; |
|
|
public name: string ; |
|
|
|
|
|
|
|
|
public name: string; |
|
|
public data: T | undefined; |
|
|
public data: T | undefined; |
|
|
|
|
|
|
|
|
public constructor(data: T | undefined = undefined, parent: TreeNode<T> | undefined = undefined, name: string) { |
|
|
public constructor(data: T | undefined = undefined, parent: TreeNode<T> | undefined = undefined, name: string) { |
|
@ -155,9 +158,28 @@ export class TreeNode<T> { |
|
|
} |
|
|
} |
|
|
public repeatString(target: string, n: number): string { |
|
|
public repeatString(target: string, n: number): string { |
|
|
let total: string = ""; |
|
|
let total: string = ""; |
|
|
for(let i = 0; i < n; i++){ |
|
|
|
|
|
|
|
|
for (let i = 0; i < n; i++) { |
|
|
total += target; |
|
|
total += target; |
|
|
} |
|
|
} |
|
|
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); |
|
|
|
|
|
} |
|
|
|
|
|
} |
|
|
} |
|
|
} |