二叉树排序 php,PHP Class&Object -- PHP 自排序二叉树的深入解析

本文介绍了一种使用二叉树实现自排序的方法。在每个节点中,所有满足特定条件的元素都被安排在左子节点及其后代中。插入新元素时,从根节点开始,根据值的大小确定其在树中的位置。读取数据时,通过按序遍历二叉树即可得到有序序列。示例代码展示了如何创建并操作这个自排序树,以及如何返回排序后的元素值。
摘要由CSDN通过智能技术生成

在节点之间再应用一些排序逻辑,二叉树就能提供出色的组织方式。对于每个节点,都让满足所有特定条件的元素都位于左节点及其子节点。在插入新元素时,我们需要从树的第一个节 点(根节点)开始,判断它属于哪一侧的节点,然后沿着这一侧找到恰当的位置,类似地,在读取数据时,只需要使用按序遍历方法来遍历二叉树。

ob_start();

// Here we need to include the binary tree class

Class Binary_Tree_Node() {

// You can find the details at

}

ob_end_clean();

// Define a class to implement self sorting binary tree

class Sorting_Tree {

// Define the variable to hold our tree:

public $tree;

// We need a method that will allow for inserts that automatically place

// themselves in the proper order in the tree

public function insert($val) {

// Handle the first case:

if (!(isset($this->tree))) {

$this->tree = new Binary_Tree_Node($val);

} else {

// In all other cases:

// Start a pointer that looks at the current tree top:

$pointer = $this->tree;

// Iteratively search the tree for the right place:

for(;;) {

// If this value is less than, or equal to the current data:

if ($val <= $pointer->data) {

// We are looking to the left ... If the child exists:

if ($pointer->left) {

// Traverse deeper:

$pointer = $pointer->left;

} else {

// Found the new spot: insert the new element here:

$pointer->left = new Binary_Tree_Node($val);

break;

}

} else {

// We are looking to the right ... If the child exists:

if ($pointer->right) {

// Traverse deeper:

$pointer = $pointer->right;

} else {

// Found the new spot: insert the new element here:

$pointer->right = new Binary_Tree_Node($val);

break;

}

}

}

}

}

// Now create a method to return the sorted values of this tree.

// All it entails is using the in-order traversal, which will now

// give us the proper sorted order.

public function returnSorted() {

return $this->tree->traverseInorder();

}

}

// Declare a new sorting tree:

$sort_as_you_go = new Sorting_Tree();

// Let's randomly create 20 numbers, inserting them as we go:

for ($i = 0; $i < 20; $i++) {

$sort_as_you_go->insert(rand(1,100));

}

// Now echo the tree out, using in-order traversal, and it will be sorted:

// Example: 1, 2, 11, 18, 22, 26, 32, 32, 34, 43, 46, 47, 47, 53, 60, 71,

//   75, 84, 86, 90

echo '

', implode(', ', $sort_as_you_go->returnSorted()), '

';

?>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是 PHP 实现二叉树的基本代码: ``` class TreeNode { public $value; public $left; public $right; public function __construct($val) { $this->value = $val; $this->left = null; $this->right = null; } } class BinaryTree { public $root; public function __construct() { $this->root = null; } public function insert($val) { $new_node = new TreeNode($val); if ($this->root == null) { $this->root = $new_node; return; } $current = $this->root; while (true) { if ($val < $current->value) { if ($current->left == null) { $current->left = $new_node; return; } else { $current = $current->left; } } else { if ($current->right == null) { $current->right = $new_node; return; } else { $current = $current->right; } } } } public function search($val) { $current = $this->root; while ($current != null) { if ($val == $current->value) { return $current; } elseif ($val < $current->value) { $current = $current->left; } else { $current = $current->right; } } return null; } public function delete($val) { $parent = null; $current = $this->root; $is_left_child = false; while ($current != null) { if ($val == $current->value) { if ($current->left == null && $current->right == null) { if ($current == $this->root) { $this->root = null; } elseif ($is_left_child) { $parent->left = null; } else { $parent->right = null; } } elseif ($current->left == null) { if ($current == $this->root) { $this->root = $current->right; } elseif ($is_left_child) { $parent->left = $current->right; } else { $parent->right = $current->right; } } elseif ($current->right == null) { if ($current == $this->root) { $this->root = $current->left; } elseif ($is_left_child) { $parent->left = $current->left; } else { $parent->right = $current->left; } } else { $successor = $this->get_successor($current); if ($current == $this->root) { $this->root = $successor; } elseif ($is_left_child) { $parent->left = $successor; } else { $parent->right = $successor; } $successor->left = $current->left; } return; } elseif ($val < $current->value) { $parent = $current; $current = $current->left; $is_left_child = true; } else { $parent = $current; $current = $current->right; $is_left_child = false; } } } public function get_successor($del_node) { $successor_parent = $del_node; $successor = $del_node; $current = $del_node->right; while ($current != null) { $successor_parent = $successor; $successor = $current; $current = $current->left; } if ($successor != $del_node->right) { $successor_parent->left = $successor->right; $successor->right = $del_node->right; } return $successor; } } // 示例用法 $tree = new BinaryTree(); $tree->insert(5); $tree->insert(3); $tree->insert(7); $tree->insert(1); $tree->insert(4); $tree->insert(6); $tree->insert(8); print_r($tree->search(5)); // 输出: TreeNode Object ( [value] => 5 [left] => TreeNode Object ( [value] => 3 [left] => TreeNode Object ( [value] => 1 [left] => [right] => ) [right] => TreeNode Object ( [value] => 4 [left] => [right] => ) ) [right] => TreeNode Object ( [value] => 7 [left] => TreeNode Object ( [value] => 6 [left] => [right] => ) [right] => TreeNode Object ( [value] => 8 [left] => [right] => ) ) ) $tree->delete(4); print_r($tree->search(4)); // 输出: null ``` 上面的代码实现了二叉树的基本功能,包括插入节点、查找节点和删除节点。在实际使用时,可根据需要进行修改和扩展。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值