blobt
4 years ago
7 changed files with 211 additions and 19 deletions
-
8src/IAdapter.ts
-
5src/IEnumerator.ts
-
35src/NodeB2TEnumberator.ts
-
90src/NodeEnumeratorFactory.ts
-
54src/NodeT2BEnumerator.ts
-
2src/Queue.ts
-
36src/TreeNode.ts
@ -0,0 +1,5 @@ |
|||
export interface IEnumberator<T> { |
|||
reset(): void; |
|||
moveNext(): boolean; |
|||
readonly current: T | undefined; |
|||
} |
@ -0,0 +1,35 @@ |
|||
import { IEnumberator } from "./IEnumerator"; |
|||
import { TreeNode } from "./TreeNode"; |
|||
|
|||
export class NodeB2TEnumerator<T> implements IEnumberator<TreeNode<T>>{ |
|||
private _iter: IEnumberator<TreeNode<T>>; |
|||
private _arr: Array<TreeNode<T> | undefined>; |
|||
private _arrIdx: number; |
|||
|
|||
public constructor(iter: IEnumberator<TreeNode<T>>) { |
|||
this._iter = iter; |
|||
this.reset(); |
|||
} |
|||
public reset(): void { |
|||
this._arr = []; |
|||
//调用先根枚举器,把结果存入数组
|
|||
while (this._iter.moveNext()) { |
|||
this._arr.push(this._iter.current); |
|||
} |
|||
//设置_arrIdx为数组的length,因为从尾部先前遍历
|
|||
this._arrIdx = this._arr.length; |
|||
} |
|||
|
|||
public get current(): TreeNode<T> | undefined { |
|||
if (this._arrIdx >= this._arr.length) { |
|||
return undefined; |
|||
} else { |
|||
return this._arr[this._arrIdx]; |
|||
} |
|||
} |
|||
|
|||
public moveNext(): boolean { |
|||
this._arrIdx--; |
|||
return (this._arrIdx >= 0 && this._arrIdx < this._arr.length); |
|||
} |
|||
} |
@ -0,0 +1,90 @@ |
|||
import { IndexerL2R, IndexerR2L } from "./IAdapter"; |
|||
import { IEnumberator } from "./IEnumerator"; |
|||
import { NodeT2BEnumberator } from "./NodeT2BEnumerator"; |
|||
import { TreeNode } from "./TreeNode"; |
|||
import { Stack } from "./Stack"; |
|||
import { Queue } from "./Queue"; |
|||
import { NodeB2TEnumerator } from "./NodeB2TEnumberator"; |
|||
|
|||
export class NodeEnumeratorFactory { |
|||
|
|||
/** |
|||
* 创建深度优先,从左到右,从上到下的枚举器 |
|||
* @param node |
|||
* @returns |
|||
*/ |
|||
public static create_df_l2r_t2b_iter<T>(node: TreeNode<T> | undefined): IEnumberator<TreeNode<T>> { |
|||
let iter: IEnumberator<TreeNode<T>> = new NodeT2BEnumberator(node, IndexerR2L, Stack); |
|||
return iter; |
|||
} |
|||
|
|||
/** |
|||
* 创建深度优先,从右到左,从上到下的枚举器 |
|||
* @param node |
|||
* @returns |
|||
*/ |
|||
public static create_df_r2l_t2b_iter<T>(node: TreeNode<T> | undefined): IEnumberator<TreeNode<T>> { |
|||
let iter: IEnumberator<TreeNode<T>> = new NodeT2BEnumberator(node, IndexerL2R, Stack); |
|||
return iter; |
|||
} |
|||
|
|||
/** |
|||
* 创建广度优先,从左到右,从上到下的枚举器 |
|||
* @param node |
|||
* @returns |
|||
*/ |
|||
public static create_bf_l2r_t2b_iter<T>(node: TreeNode<T> | undefined): IEnumberator<TreeNode<T>> { |
|||
let iter: IEnumberator<TreeNode<T>> = new NodeT2BEnumberator(node, IndexerL2R, Queue); |
|||
return iter; |
|||
} |
|||
|
|||
/** |
|||
* 创建广度优先,从右到左,从上到下的枚举器 |
|||
* @param node |
|||
* @returns |
|||
*/ |
|||
public static create_bf_r2l_t2b_iter<T>(node: TreeNode<T> | undefined): IEnumberator<TreeNode<T>> { |
|||
let iter: IEnumberator<TreeNode<T>> = new NodeT2BEnumberator(node, IndexerR2L, Queue); |
|||
return iter; |
|||
} |
|||
|
|||
/** |
|||
* 创建深度优先,从左到右,从下到上的枚举器 |
|||
* @param node |
|||
* @returns |
|||
*/ |
|||
public static create_df_l2r_b2t_iter<T>(node: TreeNode<T> | undefined): IEnumberator<TreeNode<T>> { |
|||
let iter: IEnumberator<TreeNode<T>> = new NodeB2TEnumerator<T>(NodeEnumeratorFactory.create_df_r2l_t2b_iter(node)); |
|||
return iter; |
|||
} |
|||
|
|||
/** |
|||
* 创建深度优先,从右到左,从下到上的枚举器 |
|||
* @param node |
|||
* @returns |
|||
*/ |
|||
public static create_df_r2l_b2t_iter<T>(node: TreeNode<T> | undefined): IEnumberator<TreeNode<T>> { |
|||
let iter: IEnumberator<TreeNode<T>> = new NodeB2TEnumerator<T>(NodeEnumeratorFactory.create_df_l2r_t2b_iter(node)); |
|||
return iter; |
|||
} |
|||
|
|||
/** |
|||
* 创建广度优先,从左到右,从下到上的枚举器 |
|||
* @param node |
|||
* @returns |
|||
*/ |
|||
public static create_bf_l2r_b2t_iter<T>(node: TreeNode<T> | undefined): IEnumberator<TreeNode<T>> { |
|||
let iter: IEnumberator<TreeNode<T>> = new NodeB2TEnumerator<T>(NodeEnumeratorFactory.create_bf_r2l_t2b_iter(node)); |
|||
return iter; |
|||
} |
|||
|
|||
/** |
|||
* 创建广度优先,从右到左,从下到上的枚举器 |
|||
* @param node |
|||
* @returns |
|||
*/ |
|||
public static create_bf_r2l_b2t_iter<T>(node: TreeNode<T> | undefined): IEnumberator<TreeNode<T>> { |
|||
let iter: IEnumberator<TreeNode<T>> = new NodeB2TEnumerator<T>(NodeEnumeratorFactory.create_bf_l2r_t2b_iter(node)); |
|||
return iter; |
|||
} |
|||
} |
@ -0,0 +1,54 @@ |
|||
import { IAdapter, Indexer } from "./IAdapter"; |
|||
import { IEnumberator } from "./IEnumerator"; |
|||
import { TreeNode } from "./TreeNode"; |
|||
|
|||
export class NodeT2BEnumberator<T, IdxFunc extends Indexer, Adapter extends IAdapter<TreeNode<T>>> implements IEnumberator<TreeNode<T>> { |
|||
private _node: TreeNode<T> | undefined; |
|||
private _adapter !: IAdapter<TreeNode<T>>; |
|||
private _currNode !: TreeNode<T> | undefined; |
|||
private _indexer !: IdxFunc; |
|||
|
|||
public constructor(node: TreeNode<T> | undefined, func: IdxFunc, adapter: new () => Adapter) { |
|||
if (node === undefined) { |
|||
return; |
|||
} |
|||
this._node = node; |
|||
this._indexer = func; |
|||
this._adapter = new adapter(); |
|||
this._currNode = undefined; |
|||
this._adapter.add(this._node); //将根节点放入到堆栈或队列中
|
|||
} |
|||
reset(): void { |
|||
if (this._node === undefined) { |
|||
return; |
|||
} |
|||
this._currNode = undefined; |
|||
this._adapter.clear(); |
|||
this._adapter.add(this._node); |
|||
} |
|||
moveNext(): boolean { |
|||
if (this._adapter.isEmpty) { |
|||
return false; |
|||
} |
|||
//弹出头或尾元素,依赖adapter是stack还是queue
|
|||
this._currNode = this._adapter.remove(); |
|||
|
|||
if (this._currNode !== undefined) { |
|||
//获取当前节点的儿子个数
|
|||
let len: number = this._currNode.childCount; |
|||
|
|||
for (let i = 0; i < len; i++) { |
|||
let childIdx: number = this._indexer(len, i); |
|||
let child: TreeNode<T> | undefined = this._currNode.getChildAt(childIdx); |
|||
if(child !== undefined){ |
|||
this._adapter.add(child); |
|||
} |
|||
} |
|||
} |
|||
return true; |
|||
} |
|||
public get current(): TreeNode<T> | undefined { |
|||
return this._currNode; |
|||
} |
|||
|
|||
} |
Write
Preview
Loading…
Cancel
Save
Reference in new issue