Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a tree structure or algorithm to shuffle around levels in a tree?

I have what I think is an interesting problem.

Basically, I have a list of Items, where each Item has a fixed set of meta-data, of different values.

For example:

  • Item 1: {Type = "Text", Author = "User A", Edited Date = "03/03/2003"}
  • Item 2: {Type = "Table", Author = "User A", Edited Date = "04/05/2006"}
  • Item 3: {Type = "Image", Author = "User B", Edited Date = "05/05/2005"}
  • Item 4: {Type = "Text", Author = "User B", Edited Date = "05/07/2007"}

Now, as it stands, that list of items is flattened and presented in a table.

However, we would like to find a way to allow the users to browse it in a tree, but with the added flexibility that they can 'pivot' the order each of the meta-data tags appears in the tree.

So, initially it might look like:

Items
+ Table
  + User A
    + 04/05/2006
      -> Item 2
    -> Item 2
  -> Item 2
+ Text
  + User A
    + 03/03/2003
      -> Item 1
    -> Item 1
  + User B
    + 05/07/2007
      -> Item 4
    -> Item 4
  -> Item 1
  -> Item 4
+ Image
  ..

However, suppose instead, a user wants to flip it round and view all items related to a particular user:

Items
+ User A
  + Text
  + Table
  -> Item 1
  -> Item 2
+ User B
  + Image
  + Text
  -> Item 3
  -> Item 4

And so on.

I hope that makes sense.

So, what I'm wondering therefore, is if there is a best practice approach to achieving this at low cost? The result of each 'flip/shuffle/pivot' is nicely represented in a tree, so obviously the first thought is that when a user requests to change the representation, a new tree could be generated of the list of items as required. However, I was hoping perhaps there may be a better way, simply rotating a single tree etc.

Also, is this something that could be done computationally cheaply in JavaScript on the user's browser, if the backend were to just simply return a flat list of items?

Many thanks & kind regards,

Jamie

like image 214
Jay Avatar asked Aug 18 '11 15:08

Jay


1 Answers

You want to present elements in a tree structure, but with a variable tree depth and changing tree branching: I doubt a tree structure is actualy what you want.

I think you should consider instead that the world is flat (like in your table). A javascript database could help (there is http://taffydb.com/)

Still considering the world is flat, you could also create a signature function that returns a string

separator="µ"; //TODO Find something better
function signature() {
  return item.Type + separator + item.Author + separator + item.EditedDate;
}


assert(item1.signature == "TextµUser Aµ03/03/2003")

Then you store your objects in a simple dictionary using this signature as the key.

And then, you can perform a regexp match on the keys to get the objects you want. First, edit the signature function to return "([^separator]+)" if the corresponding item property is undefined.

assert ({Type="Text"}.signature() == "Textµ[^µ]+µ[^µ]+")

function find(filterItem) {
  retval= = new generic.list();
  for (var k in dict.keys()) {
    if (k.match(regexp)) {
      retval.add(dcit[k]);
    }
  }
}

I have no idea whether this is faster than browsing all elements though.

like image 81
rds Avatar answered Oct 22 '22 20:10

rds