javascript如何實(shí)現(xiàn)二叉樹-創(chuàng)新互聯(lián)

小編給大家分享一下javascript如何實(shí)現(xiàn)二叉樹,相信大部分人都還不怎么了解,因此分享這篇文章給大家參考一下,希望大家閱讀完這篇文章后大有收獲,下面讓我們一起去了解一下吧!

創(chuàng)新互聯(lián)-專業(yè)網(wǎng)站定制、快速模板網(wǎng)站建設(shè)、高性價比遂昌網(wǎng)站開發(fā)、企業(yè)建站全套包干低至880元,成熟完善的模板庫,直接使用。一站式遂昌網(wǎng)站制作公司更省心,省錢,快速模板網(wǎng)站建設(shè)找我們,業(yè)務(wù)覆蓋遂昌地區(qū)。費(fèi)用合理售后完善,10余年實(shí)體公司更值得信賴。

前言:

javascript如何實(shí)現(xiàn)二叉樹

二叉樹的特點(diǎn)(例圖只是二叉樹的一種情況,不要嘗試用例圖推理以下結(jié)論)

  1. 除了最下面一層,每個節(jié)點(diǎn)都是父節(jié)點(diǎn),每個節(jié)點(diǎn)都有且最多有兩個子節(jié)點(diǎn);

  2. 除了嘴上面一層,每個節(jié)點(diǎn)是子節(jié)點(diǎn),每個節(jié)點(diǎn)都會有一個父節(jié)點(diǎn);

  3. 最上面一層的節(jié)點(diǎn)(即例圖中的節(jié)點(diǎn)50)為根節(jié)點(diǎn);

javascript如何實(shí)現(xiàn)二叉樹

最下面一層的節(jié)點(diǎn)稱為葉子節(jié)點(diǎn),他們沒有子節(jié)點(diǎn);

javascript如何實(shí)現(xiàn)二叉樹

左子節(jié)點(diǎn)的值 < 父節(jié)點(diǎn)的值 <= 右節(jié)點(diǎn)的值

1 節(jié)點(diǎn)的javascript實(shí)現(xiàn)

// 節(jié)點(diǎn)對象
function Node(data, left, right) {
  this.data = data; // 節(jié)點(diǎn)值
  this.left = left; // 當(dāng)前節(jié)點(diǎn)的左子節(jié)點(diǎn)
  this.right = right; // 當(dāng)前節(jié)點(diǎn)的右子節(jié)點(diǎn)
  this.show = show; // 輔助function
}

function show() {
  return this.data;
}

感受下上面實(shí)現(xiàn)節(jié)點(diǎn)的代碼,感覺和鏈表有點(diǎn)相似不是嗎,存著當(dāng)前值,又存著下個節(jié)點(diǎn)(左、右子節(jié)點(diǎn))的引用,下面是一張偽代碼的草圖:

javascript如何實(shí)現(xiàn)二叉樹

2 二叉樹的實(shí)現(xiàn)

實(shí)現(xiàn)二叉樹,當(dāng)然就是要插入節(jié)點(diǎn)構(gòu)成二叉樹,先看看實(shí)現(xiàn)二叉樹的js代碼

function BST() {
  this.root = null;
  this.insert = insert;
}

function insert(data) {
  var n = new Node(data, null, null);
  if (this.root == null) {
   this.root = n;
  }
  else {
   var current = this.root;
   var parent;
   while (true) {
     parent = current;
     if (data < current.data) {
      current = current.left;
      if (current == null) {
        parent.left = n;
        break;
      }
     }
     else {
      current = current.right;
      if (current == null) {
        parent.right = n;
        break;
      }
     }
   }
  }
}

然后是看一下偽代碼:

function BST() {
  this.root = null; // 根節(jié)點(diǎn)
  this.insert = insert;
}

function insert(data) {
  // 初始化一個節(jié)點(diǎn),為什么要將左右子節(jié)點(diǎn)的引用初始化為空呢,因?yàn)榭赡苁侨~子節(jié)點(diǎn),加入他有子節(jié)點(diǎn),會在下面的代碼添加
  var n = new Node(data, null, null);
  if (該二叉樹是否為空,是空則根節(jié)點(diǎn)為空,因此可以用根節(jié)點(diǎn)判斷二叉樹是否為空) {
   // 將當(dāng)前節(jié)點(diǎn)存為根節(jié)點(diǎn)
   this.root = n;
  }
  else {
   // 來到這里就表示,該二叉樹不為空,這里關(guān)鍵的是兩句代碼:
   // 0.while (true);
   // 1.parent = current;
   // 2.current = current.left;/current = current.right;
   // 3.break;
   var current = this.root;
   var parent;
   while (true) {
     parent = current; // 獲得父節(jié)點(diǎn),第一次循環(huán),那么父節(jié)點(diǎn)就是根節(jié)點(diǎn)
     if (data < current.data) { // 當(dāng)前節(jié)點(diǎn)值小于父節(jié)點(diǎn)的值就是存左邊,記得二叉樹的特點(diǎn)吧,如果真是小于父節(jié)點(diǎn),那么就說明該節(jié)點(diǎn)屬于,該父節(jié)點(diǎn)的左子樹。
      current = current.left;
      if (current == null) {
        parent.left = n;
        break;
      }

      // 其實(shí)上面這樣寫不好理解,可以等價于下面的代碼:
      // start
      if(current.left == null){ // 若果左節(jié)點(diǎn)空,那么這個空的節(jié)點(diǎn)就是我們要插入的位置
        current.left = n;
        break;
      }else{
        // 不空則繼續(xù)往下一層找空節(jié)點(diǎn)(插入的位置)
        current = current.left;
      }
      // end
     }
     else {
      // 右節(jié)點(diǎn)的邏輯代碼個左節(jié)點(diǎn)的一樣的
      current = current.right;
      if (current == null) {
        parent.right = n;
        break;
      }
     }
   }
  }
}

下面是一個更好理解的插入函數(shù)

function insert(data) {
  var n = new Node(data, null, null);
  if (this.root == null) {
   this.root = n;
  }
  else {
   var current = this.root;
   // start change
   while (true) {
     if (data < current.data) {
      if (current.left == null) {
        current.left = n;
        break;
      }else{
        current = current.left;
      }
     }else {
      if (current.right == null) {
        current.right = n;
        break;
      }else{
        current = current.right;
      }
     }
   }
  }
}

小結(jié):

二叉樹的實(shí)現(xiàn)的三個部件

Node對象

function Node(data, left, right) { ... }

BST對象

function BST() { ... }

插入節(jié)點(diǎn)函數(shù)

function insert(data) { ... }

以上是“javascript如何實(shí)現(xiàn)二叉樹”這篇文章的所有內(nèi)容,感謝各位的閱讀!相信大家都有了一定的了解,希望分享的內(nèi)容對大家有所幫助,如果還想學(xué)習(xí)更多知識,歡迎關(guān)注創(chuàng)新互聯(lián)網(wǎng)站建設(shè)公司行業(yè)資訊頻道!

另外有需要云服務(wù)器可以了解下創(chuàng)新互聯(lián)建站muchs.cn,海內(nèi)外云服務(wù)器15元起步,三天無理由+7*72小時售后在線,公司持有idc許可證,提供“云服務(wù)器、裸金屬服務(wù)器、高防服務(wù)器、香港服務(wù)器、美國服務(wù)器、虛擬主機(jī)、免備案服務(wù)器”等云主機(jī)租用服務(wù)以及企業(yè)上云的綜合解決方案,具有“安全穩(wěn)定、簡單易用、服務(wù)可用性高、性價比高”等特點(diǎn)與優(yōu)勢,專為企業(yè)上云打造定制,能夠滿足用戶豐富、多元化的應(yīng)用場景需求。

網(wǎng)頁名稱:javascript如何實(shí)現(xiàn)二叉樹-創(chuàng)新互聯(lián)
瀏覽路徑:http://muchs.cn/article28/pspjp.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供關(guān)鍵詞優(yōu)化、企業(yè)建站、域名注冊、全網(wǎng)營銷推廣、ChatGPT、網(wǎng)站維護(hù)

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)

成都seo排名網(wǎng)站優(yōu)化