zharikovpro / overlapping-hierarchy

Library for modeling overlapping hierarchy, in which nodes can have multiple parents.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

OH | Overlapping Hierarchy

⚠️ Superseded by OOH | Ordered Overlapping Hierarcny and no longer maintained.

Library for modeling overlapping hierarchy, in which nodes can have multiple parents.

Equivalent of transitively reduced directed acyclic graph, in which edges represent parenthood.

Example

A  B F  G
 \ | | /
   C H
 / | | \
D  E I  J
const hierarchy = new OverlappingHierarchy()
hierarchy.add('A')
hierarchy.add('B')
hierarchy.attach('A', 'C')
hierarchy.attach('B', 'C')
hierarchy.attach('C', 'D')
hierarchy.attach('C', 'E')
hierarchy.add('F')
hierarchy.add('G')
hierarchy.attach('F', 'H')
hierarchy.attach('G', 'H')
hierarchy.attach('H', 'I')
hierarchy.attach('H', 'J')

API

Initialization

const empty = new OverlappingHierarchy()

const cloned = new OverlappingHierarchy(source)

Mutation

hierarchy.add(node)

hierarchy.attach(parent, child)

hierarchy.detach(parent, child)

hierarchy.delete(node)

Traversal

hierarchy.nodes()

hierarchy.hierarchs()

hierarchy.children(parent)

hierarchy.parents(child)

hierarchy.descendants(ancestor)

hierarchy.ancestors(descendant)

Errors

LoopError

hierarchy.attach('A', 'A') // LoopError: Cannot add node to itself

CycleError

hierarchy.attach('D', 'A') // CycleError: Cannot add ancestor as a child

TransitiveReductionError

hierarchy.attach('A', 'D') // TransitiveReductionError: Cannot attach non-child descendant as a child
hierarchy.attach('A', 'B') // TransitiveReductionError: Cannot attach child whose descendant is a child of the parent

About

Library for modeling overlapping hierarchy, in which nodes can have multiple parents.

License:MIT License


Languages

Language:TypeScript 100.0%