在节点之间再选用有的排序逻辑,二叉树就能够提供优异的团体办法。对于每一个节点,都让满足全部特定条件的成分都坐落于左节点及其子节点。在插入新因素时,大家须要从树的首先个节
点开始,判别它归于哪意气风发侧的节点,然后沿着那旁边找到相符的地点,相近地,在读取数据时,只要求使用按序遍历方法来遍历二叉树。复制代码 代码如下:tree = new
Binary_Tree_Node; } 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 { // Traverse deeper: $pointer = $pointer->left; } else {
// Found the new spot: insert the new element here: $pointer->left =
new Binary_Tree_Node; break; } } else { // We are looking to the right
… If the child exists: if { // Traverse deeper: $pointer =
$pointer->right; } else { // Found the new spot: insert the new
element here: $pointer->right = new Binary_Tree_Node; 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;}// 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, 90echo ‘
二叉树及其变体是数据构造亲族里的首要性组成都部队分。最为链表的生龙活虎种变体,二叉树最切合管理要求生龙活虎一定程序连忙组织和找出的数码。
复制代码 代码如下:
// Define a class to implement a binary tree
class Binary_Tree_Node {
// Define the variable to hold our data:
public $data;
// And a variable to hold the left and right objects:
public $left;
public $right;
‘, implode(‘, ‘, $sort_as_you_go->returnSorted, ‘
// A constructor method that allows for data to be passed in
public function __construct($d = NULL) {
$this->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();
$r = array();
// Read in the left and right children appropriately
traversed:
if ($this->left) { $l =
$this->left->traversePreorder(); }
if ($this->right) { $r =
$this->right->traversePreorder(); }
// Return a merged array of the current value, left, and
right:
return array_merge(array($this->data), $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();
$r = array();
// Read in the left and right children appropriately
traversed:
if ($this->left) { $l =
$this->left->traversePostorder(); }
if ($this->right) { $r =
$this->right->traversePostorder(); }
// Return a merged array of the current value, left, and
right:
return array_merge($l, $r, array($this->data));
}
// 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();
$r = array();
// Read in the left and right children appropriately
traversed:
if ($this->left) { $l = $this->left->traverseInorder();
}
if ($this->right) { $r =
$this->right->traverseInorder(); }
// Return a merged array of the current value, left, and
right:
return array_merge($l, array($this->data), $r);
二叉树及其变体是数据布局亲族里的重大组成都部队分。 }
}
// Let’s create a binary tree that will equal the following: 3
// /
/
// h
9
// /
/
// Create the tree: 6
a
$tree = new Binary_Tree_Node(3);
$tree->left = new Binary_Tree_Node(‘h’);
$tree->right = new Binary_Tree_Node(9);
$tree->right->left = new Binary_Tree_Node(6);
$tree->right->right = new Binary_Tree_Node(‘a’);
// Now traverse this tree in all possible orders and display the
results:
// Pre-order: 3, h, 9, 6, a
echo ‘
‘, implode(‘, ‘, $tree->traversePreorder()), ‘
‘;
// Post-order: h, 9, 6, a, 3
echo ‘
‘, implode(‘, ‘, $tree->traversePostorder()), ‘
‘;
// In-order: h, 3, 6, 9, a
echo ‘
‘, implode(‘, ‘, $tree->traverseInorder()), ‘
‘;
?>
www.2979.com, 之家, 二叉树, 代码, 变体, 技巧, 数据结构, 脚本