File size: 7,120 Bytes
71174bc | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 | import type { TreeNode } from "../TreeNode/TreeNode";
import type { TreeNodeList } from "./TreeNodeList";
import { getFlattenedFromCache, setFlattenedInCache } from "../TreeCache";
/**
* TreeNodeListNodeActions class
*/
export class TreeNodeListNodeActions {
private parentTreeNodeList: TreeNodeList;
/**
* Creates an instance of TreeNodeListNodeActions.
*
* @param {TreeNodeList} parentTreeNodeList The parent TreeNodeList.
*/
constructor(parentTreeNodeList: TreeNodeList) {
this.parentTreeNodeList = parentTreeNodeList;
}
/**
* Remove a node of given id, as well as any childless nodes that result.
* In-place operation.
*
* @param {string | TreeNode | null} node The id of the node to remove, or
* the node itself.
*/
public remove(node: string | TreeNode | null) {
if (typeof node === "string") {
node = this.parentTreeNodeList.filters.onlyId(node);
}
if (!node) {
// TreeNode doesn't exist. Take no action.
return;
}
let { id } = node;
if (!node.parentId) {
// It's a root node, without a parent id.
this.parentTreeNodeList.nodes =
this.parentTreeNodeList.filters.removeId(id as string).nodes;
return;
}
// If you get here, node is not string or null, but must be TreeNode.
let curNode = this.parentTreeNodeList.filters.onlyId(node.parentId);
// Could be that in removing this node, parent node has no children. Delete
// that too, up the tree (because these are not terminal nodes, and if a
// non-terminal node doens't have any children, there's no reason for it to
// exist).
// eslint-disable-next-line no-constant-condition
while (true) {
if (!curNode) {
// Parent node does not exist. Something's wrong.
break;
}
if (!curNode.nodes) {
// Parent node has no children, something's wrong.
break;
}
curNode.nodes = curNode.nodes.filters.removeId(id as string);
if (curNode.nodes.length > 0) {
// Parent node still has children (siblings of just deleted), so
// we're done.
break;
}
if (!curNode.parentId) {
// No parent node, so we're done
break;
}
// Go up to parent.
id = curNode.id as string;
curNode = this.parentTreeNodeList.filters.onlyId(curNode.parentId);
if (!curNode) {
// No parent node, so we're done. Already checked using parentId,
// but you need this here for typescript.
break;
}
}
}
/**
* Gets all the nodes, whether terminal or not. I ended up prefering this
* version to the ones below because it preserves order.
*
* @returns {TreeNodeList} The flat array of all nodes.
*/
public get flattened(): TreeNodeList {
const cached = getFlattenedFromCache(this.parentTreeNodeList);
if (cached) {
return cached;
}
const resultNodes: TreeNode[] = [];
// Use a stack for iterative traversal to avoid recursion limits and improve performance.
const stack: TreeNode[] = [];
const rootNodes = this.parentTreeNodeList.nodes;
// Initialize stack with root nodes in reverse order so they are popped in the correct order.
for (let i = rootNodes.length - 1; i >= 0; i--) {
stack.push(rootNodes[i]);
}
while (stack.length > 0) {
const node = stack.pop();
if (node) {
resultNodes.push(node);
if (node.nodes && node.nodes.length > 0) {
const children = node.nodes.nodes;
// Push children in reverse order to maintain Pre-order traversal
for (let i = children.length - 1; i >= 0; i--) {
stack.push(children[i]);
}
}
}
}
const result = this.parentTreeNodeList.newTreeNodeList(resultNodes);
setFlattenedInCache(this.parentTreeNodeList, result);
return result;
}
/**
* Gets all the nodes, whether terminal or not.
*
* @returns {TreeNodeList} The flat array of all nodes.
*/
// public get flattened(): TreeNodeList {
// // NOTE: This is adapted from ChatGPT's recommended revision of above
// // function. I'm fairly certain it works the same, but if you run into
// // errors, consider the differences between the two functions.
// const allNodes: TreeNode[] = [];
// const processedNodes = new Set<TreeNode>();
// const stack = [this.parentTreeNodeList._nodes];
// while (stack.length > 0) {
// const nodes = stack.pop();
// // Note nodes should not be null, but typescript doesn't know that.
// // Don't want to do check for null every time.
// // eslint-disable-next-line @typescript-eslint/ban-ts-comment
// // @ts-ignore
// for (const node of nodes) {
// if (!processedNodes.has(node)) {
// processedNodes.add(node);
// allNodes.push(node);
// if (node.nodes && node.nodes.length > 0) {
// stack.push(node.nodes._nodes);
// }
// }
// }
// }
// return this.parentTreeNodeList.newTreeNodeList(allNodes);
// }
// /**
// * Gets all the nodes, whether terminal or not.
// *
// * @returns {TreeNodeList} The flat array of all nodes.
// */
// public get flattened(): TreeNodeList {
// // NOTE: This is adapted from ChatGPT's recommended revision of above
// // function. I'm fairly certain it works the same, but if you run into
// // errors, consider the differences between the two functions.
// const treeNodeListStack: TreeNodeList[] = [this.parentTreeNodeList];
// const allNodes = this.parentTreeNodeList.newTreeNodeList();
// while (treeNodeListStack.length > 0) {
// const currentTreeNode = treeNodeListStack.pop();
// if (!currentTreeNode) {
// continue;
// }
// currentTreeNode.forEach((treeNode: TreeNode) => {
// allNodes.push(treeNode);
// if (treeNode.nodes && treeNode.nodes.length > 0) {
// treeNode.nodes.forEach((node) => {
// if (node.nodes) {
// treeNodeListStack.push(node.nodes);
// }
// })
// }
// });
// }
// return allNodes;
// }
}
|