PHPClass & amp; Object -- parsing PHP binary tree _ PHP Tutorial

Source: Internet
Author: User
PHPClass & amp; Object -- parse PHP to implement binary tree. Binary trees and their variants are an important part of the data structure family. One of the most linked list variants, the binary tree is most suitable for processing data that requires a specific order of fast organization and retrieval. Copying a binary tree and its variants are an important part of the data structure family. One of the most linked list variants, the binary tree is most suitable for processing data that requires a specific order of fast organization and retrieval.

The code is as follows:


// 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;

// 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
// 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
$ 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 = new Binary_Tree_Node ('A ');
// Now traverse this tree in all possible orders and display the results:
// Pre-order: 3, h, 9, 6,
Echo'

', Implode (', ', $ tree-> traversePreorder ()),'

';
// Post-order: h, 9, 6, a, 3
Echo'

', Implode (', ', $ tree-> traversePostorder ()),'

';
// In-order: h, 3, 6, 9,
Echo'

', Implode (', ', $ tree-> traverseInorder ()),'

';
?>


Bytes. One of the most linked list variants, the binary tree is most suitable for processing data that requires a specific order of fast organization and retrieval. Copy...

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.