Skip to content

0xShady/fancy_tree

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

19 Commits
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

Fancy Tree

The right way to log a Binary search tree

sample

How to use fancy tree

In fancy_tree.hpp line:9 include the header file with the definition of your node (test.hpp in our case).

//header file that contains Node definition
#include "test.hpp"

Replace the flowing macros according to your Node definition fancy_tree.hpp line:20

// Node class
#define NODE Node<T>

// Node class attributes
#define LEFT _left				// left attribute
#define RIGHT _right			// right attribute
#define PARENT _parent			// parent attribute (can be ignnored)
#define CONTENT _value			// data attribute + element of pair(if exists)

According to this defnition in our test file test.hpp

template <typename T>
class Node {

    public:
        T           _value;
        Node<T>     *_left;
        Node<T>     *_right;
        Node<T>     *_parent;

        Node(T value)
        {
            this->_value = value;
            this->_left = nullptr;
            this->_right = nullptr;
            this->_parent = nullptr;
        }
};

By running the following test test.cpp

int main()
{
    Node<int> *root = new Node<int>(38);
    root->_left = new Node<int>(26);
    root->_right = new Node<int>(70);
    root->_left->_left = new Node<int>(6);
    root->_left->_right = new Node<int>(15);
    root->_right->_left = new Node<int>(43);
    root->_right->_right = new Node<int>(88);
    root->_left->_left->_left = new Node<int>(3);
    root->_left->_left->_right = new Node<int>(15);
    root->_left->_right->_left = new Node<int>(27);
    root->_left->_right->_right = new Node<int>(36);
    root->_right->_left->_left = new Node<int>(40);
    root->_right->_right->_left = new Node<int>(85);
    root->_right->_right->_right = new Node<int>(89);
    root->_left->_left->_right->_left = new Node<int>(12);

    // construct and object of type FancyTree
    fancy_tree<int> tree; 

    // to print in a vrtical view (root on the top mid of the tree)
    tree.print_tree(root, V_VIEW);

    // to print in a horizontal view (root on the left mid of the tree)
    tree.print_tree(root, H_VIEW);
}

Output

  • Vertical view print_tree(root, V_VIEW);
                                                               β”Œβ”€β”€β”€β”€β”
                                   β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€ 38 β”œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
                                   β”‚                           β””β”€β”€β”€β”€β”˜                           β”‚
                                   β”‚                                                            β”‚
                               β”Œβ”€β”€β”€β”΄β”                                                          β”Œβ”΄β”€β”€β”€β”
                   β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€ 26 β”œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”                                  β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€ 70 β”œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
                   β”‚           β””β”€β”€β”€β”€β”˜           β”‚                                  β”‚           β””β”€β”€β”€β”€β”˜           β”‚
                   β”‚                            β”‚                                  β”‚                            β”‚
               β”Œβ”€β”€β”€β”΄β”                          β”Œβ”΄β”€β”€β”€β”                          β”Œβ”€β”€β”€β”΄β”                          β”Œβ”΄β”€β”€β”€β”
           β”Œβ”€β”€β”€β”€ 6  β”œβ”€β”€β”€β”                  β”Œβ”€β”€β”€β”€ 15 β”œβ”€β”€β”€β”                  β”Œβ”€β”€β”€β”€ 43 β”‚                      β”Œβ”€β”€β”€β”€ 88 β”œβ”€β”€β”€β”
           β”‚   β””β”€β”€β”€β”€β”˜   β”‚                  β”‚   β””β”€β”€β”€β”€β”˜   β”‚                  β”‚   β””β”€β”€β”€β”€β”˜                      β”‚   β””β”€β”€β”€β”€β”˜   β”‚
           β”‚            β”‚                  β”‚            β”‚                  β”‚                               β”‚            β”‚
       β”Œβ”€β”€β”€β”΄β”          β”Œβ”΄β”€β”€β”€β”          β”Œβ”€β”€β”€β”΄β”          β”Œβ”΄β”€β”€β”€β”          β”Œβ”€β”€β”€β”΄β”                          β”Œβ”€β”€β”€β”΄β”          β”Œβ”΄β”€β”€β”€β”
       β”‚ 3  β”‚        β”Œβ”€β”€ 15 β”‚          β”‚ 27 β”‚          β”‚ 36 β”‚          β”‚ 40 β”‚                          β”‚ 85 β”‚          β”‚ 89 β”‚
       β””β”€β”€β”€β”€β”˜        β”‚ β””β”€β”€β”€β”€β”˜          β””β”€β”€β”€β”€β”˜          β””β”€β”€β”€β”€β”˜          β””β”€β”€β”€β”€β”˜                          β””β”€β”€β”€β”€β”˜          β””β”€β”€β”€β”€β”˜
                     β”‚
                   β”Œβ”€β”΄β”€β”€β”
                   β”‚ 12 β”‚
                   β””β”€β”€β”€β”€β”˜
  • Horizontal view print_tree(root, H_VIEW);
            β”Œβ”€β”€β”€89
        β”Œβ”€β”€β”€88
        β”‚   └───85
    β”Œβ”€β”€β”€70
    β”‚   └───43
    β”‚       └───40
───38
    β”‚       β”Œβ”€β”€β”€36
    β”‚   β”Œβ”€β”€β”€15
    β”‚   β”‚   └───27
    └───26
        β”‚   β”Œβ”€β”€β”€15
        β”‚   β”‚   └───12
        └───6
            └───3

About

Stop printing your tree the ugly way.

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages