1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 | <?php class BinTree{ public $data; public $left; public $right; } //前序遍历生成二叉树 function createBinTree(){ $handle=fopen("php://stdin","r"); $e=trim(fgets($handle)); if($e=="#"){ $binTree=null; }else{ $binTree=new BinTree(); $binTree->data=$e; $binTree->left=createBinTree(); $binTree->right=createBinTree(); } return $binTree; } $tree=createBinTree(); var_dump($tree); # object(BinTree)#1 (3) { ["data"]=> string(1) "A" ["left"]=> object(BinTree)#2 (3) { ["data"]=> string(1) "B" ["left"]=> NULL ["right"]=> object(BinTree)#3 (3) { ["data"]=> string(1) "D" ["left"]=> NULL ["right"]=> NULL } } ["right"]=> object(BinTree)#4 (3) { ["data"]=> string(1) "C" ["left"]=> NULL ["right"]=> NULL } } |
欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/) | 黑马程序员IT技术论坛 X3.2 |