Home  >  Article  >  Backend Development  >  PHP data structure: exploration of tree structure, mastering the organization of hierarchical data

PHP data structure: exploration of tree structure, mastering the organization of hierarchical data

WBOY
WBOYOriginal
2024-06-02 19:28:021142browse

Tree structure is a non-linear structure that organizes data hierarchically, and can be represented and traversed recursively or iteratively in PHP. Representation methods include recursion (using class) and iteration (using array); traversal methods include recursive traversal and iterative traversal (using stack). In the actual case, the file system directory tree is efficiently organized using a tree structure to facilitate browsing and obtaining information.

PHP data structure: exploration of tree structure, mastering the organization of hierarchical data

PHP tree structure exploration: a powerful tool for hierarchical data organization

The tree structure is a non-linear data structure. Organizing data in a hierarchical manner is very suitable for data that needs to express hierarchical relationships. In PHP, tree structures can be represented and traversed using either recursion or iteration.

Representing a tree structure

There are two main ways to represent a tree structure in PHP:

Recursive representation:

class Node {
  public $value;
  public $children = [];

  public function __construct($value) {
    $this->value = $value;
  }

  public function addChild(Node $child) {
    $this->children[] = $child;
  }
}

Iterative representation (using array):

$tree = [
  'value' => 'Root',
  'children' => [
    [
      'value' => 'Child 1',
      'children' => []
    ],
    [
      'value' => 'Child 2',
      'children' => [
        'value' => 'Grandchild'
      ]
    ]
  ]
];

Traversing the tree structure

The following two methods can be used Traverse the tree structure:

Recursive traversal:

function traverseRecursively($node) {
  echo $node->value . PHP_EOL;
  foreach ($node->children as $child) {
    traverseRecursively($child);
  }
}

Iterative traversal (using stack):

function traverseIteratively($node) {
  $stack = [$node];
  while (!empty($stack)) {
    $current = array_pop($stack);
    echo $current->value . PHP_EOL;
    foreach (array_reverse($current->children) as $child) {
      $stack[] = $child;
    }
  }
}

Practical case: File system directory tree

Consider a file system directory tree, where each directory contains subdirectories and files. This data structure can be organized and represented efficiently using a tree structure.

class Directory {
  public $name;
  public $children = [];

  public function __construct($name) {
    $this->name = $name;
  }

  public function addChild(Node $child) {
    $this->children[] = $child;
  }
}

$root = new Directory('/');

$dir1 = new Directory('dir1');
$dir2 = new Directory('dir2');
$dir3 = new Directory('dir3');

$file1 = new File('file1.txt');
$file2 = new File('file2.php');

$dir1->addChild($file1);
$dir2->addChild($file2);
$root->addChild($dir1);
$root->addChild($dir2);
$root->addChild($dir3);

traverseRecursively($root);

By using the tree structure, we can easily browse and organize the file system directory tree and obtain the required information efficiently.

The above is the detailed content of PHP data structure: exploration of tree structure, mastering the organization of hierarchical data. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn