views:

587

answers:

2

Hi, I have a binary tree in unordered list that looks like this:

<ul>
<li>1
 <ul> 
  <li>2
   <ul> 
    <li>4
     <ul> 
      <li>8</li> 
      <li>--</li>
     </ul> 
    </li> 
    <li>5</li> 
   </ul> 
  </li> 
  <li>3
   <ul> 
    <li>6</li> 
    <li>7</li> 
   </ul> 
  </li> 
 </ul> 
</li>

Where -- is a empty space (to differ left/right child).

It displays as a classic unordered list. But that's hard to read and navigate through. I need a horizontal tree that looks like this:

http://www.knowledgerush.com/wiki%5Fimage/d/df/Binary%5Ftree.png

It can be with or without the lines between the nodes.

My question is - can this be done via html/css alone or do I need to use javascript for this?

+1  A: 

Sounds like a great idea, but I see it very difficult to achieve.

  • Lines vary in angle, so forget about doing it without some special logic (not achievable through HTML/CSS alone)
  • Child nodes seem to occupy the same vertical space as other parent nodes (see for example nodes 5 and 7), while others don't (see nodes 4 and 5). That's also some special ordering.

Anyway, you try to set each LI to float left and have a width of 50%, so each new level will be narrower and narrower - haven't tried yet.

Also, you might find this plugin interesting, although it doesn't use ULs but DIVs and uses jQuery: http://www.ajaxupdates.com/jquery-binary-tree-plugin/

Seb
I tried the plugin - it's completely useless, it just displays it in order of the divs without any logic. I'll try the float and width of 50% ... if it doesnt work, I'll have to write something in javascript
Smaug
okay, the 50% and float works ... however after 4-6 levels the width becomes very narrow ... so I may have to use quite big page (or rather a containing div with overflow:scrollable)
Smaug
Yeah, available space will be reduced exponentially... On the other hand, if you need plenty of space consider making that available as an image instead, preprocessing it server-side. Or maybe even a PDF. Having a huge wide page isn't great for users with resolution @ 1024x768 :)
Seb
Yeah, pre-processing might be the best idea ... I need this mainly for users to see reports of their mlm downline - so some extra processing time won't hurt.
Smaug
+1  A: 

I did something very similar to this a while back, and examined the various options available. I wanted to be able to update the diagram in real time on an AJAX page, and ended up generating and modifying a SVG graphic with Javascript code. Firefox handled the resulting page extremely well and responsively, and I wasn't that worried that IE didn't cope with it at all.

Since then, we have had more developments with HTML5 and in particular the new canvas tag. This sounds to be ideal for this sort of thing, and might be worth an experiment.

Good luck!

Tim
yes, I've been dying to try HTML5 and canvas tag - however I need this to work in every browser on every machine so ...
Smaug