‘, implode(‘, ‘, $tree->traversePreorder, ‘
‘;
?>
‘;// Post-order: h, 9, 6, a, 3echo ‘
在节点之间再应用一些排序逻辑,二叉树就能提供出色的组织方式。对于每个节点,都让满足所有特定条件的元素都位于左节点及其子节点。在插入新元素时,我们需要从树的第一个节
点(根节点)开始,判断它属于哪一侧的节点,然后沿着这一侧找到恰当的位置,类似地,在读取数据时,只需要使用按序遍历方法来遍历二叉树。
复制代码 代码如下:
ob_start();
// Here we need to include the binary tree class
Class Binary_Tree_Node() {
// You can find the details at
}
ob_金沙国际唯一官网网址 ,end_www.2979.com ,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_【www.2979.com】二叉树及其变体是数据结构亲族里的严重性组成都部队分,推断它归属哪生机勃勃侧的节点。Node($val);
break;
}
}
}
}
}
‘, implode(‘, ‘, $tree->traverseInorder, ‘
‘, implode(‘, ‘, $sort_as_you_go->returnSorted()), ‘
‘;?>
// 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(‘, ‘, $tree->traversePostorder, ‘
二叉树及其变体是数据结构家族里的重要组成部分。最为链表的一种变体,二叉树最适合处理需要一特定次序快速组织和检索的数据。复制代码 代码如下:data = $d; } // Traverse the
tree, left to right, in pre-order, returning an array // Preorder means
that each node’s value preceeds its children. public function
traversePreorder() { // Prep some variables. $l = array; // Read in the
left and right children appropriately traversed: if { $l =
$this->left->traversePreorder(); } if { $r =
$this->right->traversePreorder(); } // Return a merged array of
the current value, left, and right: return array_merge, $l, $r); } //
Traverse the tree, left to right, in postorder, returning an array //
Postorder means that each node’s value follows its children. public
function traversePostorder() { // Prep some variables. $l = array; //
Read in the left and right children appropriately traversed: if { $l =
$this->left->traversePostorder(); } if { $r =
$this->right->traversePostorder(); } // Return a merged array of
the current value, left, and right: return array_merge($l, $r, array; }
// Traverse the tree, left to right, in-order, returning an array. //
In-order means that values are ordered as left children, then the //
node value, then the right children. public function traverseInorder() {
// Prep some variables. $l = array; // Read in the left and right
children appropriately traversed: if { $l =
$this->left->traverseInorder(); } if { $r =
$this->right->traverseInorder(); } // Return a merged array of the
current value, left, and right: return array_merge($l, array; }}//
Let’s create a binary tree that will equal the following: 3// / / // h 9
// / / // Create the tree: 6 a $tree = new
Binary_Tree_Node;$tree->left = new
Binary_Tree_Node;$tree->right = new
Binary_Tree_Node;$tree->right->left = new
Binary_Tree_Node;$tree->right->right = new Binary_Tree_Node;//
Now traverse this tree in all possible orders and display the results://
Pre-order: 3, h, 9, 6, aecho ‘
‘;// In-order: h, 3, 6, 9, aecho ‘
www.2979.com, 之家, 二叉树, 代码, 技巧, 脚本, 节点, 遍历