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

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

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

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

映射

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

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

實(shí)現(xiàn)

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

鏈表實(shí)現(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;
  }
}

測(cè)試:

<?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

二叉樹實(shí)現(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);
      //替換當(dāng)前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è)計(jì)算法總結(jié)》、《php字符串(string)用法總結(jié)》、《PHP數(shù)組(Array)操作技巧大全》、《PHP常用遍歷算法與技巧總結(jié)》及《PHP數(shù)學(xué)運(yùn)算技巧總結(jié)

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

相關(guān)文章

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

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

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

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

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

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

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

    PHP中ADODB類詳解

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

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

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

    PHP面相對(duì)象中的重載與重寫

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

    php計(jì)算年齡精準(zhǔn)到年月日

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

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

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

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

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

    防止本地用戶用fsockopen DDOS攻擊對(duì)策

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

最新評(píng)論

淮北市| 沂源县| 怀柔区| 黄浦区| 抚松县| 灵宝市| 宜城市| 绥芬河市| 宜春市| 临湘市| 同心县| 双城市| 大厂| 闵行区| 阳高县| 安乡县| 云龙县| 沅陵县| 元阳县| 东明县| 南安市| 定州市| 集安市| 思茅市| 裕民县| 台前县| 松桃| 泗阳县| 万全县| 农安县| 呈贡县| 南靖县| 重庆市| 定兴县| 溧水县| 乌恰县| 北宁市| 孝昌县| 安丘市| 宁乡县| 天台县|