PHPClassObject-in-depth analysis of PHP self-Ordered Binary Tree

Source: Internet
Author: User
This article provides a detailed analysis of the self-Ordered Binary Tree in PHP. For more information, see

This article provides a detailed analysis of the self-Ordered Binary Tree in PHP. For more information, see

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. When inserting new elements, for US space, 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, the server space only needs to traverse the binary tree in order.

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 ()),'

';
?>


, Website Space

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.