Solve the idea of tree-like data (adjacency list) with List

Source: Internet
Author: User
Using list to process tree data (adjacency list)
An existing array
$d = Array (
Array (' Announcements ', 1, 0),
Array (' article ', 2, 0),
Array (' Article 1 ', 3, 2),
Array (' Article 2 ', 4, 2),
Array (' Article 1 comments ', 5, 3),
Array (' Article 2 Comments ', 6, 4),
Array (' Article 1 reviews 1 ', 7, 3),
Array (' Article 1 comment comments ', 8, 5),
);
Expect the following output
Notice
Article
Article 1
Article 1 comments
Article 1 Comment comments
Posts 1 Comments 1
Article 2
Article 2 Comments

So you can
foreach ($d as $t) list ($a [$pid] [$id], $id, $pid) = $t;
Get
Array
(
[0] = = Array
(
[1] = = Announcement
[2] = = Articles
)

[2] = = Array
(
[3] = = Article 1
[4] = = Article 2
)

[3] = = Array
(
[5] = + Article 1 comments
[7] = + Article 1 comments 1
)

[4] = = Array
(
[6] = + Article 2 comments
)

[5] = = Array
(
[8] = + Article 1 Comment Comment
)

)
As you can see, the data is clustered in column 3rd.
Then a recursive function can be used to achieve the presentation of the data
function foo ($ar, $pid =0, $deep =0) {
foreach ($ar [$pid] as $k = = $v) {
printf ("%s%s\n", Str_repeat (", $deep), $v);
if (Isset ($ar [$k]) foo ($ar, $k, $deep +2);
}
}
Call Foo ($a);

------Solution--------------------
Moderators are a great guy.
------Solution--------------------

The owner of the infinite-level tree has a unique feeling.
Every time I see a new harvest.
------Solution--------------------

------Solution--------------------
Front Row Study!
------Solution--------------------
Study. Oh

------Solution--------------------

------Solution--------------------
That's how it turns out.
------Solution--------------------
Really concise, study.
------Solution--------------------
Good writing, study
------Solution--------------------
 static void Main (string[] args) 
{
Double A, B, C, p, h, area;
Console.Write ("Please enter the edge of the Triangle A:");
String s = Console.ReadLine ();
A = double. Parse (s);
Console.Write ("Please enter the edge of the Triangle B:");
S = console.readline ();
B = Double. Parse (s);
Console.Write ("Please enter the edge of the Triangle C:");
S = console.readline ();
C = Double. Parse (s);
if (a > 0 && B > 0 && C > 0 && a + B > C && a + C > B & & B + C > a)
{
Console.WriteLine (three sides of the triangle are: a={0},b={1},c={2} ", A, B, c);
p = A + B + C;
  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.