Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to display a binary search tree using CSS, HTML and a bit of Javascript?

I want to "paint" the tree on screen using CSS and HTML and not represent it in any way or data structure ...

like image 480
mythbu Avatar asked Feb 21 '13 10:02

mythbu


People also ask

How do you display a binary search tree?

Displaying binary tree Binary tree can be displayed in three forms – pre-order, in-order and post-order. Pre-order displays root node, left node and then right node. In-order displays left node, root node and then right node. Post-order displays left node, right node and then root node.

What is a binary search tree in JavaScript?

A Binary Search tree is a binary tree in which nodes that have lesser value are stored on the left while the nodes with a higher value are stored at the right. Now let's see an example of a Binary Search Tree node: Javascript.

Is valid binary search tree JavaScript?

To verify if a tree is a valid binary search tree: Define the min and max value the current node can have. If a node's value is not within those bounds, return false. Recursively validate the node's left children, with the max bound set to the node's value.


2 Answers

Unlimit binary and unilevel tree with css

Demo: http://jsfiddle.net/Limitlessisa/5Lhb0ron/

Html:

<div class="tree">

        <ul>
            <li>
                <div><input type="checkbox"> Main <br/> <button> Test Btn </button></div>
                <ul>
                    <li>
                        <div><input type="checkbox"> Sub-1</div>
                    </li>
                    <li>
                        <div><input type="checkbox"> Sub-2</div>
                        <ul>
                            <li>
                                <div><input type="checkbox"> Sub-2-1</div>
                            </li>
                            <li>
                                <div><input type="checkbox"> Sub-2-2</div>
                            </li>
                        </ul>  
                    </li>
                </ul>

            </li>
        </ul>

    </div>

Css:

<style>
.tree ul {
    padding-top: 20px; position: relative;

    transition: all 0.5s;
    -webkit-transition: all 0.5s;
    -moz-transition: all 0.5s;
}

.tree li {
    float: left; text-align: center;
    list-style-type: none;
    position: relative;
    padding: 20px 5px 0 5px;

    transition: all 0.5s;
    -webkit-transition: all 0.5s;
    -moz-transition: all 0.5s;
}

.tree li::before, .tree li::after{
    content: '';
    position: absolute; top: 0; right: 50%;
    border-top: 1px solid #ccc;
    width: 50%; height: 20px;
}
.tree li::after{
    right: auto; left: 50%;
    border-left: 1px solid #ccc;
}

.tree li:only-child::after, .tree li:only-child::before {
    display: none;
}

.tree li:only-child{ padding-top: 0;}

.tree li:first-child::before, .tree li:last-child::after{
    border: 0 none;
}
.tree li:last-child::before{
    border-right: 1px solid #ccc;
    border-radius: 0 5px 0 0;
    -webkit-border-radius: 0 5px 0 0;
    -moz-border-radius: 0 5px 0 0;
}
.tree li:first-child::after{
    border-radius: 5px 0 0 0;
    -webkit-border-radius: 5px 0 0 0;
    -moz-border-radius: 5px 0 0 0;
}
.tree ul ul::before{
    content: '';
    position: absolute; top: 0; left: 50%;
    border-left: 1px solid #ccc;
    width: 0; height: 20px;
}
.tree li div{
    border: 1px solid #ccc;
    padding: 5px 10px;
    text-decoration: none;
    color: #666;
    font-family: arial, verdana, tahoma;
    font-size: 11px;
    display: inline-block;

    border-radius: 5px;
    -webkit-border-radius: 5px;
    -moz-border-radius: 5px;

    transition: all 0.5s;
    -webkit-transition: all 0.5s;
    -moz-transition: all 0.5s;
}
.tree li div:hover, .tree li div:hover+ul li div {
    background: #c8e4f8; color: #000; border: 1px solid #94a0b4;
}
.tree li div:hover+ul li::after, 
.tree li div:hover+ul li::before, 
.tree li div:hover+ul::before, 
.tree li div:hover+ul ul::before{
    border-color:  #94a0b4;
}

</style>
like image 165
Limitless isa Avatar answered Oct 11 '22 18:10

Limitless isa


A very simple way of creating a tree-like structure with HTML and CSS is nesting <div>s
Each div represents a node, and can have multiple nodes inside:

<div> //root
    <div> //child 1
        <div> //child 1.1
            <div></div> //child 1.1.1
            <div></div> //child 1.1.2
            <div></div> //child 1.1.3
        </div>
        <div></div> //child 1.2
    </div>
    <div></div> //child 2
</div>

Then you can add a margin-top to all divs, so that they appear under its parent div.

A JSFiddle: http://jsfiddle.net/VxRmc/

I've added percentage widths to each div. You can calculate widths if you know how many childs a node has. Or you can use fixed widths...

Yeah, it's not a beautiful tree, but it can't be simpler.

like image 37
Salvatorelab Avatar answered Oct 11 '22 18:10

Salvatorelab