mirror of
https://github.com/mermaid-js/mermaid.git
synced 2025-01-28 07:03:17 +08:00
33 lines
673 B
TypeScript
33 lines
673 B
TypeScript
export interface TreeData {
|
|
parentById: Record<string, string>;
|
|
childrenById: Record<string, string[]>;
|
|
}
|
|
|
|
export const findCommonAncestor = (id1: string, id2: string, { parentById }: TreeData) => {
|
|
const visited = new Set();
|
|
let currentId = id1;
|
|
|
|
// Edge case with self edges
|
|
if (id1 === id2) {
|
|
return parentById[id1] || 'root';
|
|
}
|
|
|
|
while (currentId) {
|
|
visited.add(currentId);
|
|
if (currentId === id2) {
|
|
return currentId;
|
|
}
|
|
currentId = parentById[currentId];
|
|
}
|
|
|
|
currentId = id2;
|
|
while (currentId) {
|
|
if (visited.has(currentId)) {
|
|
return currentId;
|
|
}
|
|
currentId = parentById[currentId];
|
|
}
|
|
|
|
return 'root';
|
|
};
|