Task: Visualize a tree


C++: #include <functional>
#include <iostream>
#include <random>

class BinarySearchTree {
private:
    struct Node {
        int key;
        Node *left, *right;

        Node(int k) : key(k), left(nullptr), right(nullptr) {
            
        }
    } *root;

public:
    BinarySearchTree() : root(nullptr) {
        
    }

    bool insert(int key) {
        if (root == nullptr) {
            root = new Node(key);
        } else {
            auto n = root;
            Node *parent;
            while (true) {
                if (n->key == key) {
                    return false;
                }

                parent = n;

                bool goLeft = key < n->key;
                n = goLeft ? n->left : n->right;

                if (n == nullptr) {
                    if (goLeft) {
                        parent->left = new Node(key);
                    } else {
                        parent->right = new Node(key);
                    }
                    break;
                }
            }
        }
        return true;
    }

    friend std::ostream &operator<<(std::ostream &, const BinarySearchTree &);
};

template<typename T>
void display(std::ostream &os, const T *n) {
    if (n != nullptr) {
        os << "Node(";

        display(os, n->left);

        os << ',' << n->key << ',';

        display(os, n->right);

        os << ")";
    } else {
        os << '-';
    }
}

std::ostream &operator<<(std::ostream &os, const BinarySearchTree &bst) {
    display(os, bst.root);
    return os;
}

int main() {
    std::default_random_engine generator;
    std::uniform_int_distribution<int> distribution(0, 200);
    auto rng = std::bind(distribution, generator);

    BinarySearchTree tree;

    tree.insert(100);
    for (size_t i = 0; i < 20; i++) {
        tree.insert(rng());
    }
    std::cout << tree << '\n';

    return 0;
}





PHP: <?php

function printTree( array $tree , string $key = "." , string $stack = "" , $first = TRUE , $firstPadding = NULL )
{
    if ( gettype($tree) == "array" )
    {
        if($firstPadding === NULL) $firstPadding = ( count($tree)>1 ) ? "│   " : "    " ;
        echo   $key . PHP_EOL ;
        foreach ($tree as $key => $value) {
            if ($key === array_key_last($tree)){
                echo (($first) ? "" : $firstPadding ) . $stack . "└── ";
                $padding = "    ";
                if($first) $firstPadding = "    ";
            }
            else {
                echo (($first) ? "" : $firstPadding ) . $stack . "├── ";
                $padding = "│   ";
            }
            if( is_array($value) )printTree( $value , $key ,   $stack . (($first) ? "" : $padding ) , FALSE , $firstPadding );
            else echo $key . " -> " . $value . PHP_EOL;
        }
    }
    else echo $tree . PHP_EOL;
}



$sample_array_1 = 
[
    0 => [
        'item_id' => 6,
        'price' => "2311.00",
        'qty' => 12,
        'discount' => 0
    ],
    1 => [
        'item_id' => 7,
        'price' => "1231.00",
        'qty' => 1,
        'discount' => 12
    ],
    2 => [
        'item_id' => 8,
        'price' => "123896.00",
        'qty' => 0,
        'discount' => 24
    ]
];
$sample_array_2 = array(
    array(
         "name"=>"John",
        "lastname"=>"Doe",
        "country"=>"Japan",
        "nationality"=>"Japanese",
        "job"=>"web developer",
        "hobbies"=>array(
                "sports"=>"soccer",
                "others"=>array(
                        "freetime"=>"watching Tv"
                )
        )

    )
);
$sample_array_3 = [
    "root" => [
        "first_depth_node1" =>[
            "second_depth_node1",
            "second_depth_node2" => [
                "third_depth_node1" ,
                "third_depth_node2" ,
                "third_depth_node3" => [
                    "fourth_depth_node1",
                    "fourth_depth_node2",
                ]
                ],
            "second_depth_node3",
        ] ,
        "first_depth_node2" => [
            "second_depth_node4" => [ "third_depth_node3" => [1]]
        ]
        ]
];
$sample_array_4 = [];
$sample_array_5 = ["1"];
$sample_array_5 = ["1"];
$sample_array_6 = [
    "T",
    "Ta",
    "Tad",
    "Tada",
    "Tadav",
    "Tadavo",
    "Tadavom",
    "Tadavomn",
    "Tadavomni",
    "Tadavomnis",
    "TadavomnisT",
];


printTree($sample_array_1);
echo PHP_EOL . "------------------------------" . PHP_EOL;
printTree($sample_array_2);
echo PHP_EOL . "------------------------------" . PHP_EOL;
printTree($sample_array_3);
echo PHP_EOL . "------------------------------" . PHP_EOL;
printTree($sample_array_4);
echo PHP_EOL . "------------------------------" . PHP_EOL;
printTree($sample_array_5);
echo PHP_EOL . "------------------------------" . PHP_EOL;
printTree($sample_array_6);


?>