This article provides a detailed analysis of the binary tree implementation code in PHP. For more information, see
This article provides a detailed analysis of the binary tree implementation code in PHP. For more information, see
Binary Trees and their variants are an important part of the data structure family. A variant of the most linked list, Hong Kong Space, website space, virtual host, and binary tree are the most suitable for processing data that requires a specific sequence 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 ()),'
';
?>