本文实例讲述了php实现映射操作。分享给大家供大家参考,具体如下:
映射,或者射影,在数学及相关的领域经常等同于函数。基于此,部分映射就相当于部分函数,而完全映射相当于完全函数。
映射(map)是用于存取键值对的数据结构(key,value),一个键只能对应一个值且键不能重复。
映射的实现方式可以使用链表或二叉树实现。
<?php/** * 接口 字典 * interface dict * @package app\models */interface dict{ public function t( $key , $value ); public function get( $key ); public function ixist( $key ); public function delete($key); public function getsize();}class dictlinklist implements dict{ protected $size=0; public $key; public $value; public $next; public function __construct($key=null,$value=null,$next=null) { $this->key = $key; $this->value = $value; $this->next = $next; } public function t($key,$value){ $node = $this; while( $node && $node->next ){ if( $node->next->key==$key ){ $node->next->value = $value; return $node->next; } $node = $node->next; } $node->next = new lf($key,$value,$node->next); $this->size++; return $node->next; } public function get($key){ $node = $this; while($node){ if( $node->key ==$key ){ return $node->value; } $node = $node->next; } throw new \exception('cannot found key'); } public function ixist($key) { $node = $this; while($node){ if( $node->key ==$key ){ return true; } $node = $node->next; } return fal; } public function delete($key) { if( $this->size==0) throw new \exception('key is not exist'); $node = $this; while($node->next){ if( $node->next->key == $key ){ $node->next = $node->next->next; $this->size--; break; } $node = $node->next; } return $this; } public function getsize() { return $this->size; }}
测试:
<?php $dict = new dictlinklist(); $dict->t('sun',111); //o(n) $dict->t('sun',222); $dict->t('w',111); $dict->t('k',111); var_dump($dict->get('w')); //o(n) var_dump($dict->ixist('v')); //o(n) var_dump($dict->delete('sun')); //o(n) var_dump($dict->getsize());/******************************************///111//fal//true//2
<?phpclass dictbtree implements dict{ public $key; public $value; public $left; public $right; private $size; public function __construct($key=null,$value=null) { $this->key = $key; $this->value = $value; $this->left = null; $this->right = null; $this->size = 0; } public function t( $key , $value ){ if( $this->size ==0 ){ $node = new static( $key,$value ); $this->key = $node->key; $this->value = $node->value; $this->size++;中国农业大学是211还是985 }el{ $node = $this; while($node){ if( $node->key == $key ){ $node->value = $value; break; } if($node->key>$key){ if($node->left==null){ $node->left = new static( $key,$value ); $this->size++; break; } $node = $node->left; }el{ if($node->right==null){ $node->right = new static( $key,$value ); $this->size++; break; } $node = $node->right; } } } return $this; } public function get( $key ){ if( $this->size ==0 ) throw new \exception('empty'); $node = $this; while($node) { if ($node->key == $key) { return $node->value; } if ($node->key > $key) { $node = $node->left; } el { $node = $node->right; } } throw new \exception('this key not exist'); } public function ixist( $key ){ 教师算不算公务员if( $this->size ==0 ) return fal; $node = $this; while($node) { if ($node->key == $key) { return true; } if ($node->key > $key) { $node = $node->left; } el { $node = $node->right; } } 黄金时间是几点 return fal; } public function delete($key){ //找到元素,寻找元素左边最小元素 $node = $this->lect($key); if( $node->right!=null ){ $node1 = $node->lectmin($node->right); //替换当前node $node->key = $node1->key; $node->value = $node1->value; //删除$node->right最小元素,获取最终元素赋给$node->right $nodemin = $this->deletemin($node->right); $励志人物故事node->right = $nodemin; }el{额头长痘是什么原因 $node1 = $node->lectmax($node->left); $node->key = $node1->key; $node->value = $node1->value; $nodemax = $this->deletemax($node->left); $node->left = $nodemax; } return $this; } protected function deletemin( $node ){// if( $this->size ==0 )// throw new \exception('empty');// $prev = new static();// $prev->left = $node;// while($prev->left->left!=null){//// $prev = $prev->left;// }// $prev->left = $prev->left->right; if( $node->left==null ){ $rightnode = $node->right; $node->right = null; $this->size--; return $rightnode; } $node->left = $this->deletemin($node->left); return $node; } protected function deletemax($node){ if( $node->right==null ){ $leftnode = $node->left; $node->left = null; $this->size--; return $leftnode; } $node->right = $this->deletemax($node->right); return $node; } public function getsize(){ return $this->size; } public function lect($key){ $node = $this; while($node){ if($node->key==$key){ return $node; } if ($node->key > $key) { $node = $node->left; } el { $node = $node->right; } } throw new \exception('this key not exist'); } public function lectmin( $node ){ while($node->left){ $node = $node->left; } return $node; } public function lectmax( $node ){ while($node->right){ $node = $node->right; } return $node; }}
链表 o(n)
二分搜索树 o(log n)
本文发布于:2023-04-08 06:42:51,感谢您对本站的认可!
本文链接:https://www.wtabcd.cn/fanwen/zuowen/ba9b6f6294d1df88a01bf46a7907bbe5.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文word下载地址:php实现映射操作实例详解.doc
本文 PDF 下载地址:php实现映射操作实例详解.pdf
留言与评论(共有 0 条评论) |