Project

tree_print

0.0
No commit activity in last 3 years
No release in over 3 years
Recursive method that converts the head node to a plain string, allowing you to print and visualize your binary tree.
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
 Dependencies
 Project Readme

tree_print

A gem for visualizing binary tree data structures in ruby.

Works for any node-based implementations where the node responds to the following methods
node.left     # returns node object 'on the left'
node.right    # returns node object 'on the right'
node.val      # returns value at that node
node.height   # however many levels above the bottom-most child (leaf) node
              # (leaf nodes are at height 1)

Simply pass the head node as an argument to the TreePrint::tree_to_s method to convert the whole tree to a nicely formatted, multi-line string. You can include the TreePrint module to your class as well.

See examples for integration with a binary search tree class. Running the example.rb in the examples directory produces the following output:

example1 example2 example3

RubyGems link

https://rubygems.org/gems/tree_print