All Projects → billvanyo-zz → Tree_printer

billvanyo-zz / Tree_printer

Licence: unlicense
A Java class for printing binary trees as ASCII text

Programming Languages

java
68154 projects - #9 most used programming language

Projects that are alternatives of or similar to Tree printer

React Ui Tree
React tree component with drag & drop
Stars: ✭ 720 (+3500%)
Mutual labels:  tree
Modiscript
Acche din aa gaye
Stars: ✭ 888 (+4340%)
Mutual labels:  tree
Ascii Generator
ASCII generator (image to text, image to image, video to video)
Stars: ✭ 939 (+4595%)
Mutual labels:  ascii
Vue.d3.tree
Vue component to display tree based on D3.js layout.
Stars: ✭ 726 (+3530%)
Mutual labels:  tree
Cfonts
Sexy fonts for the console
Stars: ✭ 789 (+3845%)
Mutual labels:  ascii
Printthis
jQuery printing plugin; print specific elements on a page
Stars: ✭ 902 (+4410%)
Mutual labels:  printing
Terminaltables
Generate simple tables in terminals from a nested list of strings.
Stars: ✭ 685 (+3325%)
Mutual labels:  ascii
React Native Pathjs Charts
Android and iOS charts based on react-native-svg and paths-js
Stars: ✭ 877 (+4285%)
Mutual labels:  tree
Gtools
gevent tools
Stars: ✭ 7 (-65%)
Mutual labels:  tree
Metric Parser
📜 AST-based advanced mathematical parser written by Typescript.
Stars: ✭ 26 (+30%)
Mutual labels:  tree
Slug Generator
Slug Generator Library for PHP, based on Unicode’s CLDR data
Stars: ✭ 740 (+3600%)
Mutual labels:  ascii
Color Scripts
User contributed color scripts
Stars: ✭ 789 (+3845%)
Mutual labels:  ascii
Christmas Tree
ASCII christmas-tree with animations for 256 colored terminals
Stars: ✭ 23 (+15%)
Mutual labels:  tree
Coc Explorer
📁 Explorer for coc.nvim
Stars: ✭ 722 (+3510%)
Mutual labels:  tree
Sadconsole
A .NET ascii/ansi console engine written in C# for MonoGame and XNA. Create your own text roguelike (or other) games!
Stars: ✭ 853 (+4165%)
Mutual labels:  ascii
Encoding
Go package containing implementations of efficient encoding, decoding, and validation APIs.
Stars: ✭ 705 (+3425%)
Mutual labels:  ascii
Tablign
Aligns columns in your ASCII tables on the command line or in vim.
Stars: ✭ 18 (-10%)
Mutual labels:  ascii
Mapbox Gl Print Export For Port
Print/Export for Mapbox GL
Stars: ✭ 14 (-30%)
Mutual labels:  printing
Ascii canvas
ASCII canvas for drawing in console
Stars: ✭ 11 (-45%)
Mutual labels:  ascii
Tree layout
Algorithms to layout trees in a pretty manner.
Stars: ✭ 24 (+20%)
Mutual labels:  tree

tree_printer

A Java class for printing binary trees as ASCII text

It hasn't been optimized for run time efficiency, but since we're talking about printing in ASCII, I figured it's not going to be used on very large trees. It does have some nice features though.

  1. It makes efficient use of space in that a large subtree extends under a smaller one as much as possible.
  2. It's generic, working for any binary tree data objects, as long as you can provide functions (lambda functions will do) to get a nodes label as a String, and to get the left and right sub-nodes.
  3. There's a parameter to set the minimum horizontal space between node labels.
  4. Node labels are strings of arbitrary length.
  5. In addition to a method for printing a single tree, there's a method for printing a list of trees horizontally across the page (with a parameter for page width), using as many rows as necessary.
  6. There's an option to print trees with diagonal branches (using slash and backslash characters) or with horizontal branches (using ascii box drawing characters). The latter is more compact and makes tree levels more visually clear.
  7. It works.

Some demo/test programs are included.

The TreePrinter object has two methods for printing binary trees as ASCII text. PrintTree prints a single tree. PrintTrees prints a list of trees horizontally across the page, in multiple rows if necessary.

The TreePrinter object has a few settable parameters affecting how it prints trees. A positive integer parameter 'hspace' specifies the minimum number of horizontal spaces between any two node labels in the tree. A boolean parameter 'squareBranches' determines whether the tree is drawn with horizontal branches (using ascii box drawing characters) or diagonal branches (using slash and backslash characters). The boolean 'lrAgnostic' parameter only affects trees drawn with the ascii box drawing characters. Its effect is is that tree nodes with only a single subtree are drawn with a straight down vertical branch, providing no indication of whether it is a left or right subtree.

A few test/demo programs are included. For instance, the program EnumTrees can be used to print an enumeration of all binary trees of a given size. All trees of size 5, labeled with number words (one, two, etc) is printed as:

mvn compile 
mvn exec:java -Dexec.mainClass="EnumTrees"

This produces output like:

one           one         one          one        one       one         one       one          one      
  \             \           \            \          \         \           \         \            \      
  two           two         two          two        two      three       three      four         four   
    \             \           \            \          \       / \         / \       / \          / \    
   three         three        four         five       five  two four    two five  two five      /   \   
      \             \         / \          /          /           \         /       \        three  five
      four          five     /   \      three       four          five    four     three      /         
        \           /     three  five      \        /                                       two         
        five      four                     four  three                                                  



one        one       one       one         one        two             two          two          two   
  \          \         \         \           \        / \             / \          / \          / \   
  five       five      five      five        five    /   \           /   \       one four     one five
  /          /         /         /           /     one  three      one  three        / \          /   
two        two      three      four        four            \               \        /   \      three  
  \          \       / \       /           /               four            five  three  five      \   
 three       four  two four  two        three                \             /                      four
    \        /                 \         /                   five        four                         
    four  three               three    two                                                            



   two      three         three         three         three      four      four         four  
   / \       / \           / \           / \           / \       / \       / \          / \   
 one five  one four       /   \        two four      two five  one five  one five     two five
     /       \   \       /     \       /     \       /   /       \         \          / \     
   four      two five  one     five  one     five  one four      two      three      /   \    
   /                     \     /                                   \       /       one  three 
three                    two four                                 three  two                  



     four            four       five        five      five      five      five       five         five
     / \             / \        /           /         /         /         /          /            /   
    /   \           /   \     one         one       one       one       one        two          two   
 three  five     three  five    \           \         \         \         \        / \          / \   
  /               /             two         two      three      four      four    /   \       one four
one             two               \           \       / \       /         /     one  three        /   
  \             /                three        four  two four  two      three            \      three  
  two         one                   \         /                 \       /               four          
                                    four   three               three  two                             



    five        five      five      five         five        five          five
    /           /         /         /            /           /             /   
 three       three      four      four         four        four          four  
  / \         / \       /         /            /           /             /     
one four    two four  one       one          two        three         three    
  \         /           \         \          / \         /             /       
  two     one           two      three      /   \      one           two       
                          \       /       one  three     \           /         
                         three  two                      two       one         

RandomTree can be used to print a single randomly generated tree. The following is an example of the same tree printed 4 different ways, with horizontal spacing of 1 and of 3, and with diagonal and horizontal branches. To run this from the command line using maven type:

mvn compile 
mvn exec:java -Dexec.mainClass="RandomTree"

This produces output like:

                   27        
             ┌─────┴─────┐   
             13          29  
      ┌──────┴──────┐  ┌─┴─┐ 
      8             23 28  30
   ┌──┴──┐       ┌──┴──┐     
   4     11      21    26    
 ┌─┴─┐  ┌┴┐    ┌─┴─┐  ┌┘     
 2   5  9 12   18  22 24     
┌┴┐  └┐ └┐   ┌─┴─┐    └┐     
1 3   6  10  17  19    25    
      └┐    ┌┘   └┐          
       7    15    20         
          ┌─┴─┐              
          14  16             


                 27        
                / \        
               /   \       
              13    29     
             / \   / \     
            /   \ 28  30   
           /     \         
          /       \        
         /         \       
        /           \      
       8             23    
      / \           / \    
     /   \         /   \   
    4     11      /     \  
   / \   / \     21      26
  2   5 9   12  / \     /  
 / \   \ \     18  22  24  
1   3   6 10  / \       \  
         \   17  19      25
          7 /     \        
           15      20      
          / \              
         14  16            


                             27            
                    ┌────────┴────────┐    
                    13                29   
          ┌─────────┴─────────┐    ┌──┴──┐ 
          8                   23   28    30
     ┌────┴────┐         ┌────┴────┐       
     4         11        21        26      
  ┌──┴──┐    ┌─┴─┐    ┌──┴──┐     ┌┘       
  2     5    9   12   18    22    24       
┌─┴─┐   └┐   └┐    ┌──┴──┐        └┐       
1   3    6    10   17    19        25      
         └┐       ┌┘     └┐                
          7       15      20               
               ┌──┴──┐                     
               14    16                    


                      27         
                     / \         
                    /   \        
                   /     \       
                  /       \      
                 13        29    
                / \       / \    
               /   \     /   \   
              /     \   28    30 
             /       \           
            /         \          
           /           \         
          /             \        
         /               \       
        8                 23     
       / \               / \     
      /   \             /   \    
     /     \           /     \   
    4       11        /       \  
   / \     / \       21        26
  2   5   9   12    / \       /  
 / \   \   \       /   \     24  
1   3   6   10    18    22    \  
         \       / \           25
          7     /   \            
               17    19          
              /       \          
             15        20        
            / \                  
           /   \                 
          14    16               

There's a demo program that produces a tree diagram of all Collatz sequences (https://en.wikipedia.org/wiki/Collatz_conjecture) of a given length. This demonstrates an option to print trees in such a way that if there is only a single subtree, it is treated the same regardless of whether it is a left or right subtree. This produces output like:

                                                       1                                 
                                                       │                                 
                                                       2                                 
                                                       │                                 
                                                       4                                 
                                                       │                                 
                                                       8                                 
                                                       │                                 
                                                       16                                
                                 ┌─────────────────────┴─────────────────────┐           
                                 32                                          5           
                                 │                                           │           
                                 64                                          10          
                      ┌──────────┴──────────┐                      ┌─────────┴─────────┐ 
                     128                    21                     20                  3 
                      │                     │                      │                   │ 
                     256                    42                     40                  6 
           ┌──────────┴──────────┐          │             ┌────────┴────────┐          │ 
          512                    85         84            80                13         12
           │                     │          │             │                 │          │ 
          1024                  170        168           160                26         24
     ┌─────┴─────┐               │          │        ┌────┴────┐            │          │ 
    2048        341             340        336      320        53           52         48
     │           │          ┌────┴────┐     │        │         │        ┌───┴───┐      │ 
    4096        682        680       113   672      640       106      104      17     96
  ┌──┴──┐     ┌──┴──┐       │         │     │     ┌──┴──┐    ┌─┴─┐      │       │      │ 
 8192  1365  1364  227     1360      226   1344  1280  213  212  35    208      34    192
  │     │     │     │    ┌──┴──┐    ┌─┴─┐   │     │     │    │   │    ┌─┴─┐   ┌─┴─┐    │ 
16384  2730  2728  454  2720  453  452  75 2688  2560  426  424  70  416  69  68  11  384
Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].