Ask everyone a little algorithmic problem
Tree-like flowchart:
Table structure
The table structure is that each node has three child nodes, left, center, right, the requirement is to insert the node in the appropriate location, insert the rule is: Assume starting from id=1, and then find out the next three sub-nodes, if three child nodes exist, The child nodes of the three child nodes are found separately. Until the child node you are in has no data, you can insert the
concrete process: such as tree chart: id = 1 start to find, find its child nodes, there are 2, 3, 4, are present, and then in the search for the child nodes of the 2, 5,6,7 is also present, and then in the lookup of 3 child nodes, only 8, and then you can be in the middle of the 3 child node inserted. Of course, the more data, the more you will find. have been thinking, did not think out, anxious, I hope the great God to give a specific code algorithm, extremely grateful.
------to solve the idea----------------------
Previously found on the internet php two fork tree, I changed a little, become a three-pronged tree, actually added a center
Class Node {
public $data = null;
public $parent = null;
Public $left = null;
Public $center =null;
public $right = null;
}
function Build_cbtree ($a) {
$root = new Node ();
$root->data = $a [0];
for ($i = 1; $i < count ($a); $i + +) {
$node = new node ();
$node->data = $a [$i];
Insert_node ($root, $node);
}
return $root;
}
function Insert_node ($root, $inode) {
$queue = Array ();
Array_unshift ($queue, $root);
while (!empty ($queue)) {
$cnode = Array_pop ($queue);
if ($cnode->left = = null) {
$cnode->left = $inode;
$inode->parent = $cnode;
return $root;
} else {
Array_unshift ($queue, $cnode->left);
}
if ($cnode->center = = null) {
$cnode->center = $inode;
$inode->parent = $cnode;
return $root;
} else {
Array_unshift ($queue, $cnode->center);
}
if ($cnode->right = = null) {
$cnode->right = $inode;
$inode->parent = $cnode;
return $root;
} else {
Array_unshift ($queue, $cnode->right);
}
}
return $root;
}
Breadth-first traversal (traversing first layer, then traversing the next layer)
function Bf_traverse ($root) {
$queue = Array ();
Array_unshift ($queue, $root);
while (!empty ($queue)) {
$cnode = Array_pop ($queue);
Echo $cnode->data. " ";
if ($cnode->left!== null) array_unshift ($queue, $cnode->left);
if ($cnode->center!== null) array_unshift ($queue, $cnode->center);
if ($cnode->right!== null) array_unshift ($queue, $cnode->right);
}
}
$a = array (1,2,3,4,5,6,7,8);
First, turn the array into a three-pronged tree
$root = Build_cbtree ($a);
echo "";
Print_r ($root);
echo "
";
Bf_traverse ($root);
/* $root data too much will not be called out.
Traversal rules based on breadth first
1 2 3 4 5 6 7 8
*/
At this point $root is a three-pronged tree
Inserting 9 is actually the code that repeats the Build_cbtree method for the For loop
$node 1 = new Node ();
$node 1->data = 9;
Insert_node ($root, $node 1);
echo "";
Print_r ($root);
echo "
";
Bf_traverse ($root);
/*
Node Object
(
[Data] = 1
[Parent] =
[Left] = Node Object
(
[Data] = 2
[Parent] = Node Object
*recursion*
[Left] = Node Object
(
[Data] = 5
[Parent] = Node Object
*recursion*
[Left] = =
[Center] =
[Right] = =
)
[Center] = Node Object
(
[Data] = 6
[Parent] = Node Object
*recursion*
[Left] = =