Phase 1 Recursive DFS pre-order algorithm, tree print helper for visualizing tree structures, filesystem test fixture builder helper function.

deleteNode fully implemented,

test suite implemented
This commit is contained in:
2026-07-19 09:30:08 +02:00
parent b6365d6221
commit 04b66fa62a
8 changed files with 334 additions and 54 deletions
+61
View File
@@ -0,0 +1,61 @@
import type { FileSystem } from "@core/filesystem/filesystem";
import { TreeNodeType } from "@core/filesystem/treenode";
export function printTree(fs: FileSystem, root: number): string {
const rootNode = fs.getNode(root);
if (rootNode === undefined) return "";
const lines: string[] = [];
lines.push(rootNode.name);
type Frame = {
inode: number;
prefix: string;
isLast: boolean;
};
const stack: Frame[] = [];
const getSorterChildren = (inode: number): number[] => {
const node = fs.getNode(inode);
if (!node || node.type !== TreeNodeType.Directory) return [];
const entries = Object.entries(node.children);
entries.sort(([a], [b]) => a.localeCompare(b));
return entries.map(([, childInode]) => childInode) as number[];
};
const rootChildren = getSorterChildren(root);
for (let i = rootChildren.length - 1; i >= 0; i--) {
const childInode: number = rootChildren[i] as number;
const isLast = i === rootChildren.length - 1;
stack.push({ inode: childInode, prefix: "", isLast });
}
while (stack.length > 0) {
const frame = stack.pop()!;
const node = fs.getNode(frame.inode);
if (!node) continue;
const name = node.name;
const connector = frame.isLast ? "└── " : "├── ";
lines.push(frame.prefix + connector + name);
if (node.type === TreeNodeType.Directory) {
const childPrefix = frame.prefix + (frame.isLast ? " " : "│ ");
const children = getSorterChildren(frame.inode);
for (let i = children.length - 1; i >= 0; i--) {
const childInode = children[i] as number;
const childIsLast = i === children.length - 1;
stack.push({ inode: childInode, prefix: childPrefix, isLast: childIsLast });
}
}
}
return lines.join("\n");
}