php怎么打印二叉树
-
在PHP中,可以使用递归的方式来打印二叉树。以下是一个示例的代码:
“`php
class Node
{
public $left;
public $right;
public $data;public function __construct($data)
{
$this->data = $data;
$this->left = null;
$this->right = null;
}
}function printBinaryTree($root, $level = 0)
{
if ($root == null) {
return;
}// 打印右子树
printBinaryTree($root->right, $level + 1);// 打印当前节点
echo str_repeat(” “, 3 * $level); // 使用空格来表示树的层级关系
echo $root->data . “\n”;// 打印左子树
printBinaryTree($root->left, $level + 1);
}// 构建一个二叉树
$root = new Node(1);
$root->left = new Node(2);
$root->right = new Node(3);
$root->left->left = new Node(4);
$root->left->right = new Node(5);
$root->right->left = new Node(6);
$root->right->right = new Node(7);// 打印二叉树
printBinaryTree($root);
“`以上代码定义了一个`Node`类来表示二叉树的节点,其中包含左子节点、右子节点和节点数据。`printBinaryTree`函数使用递归的方式打印二叉树,先打印右子树,再打印当前节点,最后打印左子树。使用空格来表示节点的层级关系,便于观察二叉树的形状。
你可以根据需要修改节点的数据和二叉树的结构,然后调用`printBinaryTree`函数来打印二叉树。
2年前 -
在PHP中打印二叉树,您可以按照以下步骤进行:
1. 创建二叉树节点的类或结构体。每个节点应该具有一个值、指向左子节点的指针和指向右子节点的指针。
2. 创建一个函数来在二叉树中插入节点。该函数通过比较节点的值与当前节点的值,并根据比较结果递归调用自身来寻找正确的插入位置。
3. 创建一个函数来打印二叉树。该函数应该接受一个树节点作为参数,并通过递归遍历树的每个节点,然后打印该节点的值。
下面是一个示例代码:
“`php
class TreeNode
{
public $value;
public $left;
public $right;public function __construct($value)
{
$this->value = $value;
$this->left = null;
$this->right = null;
}
}class BinaryTree
{
public $root;public function __construct()
{
$this->root = null;
}public function insert($value)
{
$node = new TreeNode($value);
if ($this->root === null) {
$this->root = $node;
} else {
$this->insertNode($node, $this->root);
}
}private function insertNode($node, &$subtree)
{
if ($subtree === null) {
$subtree = $node;
} else {
if ($node->value < $subtree->value) {
$this->insertNode($node, $subtree->left);
} else {
$this->insertNode($node, $subtree->right);
}
}
}public function printTree($node)
{
if ($node !== null) {
$this->printTree($node->left);
echo $node->value . ” “;
$this->printTree($node->right);
}
}
}// 创建二叉树对象并插入节点
$tree = new BinaryTree();
$tree->insert(8);
$tree->insert(3);
$tree->insert(10);
$tree->insert(1);
$tree->insert(6);
$tree->insert(14);
$tree->insert(4);
$tree->insert(7);
$tree->insert(13);// 打印二叉树
$tree->printTree($tree->root);
“`上述代码将创建一个二叉树对象,并插入一些节点。然后调用`printTree()`函数来打印二叉树中的所有节点值。输出结果应该为`1 3 4 6 7 8 10 13 14`。
2年前 -
打印二叉树是一个常见的问题,在PHP语言中可以使用递归方法来实现。下面是一个详细的操作流程:
Step 1: 创建二叉树节点类
首先,我们需要创建一个二叉树节点类,用于表示二叉树的节点。“`php
class BinaryTreeNode {
public $val;
public $left;
public $right;public function __construct($val) {
$this->val = $val;
$this->left = null;
$this->right = null;
}
}
“`Step 2: 构建二叉树
接下来,我们需要构建一个二叉树,以便进行打印操作。可以使用以下代码来构建一个简单的二叉树。“`php
$root = new BinaryTreeNode(1);
$root->left = new BinaryTreeNode(2);
$root->right = new BinaryTreeNode(3);
$root->left->left = new BinaryTreeNode(4);
$root->left->right = new BinaryTreeNode(5);
$root->right->left = new BinaryTreeNode(6);
$root->right->right = new BinaryTreeNode(7);
“`Step 3: 打印二叉树
下面是打印二叉树的递归函数,可以使用前序、中序和后序三种方式进行打印。“`php
function printBinaryTree($node, $order) {
if ($node == null) {
return;
}if ($order == ‘preorder’) {
echo $node->val . ” “; // 先打印根节点
printBinaryTree($node->left, $order); // 再打印左子树
printBinaryTree($node->right, $order); // 最后打印右子树
} elseif ($order == ‘inorder’) {
printBinaryTree($node->left, $order); // 先打印左子树
echo $node->val . ” “; // 再打印根节点
printBinaryTree($node->right, $order); // 最后打印右子树
} elseif ($order == ‘postorder’) {
printBinaryTree($node->left, $order); // 先打印左子树
printBinaryTree($node->right, $order); // 再打印右子树
echo $node->val . ” “; // 最后打印根节点
}
}
“`Step 4: 调用函数打印二叉树
最后,我们可以调用上述函数来打印二叉树,根据需要选择打印的方式。“`php
echo “前序遍历:”;
printBinaryTree($root, ‘preorder’);
echo “\n”;echo “中序遍历:”;
printBinaryTree($root, ‘inorder’);
echo “\n”;echo “后序遍历:”;
printBinaryTree($root, ‘postorder’);
echo “\n”;
“`这样,就能够通过PHP代码打印出二叉树了。
2年前