日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > php >内容正文

php

php二叉树算法

發布時間:2024/9/5 php 30 豆豆
生活随笔 收集整理的這篇文章主要介紹了 php二叉树算法 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
二叉樹的每個結點至多只有二棵子樹(不存在度大于2的結點),二叉樹的子樹有左右之分,次序不能顛倒。二叉樹的第i層至多有2^{i-1}個結點;深度為k的二叉樹至多有2^k-1個結點;對任何一棵二叉樹T,如果其終端結點數為n_0,度為2的結點數為n_2,則n_0=n_2+1。 一棵深度為k,且有2^k-1個節點稱之為滿二叉樹;深度為k,有n個節點的二叉樹,當且僅當其每一個節點都與深度為k的滿二叉樹中,序號為1至n的節點對應時,稱之為完全二叉樹。 <?php /**? * 二叉樹的定義? */ class BinaryTree { protected $key = NULL;????? //? 當前節點的值 protected $left = NULL;???? //? 左子樹 protected $right = NULL;??? //? 右子樹 /**????? * 以指定的值構造二叉樹,并指定左右子樹????? * * @param mixed $key 節點的值. * @param mixed $left 左子樹節點. * @param mixed $right 右子樹節點. */ public function __construct( $key = NULL, $left = NULL, $right = NULL) {$this->key = $key;if ($key === NULL) {$this->left = NULL;$this->right = NULL;}elseif ($left === NULL) {$this->left = new BinaryTree();$this->right = new BinaryTree();}else {$this->left = $left;$this->right = $right;}}/*** 析構方法.*/public function __destruct() {$this->key = NULL;$this->left = NULL;$this->right = NULL;}/*** 清空二叉樹.**/public function purge () {$this->key = NULL;$this->left = NULL;$this->right = NULL;}/*** 測試當前節點是否是葉節點.** @return boolean 如果節點非空并且有兩個空的子樹時為真,否則為假.*/public function isLeaf() {return !$this->isEmpty() &&$this->left->isEmpty() &&$this->right->isEmpty();}/*** 測試節點是否為空** @return boolean 如果節點為空返回真,否則為假.*/public function isEmpty() {return $this->key === NULL;}/*** Key getter.** @return mixed 節點的值.*/public function getKey() {if ($this->isEmpty()) {return false;}return $this->key;}/*** 給節點指定Key值,節點必須為空** @param mixed $object 添加的Key值.*/public function attachKey($obj) {if (!$this->isEmpty())return false;$this->key = $obj;$this->left = new BinaryTree();$this->right = new BinaryTree();}/*** 刪除key值,使得節點為空.*/public function detachKey() {if (!$this->isLeaf())return false;$result = $this->key;$this->key = NULL;$this->left = NULL;$this->right = NULL;return $result;}/*** 返回左子樹** @return object BinaryTree 當前節點的左子樹.*/public function getLeft() {if ($this->isEmpty())return false;return $this->left;}/*** 給當前結點添加左子樹** @param object BinaryTree $t 給當前節點添加的子樹.*/public function attachLeft(BinaryTree $t) {if ($this->isEmpty() || !$this->left->isEmpty())return false;$this->left = $t;}/*** 刪除左子樹** @return object BinaryTree? 返回刪除的左子樹.*/public function detachLeft() {if ($this->isEmpty())return false;$result = $this->left;$this->left = new BinaryTree();return $result;}/*** 返回當前節點的右子樹** @return object BinaryTree 當前節點的右子樹.*/public function getRight() {if ($this->isEmpty())return false;return $this->right;}/*** 給當前節點添加右子樹** @param object BinaryTree $t 需要添加的右子樹.*/public function attachRight(BinaryTree $t) {if ($this->isEmpty() || !$this->right->isEmpty())return false;$this->right = $t;}/*** 刪除右子樹,并返回此右子樹* @return object BinaryTree 刪除的右子樹.*/public function detachRight() {if ($this->isEmpty ())return false;$result = $this->right;$this->right = new BinaryTree();return $result;}/*** 先序遍歷*/public function preorderTraversal() {if ($this->isEmpty()) {return ;}echo ' ', $this->getKey();$this->getLeft()->preorderTraversal();$this->getRight()->preorderTraversal();}/*** 中序遍歷*/public function inorderTraversal() {if ($this->isEmpty()) {return ;}$this->getLeft()->preorderTraversal();echo ' ', $this->getKey();$this->getRight()->preorderTraversal();}/*** 后序遍歷*/public function postorderTraversal() {if ($this->isEmpty()) {return ;}$this->getLeft()->preorderTraversal();$this->getRight()->preorderTraversal();echo ' ', $this->getKey();} }/*** 二叉排序樹的PHP實現*/class BST extends BinaryTree {/*** 構造空的二叉排序樹*/public function __construct() {parent::__construct(NULL, NULL, NULL);}/*** 析構*/public function __destruct() {parent::__destruct();}/*** 測試二叉排序樹中是否包含參數所指定的值** @param mixed $obj 查找的值.* @return boolean True 如果存在于二叉排序樹中則返回真,否則為假期*/public function contains($obj) {if ($this->isEmpty())return false;$diff = $this->compare($obj);if ($diff == 0) {return true;}elseif ($diff < 0)???????????? return $this->getLeft()->contains($obj);elsereturn $this->getRight()->contains($obj);}/*** 查找二叉排序樹中參數所指定的值的位置** @param mixed $obj 查找的值.* @return boolean True 如果存在則返回包含此值的對象,否則為NULL*/public function find($obj) {if ($this->isEmpty())return NULL;$diff = $this->compare($obj);if ($diff == 0)return $this->getKey();elseif ($diff < 0)???????????? return $this->getLeft()->find($obj);elsereturn $this->getRight()->find($obj);}/*** 返回二叉排序樹中的最小值* @return mixed 如果存在則返回最小值,否則返回NULL*/public function findMin() {if ($this->isEmpty ())return NULL;elseif ($this->getLeft()->isEmpty())return $this->getKey();elsereturn $this->getLeft()->findMin();}/*** 返回二叉排序樹中的最大值* @return mixed 如果存在則返回最大值,否則返回NULL*/public function findMax() {if ($this->isEmpty ())return NULL;elseif ($this->getRight()->isEmpty())return $this->getKey();elsereturn $this->getRight()->findMax();}/*** 給二叉排序樹插入指定值** @param mixed $obj 需要插入的值.* 如果指定的值在樹中存在,則返回錯誤*/public function insert($obj) {if ($this->isEmpty()) {$this->attachKey($obj);} else {$diff = $this->compare($obj);if ($diff == 0)die('argu error');if ($diff < 0)???????????????? $this->getLeft()->insert($obj);else$this->getRight()->insert($obj);}$this->balance();}/*** 從二叉排序樹中刪除指定的值** @param mixed $obj 需要刪除的值.*/public function delete($obj) {if ($this->isEmpty ())die();$diff = $this->compare($obj);if ($diff == 0) {if (!$this->getLeft()->isEmpty()) {$max = $this->getLeft()->findMax();$this->key = $max;$this->getLeft()->delete($max);}elseif (!$this->getRight()->isEmpty()) {$min = $this->getRight()->findMin();$this->key = $min;$this->getRight()->delete($min);} else$this->detachKey();} else if ($diff < 0)???????????????? $this->getLeft()->delete($obj);else$this->getRight()->delete($obj);$this->balance();}public function compare($obj) {return $obj - $this->getKey();}/*** Attaches the specified object as the key of this node.* The node must be initially empty.** @param object IObject $obj The key to attach.* @exception IllegalOperationException If this node is not empty.*/public function attachKey($obj) {if (!$this->isEmpty())return false;$this->key = $obj;$this->left = new BST();$this->right = new BST();}/*** Balances this node.* Does nothing in this class.*/protected function balance () {}/*** Main program.** @param array $args Command-line arguments.* @return integer Zero on success; non-zero on failure.*/public static function main($args) {printf("BinarySearchTree main program.\n");$root = new BST();foreach ($args as $row) {$root->insert($row);}return $root;} }$root = BST::main(array(50, 3, 10, 5, 100, 56, 78)); echo $root->findMax(); $root->delete(100); echo $root->findMax();

  

轉載于:https://www.cnblogs.com/icyy/p/4672724.html

總結

以上是生活随笔為你收集整理的php二叉树算法的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。