最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

php實現(xiàn)映射操作實例詳解

 更新時間:2019年10月02日 12:13:29   作者:walkingSun  
這篇文章主要介紹了php實現(xiàn)映射操作,結(jié)合實例形式詳細分析了PHP映射概念、原理及使用鏈表與二叉樹實現(xiàn)映射的相關(guān)操作技巧,需要的朋友可以參考下

本文實例講述了php實現(xiàn)映射操作。分享給大家供大家參考,具體如下:

映射

映射,或者射影,在數(shù)學及相關(guān)的領(lǐng)域經(jīng)常等同于函數(shù)?;诖?,部分映射就相當于部分函數(shù),而完全映射相當于完全函數(shù)。

映射(Map)是用于存取鍵值對的數(shù)據(jù)結(jié)構(gòu)(key,value),一個鍵只能對應(yīng)一個值且鍵不能重復(fù)。

實現(xiàn)

映射的實現(xiàn)方式可以使用鏈表或二叉樹實現(xiàn)。

鏈表實現(xiàn):

<?php
/**
 * 接口 字典
 * Interface Dict
 * @package app\models
 */
Interface Dict
{
  public function set( $key , $value );
  public function get( $key );
  public function isExist( $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 set($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 self($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 isExist($key)
  {
    $node = $this;
    while($node){
      if( $node->key ==$key ){
        return true;
      }
      $node = $node->next;
    }
    return false;
  }
  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->set('sun',111); //O(n)
    $dict->set('sun',222);
    $dict->set('w',111);
    $dict->set('k',111);
    var_dump($dict->get('w'));  //O(n)
    var_dump($dict->isExist('v'));  //O(n)
    var_dump($dict->delete('sun'));  //O(n)
    var_dump($dict->getSize());
/******************************************/
//111
//false
//true
//2

二叉樹實現(xiàn)

<?php
class 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 set( $key , $value ){
    if( $this->size ==0 ){
      $node = new static( $key,$value );
      $this->key = $node->key;
      $this->value = $node->value;
      $this->size++;
    }else{
      $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;
        }else{
          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;
      } else {
        $node = $node->right;
      }
    }
    throw new \Exception('this key not exist');
  }
  public function isExist( $key ){
    if( $this->size ==0 )
      return false;
    $node = $this;
    while($node) {
      if ($node->key == $key) {
        return true;
      }
      if ($node->key > $key) {
        $node = $node->left;
      } else {
        $node = $node->right;
      }
    }
    return false;
  }
  public function delete($key){
    //找到元素,尋找元素左邊最小元素
    $node = $this->select($key);
    if( $node->right!=null ){
      $node1 = $node->selectMin($node->right);
      //替換當前node
      $node->key = $node1->key;
      $node->value = $node1->value;
      //刪除$node->right最小元素,獲取最終元素賦給$node->right
      $nodeMin = $this->deleteMin($node->right);
      $node->right = $nodeMin;
    }else{
      $node1 = $node->selectMax($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 select($key){
    $node = $this;
    while($node){
      if($node->key==$key){
        return $node;
      }
      if ($node->key > $key) {
        $node = $node->left;
      } else {
        $node = $node->right;
      }
    }
    throw new \Exception('this key not exist');
  }
  public function selectMin( $node ){
    while($node->left){
      $node = $node->left;
    }
    return $node;
  }
  public function selectMax( $node ){
    while($node->right){
      $node = $node->right;
    }
    return $node;
  }
}

復(fù)雜度分析

鏈表 O(n)

二分搜索樹 O(log n)

更多關(guān)于PHP相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《PHP數(shù)據(jù)結(jié)構(gòu)與算法教程》、《php程序設(shè)計算法總結(jié)》、《php字符串(string)用法總結(jié)》、《PHP數(shù)組(Array)操作技巧大全》、《PHP常用遍歷算法與技巧總結(jié)》及《PHP數(shù)學運算技巧總結(jié)

希望本文所述對大家PHP程序設(shè)計有所幫助。

相關(guān)文章

  • php自動適應(yīng)范圍的分頁代碼

    php自動適應(yīng)范圍的分頁代碼

    分享一個自己寫的“頁碼自動適應(yīng)范圍”的分頁代碼
    2008-08-08
  • PHP實現(xiàn)將漢字轉(zhuǎn)換為拼音及獲取詞語首字母的方法

    PHP實現(xiàn)將漢字轉(zhuǎn)換為拼音及獲取詞語首字母的方法

    這篇文章主要介紹了PHP實現(xiàn)將漢字轉(zhuǎn)換為拼音及獲取詞語首字母的方法,涉及php字符串、數(shù)組的遍歷及編碼轉(zhuǎn)換相關(guān)操作技巧,需要的朋友可以參考下
    2017-08-08
  • thinkphp5 migrate數(shù)據(jù)庫遷移工具

    thinkphp5 migrate數(shù)據(jù)庫遷移工具

    這里講述的是tp5 migrate數(shù)據(jù)庫遷移工具的相關(guān)介紹,非常的簡單實用,有需要的小伙伴可以來看下本文的實例
    2018-02-02
  • PHP中ADODB類詳解

    PHP中ADODB類詳解

    1. 前言 ADODB 是 Active Data Objects Data Base 的簡稱,它是一種 PHP 存取數(shù)據(jù)庫的函式組件?,F(xiàn)在 SFS3 系統(tǒng) (校園自由軟件交流網(wǎng)學務(wù)系統(tǒng)) 計劃的主持人陳瑩光老師,決定采用此一組件,為了讓更多有心參與該項目的伙伴們能夠順利加入發(fā)展的行列,小弟認為有必要把 ADODB 的中文入門介紹寫出來,以方便伙伴們參考備查。
    2008-03-03
  • 關(guān)于js與php互相傳值的介紹

    關(guān)于js與php互相傳值的介紹

    本篇文章是對js與php互相傳值的問題進行了詳細的分析介紹,需要的朋友參考下
    2013-06-06
  • PHP面相對象中的重載與重寫

    PHP面相對象中的重載與重寫

    本文主要介紹了PHP面相對象中的重載與重寫。具有很好的參考價值,下面跟著小編一起來看下吧
    2017-02-02
  • php計算年齡精準到年月日

    php計算年齡精準到年月日

    這篇文章主要介紹了php計算年齡精準到年月日的方法,涉及php操作日期與字符串的相關(guān)技巧,非常簡單實用,需要的朋友可以參考下
    2015-11-11
  • php 地區(qū)分類排序算法

    php 地區(qū)分類排序算法

    本篇文章是對使用php實現(xiàn)地區(qū)分類排序算法進行了詳細的分析介紹,需要的朋友參考下
    2013-07-07
  • PHP轉(zhuǎn)換文件夾下所有文件編碼的實現(xiàn)代碼

    PHP轉(zhuǎn)換文件夾下所有文件編碼的實現(xiàn)代碼

    本篇文章是對PHP轉(zhuǎn)換文件夾下所有文件編碼的實現(xiàn)代碼進行了詳細的分析介紹,需要的朋友參考下
    2013-06-06
  • 防止本地用戶用fsockopen DDOS攻擊對策

    防止本地用戶用fsockopen DDOS攻擊對策

    php腳本中的 fsockopen 函數(shù),對外部地址,通過UDP發(fā)送大量的數(shù)據(jù)包,攻擊對方
    2011-11-11

最新評論

双鸭山市| 霍城县| 阿勒泰市| 阿城市| 云霄县| 滕州市| 岗巴县| 长武县| 合水县| 九龙县| 慈溪市| 永靖县| 新营市| 米脂县| 石棉县| 高清| 平舆县| 互助| 仁布县| 竹山县| 屯门区| 正蓝旗| 无锡市| 上饶县| 庆云县| 苏尼特左旗| 筠连县| 蛟河市| 莲花县| 新沂市| 五台县| 海原县| 乌什县| 崇州市| 巴南区| 宜君县| 年辖:市辖区| 郑州市| 瓮安县| 江津市| 望谟县|