blobt
4 years ago
6 changed files with 71 additions and 16 deletions
-
2src/IEnumerator.ts
-
4src/NodeB2TEnumberator.ts
-
3src/NodeT2BEnumerator.ts
-
2src/Stack.ts
-
13src/TreeNode.ts
-
63src/index.ts
@ -1,3 +1,5 @@ |
|||
import { IAdapter, Indexer } from "./IAdapter"; |
|||
import { TreeNode } from "./TreeNode"; |
|||
export interface IEnumberator<T> { |
|||
reset(): void; |
|||
moveNext(): boolean; |
|||
|
@ -1,10 +1,57 @@ |
|||
import { Dictionary } from "./Dictionary"; |
|||
import { IEnumberator } from "./IEnumerator"; |
|||
import { NodeEnumeratorFactory } from "./NodeEnumeratorFactory"; |
|||
import { TreeNode } from "./TreeNode"; |
|||
import { IndexerL2R, IndexerR2L } from "./IAdapter"; |
|||
import { NodeT2BEnumberator } from "./NodeT2BEnumerator"; |
|||
import { Stack } from "./Stack"; |
|||
import { Queue } from "./Queue"; |
|||
import { NodeB2TEnumerator } from "./NodeB2TEnumberator"; |
|||
|
|||
let dict: Dictionary<string> = new Dictionary(false); |
|||
dict.insert("ka", "a"); |
|||
dict.insert("kd", "d"); |
|||
dict.insert("kc", "c"); |
|||
dict.insert("kb", "b"); |
|||
class NumberNode extends TreeNode<number> { } |
|||
|
|||
console.log(JSON.stringify(dict.keys)); |
|||
console.log(JSON.stringify(dict.values)); |
|||
class TreeNodeTest { |
|||
public static createTree(): NumberNode { |
|||
let root: NumberNode = new NumberNode(0, undefined, " root "); |
|||
let node1: NumberNode = new NumberNode(1, root, " node1 "); |
|||
let node2: NumberNode = new NumberNode(2, root, " node2 "); |
|||
let node3: NumberNode = new NumberNode(3, root, " node3 "); |
|||
let node4: NumberNode = new NumberNode(4, node1, " node4 "); |
|||
let node5: NumberNode = new NumberNode(5, node1, " node5 "); |
|||
let node6: NumberNode = new NumberNode(6, node2, " node6 "); |
|||
let node7: NumberNode = new NumberNode(7, node2, " node7 "); |
|||
let node8: NumberNode = new NumberNode(8, node3, " node8 "); |
|||
let node9: NumberNode = new NumberNode(9, node4, " node9 "); |
|||
let node10: NumberNode = new NumberNode(10, node6, " node10 "); |
|||
let node11: NumberNode = new NumberNode(11, node7, " node11 "); |
|||
let node12: NumberNode = new NumberNode(12, node11, " node12 "); |
|||
return root; |
|||
} |
|||
|
|||
public static outputNodesInfo(iter: IEnumberator<TreeNode<number>>): string { |
|||
let output: string[] = []; |
|||
let current: TreeNode<number> | undefined = undefined; |
|||
while(iter.moveNext()){ |
|||
current = iter.current; |
|||
if(current !== undefined){ |
|||
output.push(current.name); |
|||
} |
|||
} |
|||
return "实际输出:[" + output.join(",") + " ] "; |
|||
} |
|||
} |
|||
|
|||
let root: NumberNode = TreeNodeTest.createTree(); |
|||
let iter: IEnumberator<TreeNode<number>>; |
|||
let current: TreeNode<number> | undefined = undefined; |
|||
|
|||
iter = new NodeT2BEnumberator(root, IndexerR2L, Stack); |
|||
|
|||
while (iter.moveNext()) { |
|||
current = iter.current; |
|||
if (current !== undefined) { |
|||
console.log(current.repeatString(" ", current.depth * 4) + current.name); |
|||
} |
|||
} |
|||
|
|||
// iter = new NodeT2BEnumberator(root, IndexerR2L, Queue);
|
|||
// console.log(TreeNodeTest.outputNodesInfo(iter));
|
Write
Preview
Loading…
Cancel
Save
Reference in new issue