PHPClass & amp; Object -- in-depth analysis of PHP self-ordered binary tree _ PHP Tutorial

Source: Internet
Author: User
PHPClass & amp; Object -- in-depth analysis of PHP self-ordered binary tree. By applying sorting logic between nodes, a binary tree can provide an excellent way of organizing. For each node, the elements that meet all specific conditions are placed on the left node and its subnodes, and some sorting logic is applied between nodes, so that the binary tree can provide an excellent way of organizing. For each node, the elements that meet all specific conditions are placed on the left node and its subnodes. When inserting a new element, we need to start from the first node (root node) of the tree to determine which node it belongs to, and then find the proper position along this side, similarly, when reading data, you only need to use the sequential traversal method to traverse the binary tree.

The code is as follows:


Ob_start ();
// Here we need to include the binary tree class
Class Binary_Tree_Node (){
// You can find the details
}
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-& gt; 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 ()),'

';
?>


Bytes. For each node, the elements that meet all specific conditions are placed on the left node and its subnodes...

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.