-
Notifications
You must be signed in to change notification settings - Fork 943
feat(utils): add n-ary tree LCA utility #1716
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Closed
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,28 @@ | ||
| { | ||
| "name": "@composio/ao-utils", | ||
| "version": "0.1.0", | ||
| "description": "Shared utility functions for agent-orchestrator", | ||
| "license": "MIT", | ||
| "type": "module", | ||
| "main": "dist/index.js", | ||
| "types": "dist/index.d.ts", | ||
| "exports": { | ||
| ".": { | ||
| "types": "./dist/index.d.ts", | ||
| "import": "./dist/index.js" | ||
| } | ||
| }, | ||
| "files": [ | ||
| "dist" | ||
| ], | ||
| "scripts": { | ||
| "build": "tsc -p tsconfig.build.json", | ||
| "typecheck": "tsc --noEmit", | ||
| "test": "vitest run", | ||
| "test:watch": "vitest" | ||
| }, | ||
| "devDependencies": { | ||
| "typescript": "^5.7.0", | ||
|
greptile-apps[bot] marked this conversation as resolved.
|
||
| "vitest": "^4.0.18" | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,76 @@ | ||
| import { describe, it, expect } from "vitest"; | ||
| import { findLCA, type NaryNode } from "../nary-lca.js"; | ||
|
|
||
| // Helper to build a node quickly. | ||
| function node(val: number, ...children: NaryNode[]): NaryNode { | ||
| return { val, children }; | ||
| } | ||
|
|
||
| // Tree used in most tests: | ||
| // | ||
| // 1 | ||
| // / | \ | ||
| // 2 3 4 | ||
| // / \ | ||
| // 5 6 | ||
| // | ||
| const tree = node(1, node(2, node(5), node(6)), node(3), node(4)); | ||
|
|
||
| describe("findLCA", () => { | ||
| it("returns the common ancestor of two leaf nodes", () => { | ||
| // 5 and 6 are both children of 2 | ||
| const result = findLCA(tree, 5, 6); | ||
| expect(result?.val).toBe(2); | ||
| }); | ||
|
|
||
| it("returns the ancestor when one node is an ancestor of the other", () => { | ||
| // 2 is a direct ancestor of 5 | ||
| const result = findLCA(tree, 2, 5); | ||
| expect(result?.val).toBe(2); | ||
| }); | ||
|
|
||
| it("returns the root when targets are in different subtrees far from root", () => { | ||
| // 5 is under 2, 3 is a direct child of root 1 | ||
| const result = findLCA(tree, 5, 3); | ||
| expect(result?.val).toBe(1); | ||
| }); | ||
|
|
||
| it("returns the root when both nodes are direct children of root", () => { | ||
| const result = findLCA(tree, 2, 4); | ||
| expect(result?.val).toBe(1); | ||
| }); | ||
|
|
||
| it("returns the node itself when val1 === val2", () => { | ||
| const result = findLCA(tree, 6, 6); | ||
| expect(result?.val).toBe(6); | ||
| }); | ||
|
|
||
| it("returns null when val1 is not in the tree", () => { | ||
| const result = findLCA(tree, 99, 3); | ||
| expect(result).toBeNull(); | ||
| }); | ||
|
|
||
| it("returns null when val2 is not in the tree", () => { | ||
| const result = findLCA(tree, 2, 99); | ||
| expect(result).toBeNull(); | ||
| }); | ||
|
|
||
| it("returns null when both values are absent", () => { | ||
| const result = findLCA(tree, 88, 99); | ||
| expect(result).toBeNull(); | ||
| }); | ||
|
|
||
| it("returns null for a null root", () => { | ||
| expect(findLCA(null, 1, 2)).toBeNull(); | ||
| }); | ||
|
|
||
| it("returns the single node when it matches val1 in a single-node tree", () => { | ||
| const single = node(42); | ||
| expect(findLCA(single, 42, 42)?.val).toBe(42); | ||
| }); | ||
|
|
||
| it("returns null when single-node tree does not contain either value", () => { | ||
| const single = node(42); | ||
| expect(findLCA(single, 1, 2)).toBeNull(); | ||
| }); | ||
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,2 @@ | ||
| export { findLCA } from "./nary-lca.js"; | ||
| export type { NaryNode } from "./nary-lca.js"; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,60 @@ | ||
| export interface NaryNode { | ||
| val: number; | ||
| children: NaryNode[]; | ||
| } | ||
|
|
||
| /** | ||
| * Finds the Lowest Common Ancestor (LCA) of two nodes in an n-ary tree. | ||
| * | ||
| * Returns null if either target value is absent from the tree. | ||
| * A node is considered its own ancestor (if one target is an ancestor of the | ||
| * other, that ancestor node is returned). | ||
| * | ||
| * Uses post-order DFS: children are visited before the current node is | ||
| * evaluated, so a deeper match surfaces before its ancestor is inspected. | ||
| */ | ||
| export function findLCA( | ||
| root: NaryNode | null, | ||
| val1: number, | ||
| val2: number, | ||
| ): NaryNode | null { | ||
| let found1 = false; | ||
| let found2 = false; | ||
|
|
||
| function dfs(node: NaryNode | null): NaryNode | null { | ||
| if (node === null) return null; | ||
|
|
||
| // Post-order: recurse into all children first so that a target node's | ||
| // subtree is fully searched before we process the target node itself. | ||
| const hits: NaryNode[] = []; | ||
| for (const child of node.children) { | ||
| const result = dfs(child); | ||
| if (result !== null) hits.push(result); | ||
| } | ||
|
|
||
| const isTarget = node.val === val1 || node.val === val2; | ||
| if (node.val === val1) found1 = true; | ||
| if (node.val === val2) found2 = true; | ||
|
|
||
| if (isTarget) { | ||
| // This node is one of the targets. Return it regardless of what surfaced | ||
| // from children — if the other target was in the subtree, this node is | ||
| // the LCA; if not, this node is just the "found" signal propagating up. | ||
| return node; | ||
| } | ||
|
|
||
| if (hits.length >= 2) { | ||
| // Two distinct targets surfaced from different children — this node is | ||
| // the LCA. | ||
| return node; | ||
|
greptile-apps[bot] marked this conversation as resolved.
|
||
| } | ||
|
|
||
| return hits[0] ?? null; | ||
| } | ||
|
|
||
| const candidate = dfs(root); | ||
|
|
||
| if (!found1 || !found2) return null; | ||
|
|
||
| return candidate; | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,4 @@ | ||
| { | ||
| "extends": "./tsconfig.json", | ||
| "exclude": ["src/__tests__"] | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,8 @@ | ||
| { | ||
| "extends": "../../tsconfig.base.json", | ||
| "compilerOptions": { | ||
| "outDir": "dist", | ||
| "rootDir": "src" | ||
| }, | ||
| "include": ["src"] | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| import { defineConfig } from "vitest/config"; | ||
|
|
||
| export default defineConfig({ | ||
| test: {}, | ||
| }); |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.