How does php store binary trees?

Source: Internet
Author: User
How does php store binary trees? As shown in the image above, how can I store that string of numbers in a binary tree? Use php. Please help me !!!! ========== Beginner binary tree, this is very simple class & nbsp; node {& nbsp; public & nbsp; var & nbsp; $ per; how does php store binary trees?

As shown in the image above, how can I store that string of numbers in a binary tree? Use php.
Please help me !!!!
=========== Beginner binary tree
------ Solution --------------------
This is simple
class node{
   public var $per;
   public var $lNode;
   public var $rNode;

   function test1(){
   }

   function test2(){
   }
}

Since php is a weak type language, you only need to know that $ lNode and $ rNode are of the node type. when assigning values, you must assign the left and right nodes of the node type to the corresponding ones.
$node0=new node();
$node0->per = 49;
$node1=new node();
$node1->per = 20;
$node2=new node();
$node2->per = 29;
$node0->lNode = $node1;
$node0->rNode = $node2;

------ Solution --------------------
Write in this way
 
  
<Br/> <? Php <br/> $ s & nbsp ;=& nbsp; '4 & nbsp; 6 & nbsp; 8 & nbsp; 9 & nbsp; 10 & nbsp; 12 '; <br/> foreach (explode ('& nbsp;', & nbsp; $ s) & nbsp; as & nbsp; $ k => $ v) & nbsp; {& nbsp; // Initialization <br/> & nbsp; $ t [] & nbsp ;=& nbsp; array ('W' & nbsp; ==>& nbsp; $ v, & nbsp; 'V' & nbsp ;=>& nbsp; $ v); & nbsp; // use the value as the weight <br/>}< br/> HuffmanTree ($ t); <br/> echo & nbsp; DLR ($ t), & nbsp; PHP_EOL; <br/>/* & nbsp; Harman algorithm & nbsp; */<br/> function & nbsp; HuffmanTree (& $ F) & nbsp; {<br/> & nbsp; while (count ($ F) & nbsp;> & nbsp; 1) & nbsp ;{< br/> & nbsp; & nbsp; $ r & nbsp; = & nbsp; array (); <br/> & nbsp; foreach ($ F & nbsp; as & nbsp; $ item) & nbsp; $ r [] & nbsp ;=& nbsp; $ item ['w']; <br/> & nbsp; array_multisort ($ r, & nbsp; $ F); <br/> & nbsp; & nbsp; $ t & nbsp ;=& nbsp; array ('W' & nbsp ;=>& nbsp; $ F [0] ['w'] + $ F [1] ['w'], & nbsp; 'L' & nbsp ;=>& nbsp; $ F [0], & nbsp; 'R' & nbsp ;=>& nbsp; $ F [1]); <br/> & nbsp; & nbsp; unset ($ F [0]); <br/> & nbsp; unset ($ F [1]); <br/> & nbsp; $ F [] & nbsp; = & nbsp; $ t; <br/> & nbsp; & nbsp ;}< br/> & nbsp; $ F & nbsp ;=& nbsp; current ($ F ); <br/>}< br/>/* & nbsp; forward traversal & nbsp; */<br/> function & nbsp; DLR ($ F, & nbsp; $ deep = 0) & nbsp ;{< br/> & nbsp; echo & nbsp; str_repeat ("& nbsp;", & nbsp; $ deep) & nbsp ;. '+-& nbsp;' & nbsp ;. & nbsp; $ F ['w'] & nbsp ;. & nbsp; PHP_EOL; // Print weight <br/> & nbsp; if (isset ($ F ['L']) & nbsp; DLR ($ F ['L'], & nbsp; $ deep + 1); <br/> & nbsp; if (isset ($ F ['R']) & nbsp; DLR ($ F ['R'], & nbsp; $ deep + 1 ); <br/>}< br/> </pre> <br/> +-& nbsp; 49 <br/> & nbsp; +-& nbsp; 20 <br/> & nbsp; +-& nbsp; 10 <br/> & nbsp; +-& nbsp; 10 <br/> & nbsp; & nbsp; +-& nbsp; 4 <br/> & nbsp; & nbsp; +-& nbsp; 6 <br/> & nbsp; +-& nbsp; 29 <br/> & nbsp; +-& nbsp; 12 <br/> & nbsp; +-& nbsp; 17 <br/> & nbsp; & nbsp; +-& nbsp; 8 <br/> & nbsp; & nbsp; +-& nbsp; 9 <br/>
 

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.