473,394 Members | 1,769 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

Mind bender array tree flattening

zorgi
431 Expert 256MB
I have multidimensional (tree) array of form like this:

Expand|Select|Wrap|Line Numbers
  1. $a = array
  2. (
  3.     "t" => array("b")
  4.     "d" => array(
  5.                  "p" => array("b")
  6.                  "n" => array(
  7.                               "p" => array ( "b")
  8.                               )
  9.                 )
  10.     "p" => array("b")
  11.     "n" => array(
  12.                  "p" => array("b")
  13.                  )
  14. )
  15.  
That needs converting into 2 dimensional (tree respecting) array like this:

Expand|Select|Wrap|Line Numbers
  1. $a = array
  2. (
  3.     array("t", "b"),
  4.     array("d", "p", "b"),
  5.     array("d", "n", "p", "b"),
  6.     array("p", "b"),
  7.     array("n", "p", "b")
  8. )
  9.  
Any ideas!? Solve this and beer and pizza is on me :)
May 14 '10 #1

✓ answered by jkmyoung

Looks like a depth-first search.
Expand|Select|Wrap|Line Numbers
  1. {
  2.   current node = root
  3.   current path = nothing
  4.   PrintPath(node, path)
  5. }
  6.  
  7. PrintPath(node, path){
  8.   if no children -> print path and return
  9.   foreach childNode on current node{
  10.     PrintPath(childNode, path + childnode)
  11.   }
  12. }
path would be a dynamically expanding array. path is passed by value, not reference, so depending on your language, you may have to copy the array.

6 3324
zorgi
431 Expert 256MB
Hm maybe this should be moved to algorithms
May 14 '10 #2
Markus
6,050 Expert 4TB
I'll move it to algorithms. :)
May 14 '10 #3
Markus
6,050 Expert 4TB
*goes to drawing board*
May 14 '10 #4
jkmyoung
2,057 Expert 2GB
Looks like a depth-first search.
Expand|Select|Wrap|Line Numbers
  1. {
  2.   current node = root
  3.   current path = nothing
  4.   PrintPath(node, path)
  5. }
  6.  
  7. PrintPath(node, path){
  8.   if no children -> print path and return
  9.   foreach childNode on current node{
  10.     PrintPath(childNode, path + childnode)
  11.   }
  12. }
path would be a dynamically expanding array. path is passed by value, not reference, so depending on your language, you may have to copy the array.
May 14 '10 #5
zorgi
431 Expert 256MB
Language is php. I came up with this:
May 14 '10 #6
zorgi
431 Expert 256MB
@jkmyoung
Thanks to your post I came up with much neater solution.

Expand|Select|Wrap|Line Numbers
  1. printPath($a);
  2.  
  3. function printPath($node, $path = array()){
  4.     if(!is_array($node)){
  5.         $path[] = $node;
  6.         print_r($path);    
  7.         return;
  8.     }
  9.  
  10.     foreach($node as $key => $child_node){
  11.         $p = $path;        
  12.         if(is_array($child_node)){
  13.             $p[] = $key;
  14.             printPath($child_node, $p);
  15.         }else{
  16.             printPath($child_node, $p);    
  17.         }            
  18.     }
  19. }
  20.  
Thanx. Beer and pizza is on me if we ever meet :)
May 15 '10 #7

Sign in to post your reply or Sign up for a free account.

Similar topics

0
by: me | last post by:
I've posted this in the microsoft news group but just noticed the comp newsgroups. What's the difference anyways? This one is a tricky one so I'm interested in seeing what all you gurus have to...
1
by: googleo | last post by:
Hi, in my application I want to handle and store data in a hierarchic data structure. For example: persons who manage houses; houses have various numbers of floors; floors have various numbers...
9
by: Dadi | last post by:
Hi, I can make a simple initialization work like this: Object ONE_ROW = {{"Vodafone", "5550160100197016"}}; But, now I want to create another array that consists of multiple copies of...
6
by: Yanhao Zhu | last post by:
Hi, all, If I have an array like int m = new int { 0, 1, 2, 3 }, is there a way I can separate the array into two, like int m01 = somefunction?(m,0,2) // m01 will hold 1st and 2nd items in...
3
by: Toxick | last post by:
Hello experts, I'm a total C# noob with a total C# noob question. I've been Serializing in C++ (MFC) and writing data to std::fstreams for quite some time, so maybe I'm not understanding...
4
by: leonardo.calado | last post by:
Hi, I have a simple problem, but I don't found a solution for my problem. I'm try, try and not found. If anybody help me, I very, very thankful for this help. Here is my problem: I have a...
1
by: rflloyd | last post by:
I wish to create a property of a control which is an array of Images, such that I can add, edit and delete images in the VS2005 property window. I've created the property as: private Image...
3
by: Kailash Nadh | last post by:
Hello all. I have this tab formatted hierarchical structure. -------------------- 0 A 1 B 2 C 2 D 1 E --------------------
4
by: BravoFoxtrot | last post by:
Hi, I'm trying to build an index into a multi dimensional associative array. I may not know how many dimensions there are so i want to pass the array indexes as a variable. $arrayToAccess =...
5
by: zr | last post by:
Hi, Is there a way to initialize a std::tr1::array with a pre-allocated built-in array in a copy-less assignment, such that both will point to the same memory? Vice-versa is easy to do, simply...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.