Giter Site home page Giter Site logo

arkoniak / leftchildrightsiblingtrees.jl Goto Github PK

View Code? Open in Web Editor NEW

This project forked from juliacollections/leftchildrightsiblingtrees.jl

0.0 1.0 0.0 12 KB

Memory-efficient representation of a tree with arbitrary number of children/node

License: MIT License

Julia 100.00%

leftchildrightsiblingtrees.jl's Introduction

LeftChildRightSiblingTrees

A left child, right sibling tree (frequently abbreviated as "LCRS") is a rooted tree data structure that allows a parent node to have multiple child nodes. Rather than maintain a list of children (which requires one array per node), instead it is represented as a binary tree, where the "left" branch is the first child, whose "right" branch points to its first sibling.

Concretely, suppose a particular node, A, has 3 children, a, b, and c. Then:

  • a, b, and c link to A as their parent.
  • A links a as its child (via A's left branch); a links b as its sibling (via a's right branch), and b links c as its sibling (via b's right branch).
  • A's right branch would link to any of its siblings (e.g., B), if they exist.
  • Any missing links (e.g., c does not have a sibling) link back to itself (c.sibling == c).

Tradeoffs

An LCRS tree is typically more memory efficient than an equivalent multi-way tree representation that uses an array to store the children of each node. However, for certain tasks it can be less performant, because some operations that modify the tree structure require iterating over all the children of a node.

Demo

Creating a Tree

Can addchild or addsibling.

julia> using LeftChildRightSiblingTrees

julia> mum = Node("Mum");

julia> me = addchild(mum, "Me");

julia> son = addchild(me, "Son");

julia> daughter = addchild(me, "Daughter");

julia> brother = addsibling(me, "Brother");  # equivalent: to `addchild(mum, "Brother")`

Querying about nodes:

julia> lastsibling(me)
Node(Brother)

julia> isroot(mum)
true

julia> isleaf(me)
false

julia> isleaf(daughter)
true

Iterating the Tree/Nodes

Iteration goes through all (direct) children. The .data field holds the information put in the tree. we can use this to draw a simple visualization of the tree via recursion.

julia> for child in mum
           println(child)
       end
Node(Me)
Node(Brother)

julia> function showtree(node, indent=0)
           println("\t"^indent, node.data)
           for child in node
               showtree(child, indent + 1)
           end
       end
showtree (generic function with 2 methods)

julia> showtree(mum)
Mum
        Me
                Son
                Daughter
        Brother

LeftChildRightSiblingTrees also has a built in function for showing this kind of info:

julia> LeftChildRightSiblingTrees.showedges(mum)
Mum has the following children: Me    Brother
Me has the following children: Son    Daughter
Son has no children
Daughter has no children
Brother has no children

Manipulating the tree

See the docstrings for graftchildren! and prunebranch!.

Credits

This existed as an internal component of ProfileView since its inception until it was split out as an independent package.

leftchildrightsiblingtrees.jl's People

Contributors

timholy avatar oxinabox avatar

Watchers

James Cloos avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.