A tree-like flowchart showing small algorithm problems: the table structure is that each node has three subnodes, left, center, and right. the requirement is to insert nodes at proper positions, the insert rule is as follows: assume that it starts from id1 and finds the three subnodes under it. if all three subnodes exist, find the subnodes of the three subnodes respectively. You can insert & nbsp; to solve a small algorithm problem.
Tree flowchart:
Table structure
This table structure means that each node has three sub-nodes, left, center, and right. the requirement is to insert the node in the appropriate position. The insertion rule is: assume that the table starts with id = 1, find the three subnodes under it. if all three subnodes exist, find the subnodes of the three subnodes respectively. The data can be inserted until the child node has no data.
Specific process steps: for example, tree chart: id = 1 start to search for its subnodes, there are 2, 3, 4, all exist, and then find the subnodes of 2, 5, 6, 7 also exists, and then you are looking for the child nodes of 3, only 8, and then you can insert the child nodes in the middle of 3. Of course, the more data you need, the more you will find. I have been thinking about it. I am very anxious. I hope you can give me an algorithm for specific code. thank you very much.
------ Solution ----------------------
I used to find the php binary tree on the internet. I changed it a bit and changed it into a three-tree. In fact, I added a center.
Class Node {
Public $ data = null;
Public $ parent = null;
Public $ left = null;
Public $ center = null;
Public $ right = null;
}
Function build_cbtree ($ ){
$ 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;
}
// Traverse the breadth first (traverse the first layer and then review 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, convert the array into a trigger tree.
$ Root = build_cbtree ($ );
Echo"";
print_r($root);
echo "
";
Bf_traverse ($ root );
/* $ Do not output too much root data
Based on the breadth-first traversal rule
1 2 3 4 5 6 7 8
*/
// At this time, $ root is a triple tree.
// Insert 9, which is actually the code of the for loop in the repeated build_cbtree method
$ Node1 = new Node ();
$ Node1-> data = 9;
Insert_node ($ root, $ node1 );
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] =>