Coder Social home page Coder Social logo

tree.jl's Introduction

Tree

Usage

$ julia
using Tree

# create tree

## create nodes
parent = Tree.Node("parent_value")
child_1 = Tree.Node("child_1_value")
child_2 = Tree.Node("child_2_value")
grandchild = Tree.Node("grandchild_value")

## build tree
Tree.add_children!(parent, [child_1, child_2])
Tree.add_children!(child_1, [grandchild])

# your tree:
#       parent
#       /    \
#  child_1  child_2
#     |
# grandchild

# get a Node value
child_1.value
#=> "child_1_value"

# get Node children
parent.children
#=> 2-element Array{Tree.Node,1}: ...

# get Node parent
Tree.get_parent(child_1)
#=> Tree.Node("parent_value",Nullable{Tree.Node}()...

# check if Node is leaf (no children)
Tree.is_leaf(grandchild)
#=> true
Tree.is_leaf(child_1)
#=> false

# check if Node is root (no parent)
Tree.is_root(parent)
#=> true
Tree.is_root(grandchild)
#=> false

# get root Node
Tree.get_root(parent)
#=> Tree.Node("parent_value",Nullable{Tree.Node}()...
Tree.get_root(child_1)
#=> Tree.Node("parent_value",Nullable{Tree.Node}()...

# check if Node is ancestor (above/parent) of other Node
Tree.is_ancestor(parent, grandchild)
#=> true
Tree.is_ancestor(child_1, parent)
#=> false

# check if Node is descendant (below/child) of other Node
Tree.is_descendant(child_2, parent)
#=> true
Tree.is_descendant(parent, grandchild)
#=> false

# check if Node is sibling (same parent) of other Node
Tree.is_sibling(child_1, child_2)
#=> true
Tree.is_sibling(parent, grandchild)
#=> false

# get depth (distance from node to top)
Tree.depth(parent)
#=> 0
Tree.depth(child_1)
#=> 1
Tree.depth(grandchild)
#=> 2

# get height (longest distance from node to bottom)
Tree.height(parent)
#=> 2
Tree.height(child_1)
#=> 1
Tree.height(grandchild)
#=> 0

# all done? let's get out of here
exit()

Development

# work in (`cd` to) your Julia location
cd ~/.julia/v0.4/Tree

# run tests
make test

tree.jl's People

Contributors

xta avatar

Stargazers

 avatar

Watchers

 avatar  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.