UNPKG

4.08 kBTypeScriptView Raw
1import { TreeNode } from './tree';
2export interface TreeIterator extends Iterator<TreeNode> {
3}
4export declare namespace TreeIterator {
5 interface Options {
6 readonly pruneCollapsed: boolean;
7 readonly pruneSiblings: boolean;
8 }
9 const DEFAULT_OPTIONS: Options;
10}
11export declare abstract class AbstractTreeIterator implements TreeIterator, Iterable<TreeNode> {
12 protected readonly root: TreeNode;
13 protected readonly delegate: IterableIterator<TreeNode>;
14 protected readonly options: TreeIterator.Options;
15 constructor(root: TreeNode, options?: Partial<TreeIterator.Options>);
16 [Symbol.iterator](): IterableIterator<TreeNode>;
17 next(): IteratorResult<TreeNode>;
18 protected abstract iterator(node: TreeNode): IterableIterator<TreeNode>;
19 protected children(node: TreeNode): TreeNode[] | undefined;
20 protected isCollapsed(node: TreeNode): boolean;
21 protected isEmpty(nodes: TreeNode[] | undefined): boolean;
22}
23export declare class DepthFirstTreeIterator extends AbstractTreeIterator {
24 protected iterator(root: TreeNode): IterableIterator<TreeNode>;
25}
26export declare class BreadthFirstTreeIterator extends AbstractTreeIterator {
27 protected iterator(root: TreeNode): IterableIterator<TreeNode>;
28}
29/**
30 * This tree iterator visits all nodes from top to bottom considering the following rules.
31 *
32 * Let assume the following tree:
33 * ```
34 * R
35 * |
36 * +---1
37 * | |
38 * | +---1.1
39 * | |
40 * | +---1.2
41 * | |
42 * | +---1.3
43 * | | |
44 * | | +---1.3.1
45 * | | |
46 * | | +---1.3.2
47 * | |
48 * | +---1.4
49 * |
50 * +---2
51 * |
52 * +---2.1
53 * ```
54 * When selecting `1.2` as the root, the normal `DepthFirstTreeIterator` would stop on `1.2` as it does not have children,
55 * but this iterator will visit the next sibling (`1.3` and `1.4` but **not** `1.1`) nodes. So the expected traversal order will be
56 * `1.2`, `1.3`, `1.3.1`, `1.3.2`, and `1.4` then jumps to `2` and continues with `2.1`.
57 */
58export declare class TopDownTreeIterator extends AbstractTreeIterator {
59 protected iterator(root: TreeNode): IterableIterator<TreeNode>;
60 protected doNext(node: TreeNode): TreeNode | undefined;
61 protected findFirstChild(node: TreeNode): TreeNode | undefined;
62 protected findNextSibling(node: TreeNode | undefined): TreeNode | undefined;
63}
64/**
65 * Unlike other tree iterators, this does not visit all the nodes, it stops once it reaches the root node
66 * while traversing up the tree hierarchy in an inverse pre-order fashion. This is the counterpart of the `TopDownTreeIterator`.
67 */
68export declare class BottomUpTreeIterator extends AbstractTreeIterator {
69 protected iterator(root: TreeNode): IterableIterator<TreeNode>;
70 protected doNext(node: TreeNode): TreeNode | undefined;
71 protected lastChild(node: TreeNode | undefined): TreeNode | undefined;
72}
73export declare namespace Iterators {
74 /**
75 * Generator for depth first, pre-order tree traversal iteration.
76 */
77 function depthFirst<T>(root: T, children: (node: T) => T[] | undefined, include?: (node: T) => boolean): IterableIterator<T>;
78 /**
79 * Generator for breadth first tree traversal iteration.
80 */
81 function breadthFirst<T>(root: T, children: (node: T) => T[] | undefined, include?: (node: T) => boolean): IterableIterator<T>;
82 /**
83 * Returns with the iterator of the argument.
84 */
85 function asIterator<T>(elements: ReadonlyArray<T>): IterableIterator<T>;
86 /**
87 * Returns an iterator that cycles indefinitely over the elements of iterable.
88 * - If `start` is given it starts the iteration from that element. Otherwise, it starts with the first element of the array.
89 * - If `start` is given, it must contain by the `elements` array. Otherwise, an error will be thrown.
90 *
91 * **Warning**: Typical uses of the resulting iterator may produce an infinite loop. You should use an explicit break.
92 */
93 function cycle<T>(elements: ReadonlyArray<T>, start?: T): IterableIterator<T>;
94}
95//# sourceMappingURL=tree-iterator.d.ts.map
\No newline at end of file