Git Product home page Git Product logo

ntreego's Introduction

Go Report Card

ntreego Golang N-ary Tree Implementation. Roughly ported from glib's GNode with idiomatic go

Not Production Ready. Pull Requests Welcomed! (Need better tests!)

root := ntree.New("A")
branchB := ntree.New("B")
leafC := ntree.New("C")
leafD := ntree.New("D")

root.AppendChild(branchB)
root.AppendChild(leafD)
branchB.AppendChild(leafC)

fmt.Println(root)

ntree.Traverse is done through a few options, mainly the TraverseType, TraverseFlags, and Depth as per GNode's documentation

TraverseTypes:

  • TraversePreOrder, visits a node, then its children.
  • TraverseInOrder, vists a node's left child first, then the node itself then its right child. This is the one to use if you want the output sorted according to the compare function.
  • TraversePostOrder, visits the node's children, then the node itself.
  • TraverseLevelOrder (not implemented)

TraverseFlags:

  • TraverseAll
  • TraverseLeaves
  • TraverseNonLeaves

[Depth] is -1 to start at the root and 1->n for specified depths. 0 is root so is an invalid input

[TraverseFunc] is the function applied to each node that is traversed

[Data] is anything that should be passed to each node, allowing for lambda's to capture functionality outside of the library's traversal functions.

root := ntree.New(1)
branchB := ntree.New(2)
leafC := ntree.New(3)
leafD := ntree.New(4)

root.AppendChild(branchB)
root.AppendChild(leafD)
branchB.AppendChild(leafC)

fmt.Println(root)

var matchedNode *ntree.Node
traverseFunc := func(node *Node, value interface{}) bool {
  v := value.(int)
  if v == 3 {
    matchedNode = node
  }
  node.Value.(*int).Value += v
}

data := 1
ntree.Traverse(root, ntree.TraversePreOrder, ntree.TraverseAll, -1, traverseFunc, data)

fmt.Println(matchedNode.Value) //should be 4, since it matched to the node with value 3 and incremented by 1

ntreego's People

Contributors

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