怎樣使用PHP排序二叉樹

來源:互聯網
上載者:User
這次給大家帶來怎樣使用PHP排序二叉樹,使用PHP排序二叉樹的注意事項有哪些,下面就是實戰案例,一起來看一下。

這裡示範了排序二叉樹節點的插入,中序遍曆,極值的尋找和特定值的尋找的功能.

基本沒有提供什麼概念和定義.建議先簡單瞭解一下本文提供的幾個概念在來看本文.

實際上,只是簡單的提供了代碼,注釋也很少,各位辛苦了.

二叉樹:在電腦科學中,二叉樹是每個節點最多有兩個子樹的樹結構。

排序二叉樹: 左孩子節點的值小於父節點的值,右孩子節點的值大於父節點的值.

幾個概念:

根節點
葉子節點
左子樹
右子樹
中序遍曆
前序走訪
後序遍曆
二叉樹尋找

中序遍曆:

先遍曆左子樹,在遍曆本節點,在遍曆右節點.遍曆之後的結果就是排序好之後的結果

// created by 曲朋維// 排序二叉樹// 完成以下任務.// 1. 將節點插入到對應位置// 2. 使用中序遍曆遍曆這個二叉樹// 3. 找到這個二叉樹的極值// 4. 搜尋一個特定的值class Node{  public $key,$left,$right;  public function construct($key)  {    $this->key = $key;  }}class BinaryTree{  public $root;  public $sortArr = [];  // 插入節點  public function insertNode($node,$newNode){    if ($node->key < $newNode->key){      // 如果父節點小於子節點,插到右邊      if (empty($node->right)){        $node->right = $newNode;      }else{        $this->insertNode($node->right,$newNode);      }    }elseif ($node->key > $newNode->key){      // 如果父節點大於子節點,插到左邊      if (empty($node->left)){        $node->left = $newNode;      }else{        $this->insertNode($node->left,$newNode);      }    }  }  public function insert($key){    $newNode = new Node($key);    if (empty($this->root)){      $this->root = $newNode;    }else{      $this->insertNode($this->root,$newNode);    }  }  // 中序遍曆  public function midSort(){    $this->midSortNode($this->root);  }  public function midSortNode($node){    if (!empty($node)){      $this->midSortNode($node->left);      array_push($this->sortArr,$node->key);      $this->midSortNode($node->right);    }  }  // 尋找極值  public function findMin(){    //不斷的找它的左子樹,直到這個左子樹的節點為葉子節點.    if (!empty($this->root)){      $this->findMinNode($this->root);    }  }  public function findMinNode(Node $node){    if (!empty($node->left)){      $this->findMinNode($node->left);    }else{      echo '這個二叉樹的最小值為:'.$node->key;    }  }  public function findMax(){    if (!empty($this->root)){      $this->findMaxNode($this->root);    }  }  public function findMaxNode(Node $node){    if (!empty($node->right)){      $this->findMaxNode($node->right);    }else{      echo '這個二叉樹的最大值為:'.$node->key;    }  }  // 尋找特定的值  public function find($val = ''){    if (!empty($val)){      $this->findNode($this->root,$val);    }  }  public function findNode(Node $node,$val){    if ($node->key == $val){      echo '找到'.$val.'了';    }else if ($node->key > $val){      // 如果 父節點的值 大於要尋找的值,那麼尋找它的左子樹      if (!empty($node->left)){        $this->findNode($node->left,$val);      }else{        echo '沒有這個東西!';      }    }else if ($node->key < $val){      if (!empty($node->right)){        $this->findNode($node->right,$val);      }else{        echo '沒有這個東西!';      }    }  }}$tree = new BinaryTree();// 節點插入$nodes = array(8,3,10,1,6,14,4,7,13);foreach ($nodes as $value){  $tree->insert($value);}// 中序遍曆//$tree->midSort();//print_r($tree->sortArr);// 尋找極值//$tree->findMin();//$tree->findMax();// 尋找特定的值$tree->find(7);echo "<br/>";$tree->find(11);

運行結果:

找到7了
沒有這個東西!

相信看了本文案例你已經掌握了方法,更多精彩請關注php中文網其它相關文章!

推薦閱讀:

如何使JS數組與JSON對象動態實現添加、修改、刪除

如何使用Vue+Nuxt.js 實現服務端渲染

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.