data = $data; } } //先序遍历(根,左,右)递归实现 public function PreTraverseBTree($BTree){ if (NULL !== $BTree) { var_dump($BTree->data); //根 if (NULL !== $BTree->LeftHand) { $this->PreTraverseBTree($BTree->LeftHand); //递归遍历左树 } if (NULL !== $BTree->RightHand) { $this->PreTraverseBTree($BTree->RightHand); //递归遍历右树 } } } //中序遍历(左,根,右)递归实现 public function InTraverseBTree($BTree){ if (NULL !== $BTree) { if (NULL !== $BTree->LeftHand) { $this->InTraverseBTree($BTree->LeftHand); //递归遍历左树 } var_dump($BTree->data); //根 if (NULL !== $BTree->RightHand) { $this->InTraverseBTree($BTree->RightHand); //递归遍历右树 } } } //后序遍历(左,右,根)递归实现 public function FexTarverseBTree($BTree){ if (NULL !== $BTree) { if (NULL !== $BTree->LeftHand) { $this->FexTarverseBTree($BTree->LeftHand); //递归遍历左树 } if (NULL !== $BTree->RightHand) { $this->FexTarverseBTree($BTree->RightHand); //递归遍历右树 } var_dump($BTree->data); //根 } } } header("Content-Type:text/html;charset=utf-8"); echo '先的内存为'.var_dump(memory_get_usage()); echo '
'; //创建五个节点 $A = new BTreeNode('A'); $B = new BTreeNode('B'); $C = new BTreeNode('C'); $D = new BTreeNode('D'); $E = new BTreeNode('E'); //连接形成一个二叉树 $A->LeftHand = $B; $A->RightHand = $C; $C->LeftHand = $D; $D->RightHand = $E; //先序遍历 echo '先序遍历的结果'.''; $A->PreTraverseBTree($A); echo '中序遍历的结果'.''; $A->InTraverseBTree($A); echo '后序列遍历的结果'.''; $A->FexTarverseBTree($A); echo '
'; echo '后的内存为'.var_dump(memory_get_usage());