重慶分公司,新征程啟航
為企業(yè)提供網(wǎng)站建設(shè)、域名注冊、服務(wù)器等服務(wù)
為企業(yè)提供網(wǎng)站建設(shè)、域名注冊、服務(wù)器等服務(wù)
小編給大家分享一下javascript如何實現(xiàn)二叉樹,相信大部分人都還不怎么了解,因此分享這篇文章給大家參考一下,希望大家閱讀完這篇文章后大有收獲,下面讓我們一起去了解一下吧!
站在用戶的角度思考問題,與客戶深入溝通,找到集寧網(wǎng)站設(shè)計與集寧網(wǎng)站推廣的解決方案,憑借多年的經(jīng)驗,讓設(shè)計與互聯(lián)網(wǎng)技術(shù)結(jié)合,創(chuàng)造個性化、用戶體驗好的作品,建站類型包括:成都網(wǎng)站建設(shè)、做網(wǎng)站、企業(yè)官網(wǎng)、英文網(wǎng)站、手機(jī)端網(wǎng)站、網(wǎng)站推廣、域名注冊、網(wǎng)絡(luò)空間、企業(yè)郵箱。業(yè)務(wù)覆蓋集寧地區(qū)。
前言:
二叉樹的特點(例圖只是二叉樹的一種情況,不要嘗試用例圖推理以下結(jié)論)
除了最下面一層,每個節(jié)點都是父節(jié)點,每個節(jié)點都有且最多有兩個子節(jié)點;
除了嘴上面一層,每個節(jié)點是子節(jié)點,每個節(jié)點都會有一個父節(jié)點;
最上面一層的節(jié)點(即例圖中的節(jié)點50)為根節(jié)點;
最下面一層的節(jié)點稱為葉子節(jié)點,他們沒有子節(jié)點;
左子節(jié)點的值 < 父節(jié)點的值 <= 右節(jié)點的值
1 節(jié)點的javascript實現(xiàn)
// 節(jié)點對象 function Node(data, left, right) { this.data = data; // 節(jié)點值 this.left = left; // 當(dāng)前節(jié)點的左子節(jié)點 this.right = right; // 當(dāng)前節(jié)點的右子節(jié)點 this.show = show; // 輔助function } function show() { return this.data; }
感受下上面實現(xiàn)節(jié)點的代碼,感覺和鏈表有點相似不是嗎,存著當(dāng)前值,又存著下個節(jié)點(左、右子節(jié)點)的引用,下面是一張偽代碼的草圖:
2 二叉樹的實現(xiàn)
實現(xiàn)二叉樹,當(dāng)然就是要插入節(jié)點構(gòu)成二叉樹,先看看實現(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é)點 this.insert = insert; } function insert(data) { // 初始化一個節(jié)點,為什么要將左右子節(jié)點的引用初始化為空呢,因為可能是葉子節(jié)點,加入他有子節(jié)點,會在下面的代碼添加 var n = new Node(data, null, null); if (該二叉樹是否為空,是空則根節(jié)點為空,因此可以用根節(jié)點判斷二叉樹是否為空) { // 將當(dāng)前節(jié)點存為根節(jié)點 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é)點,第一次循環(huán),那么父節(jié)點就是根節(jié)點 if (data < current.data) { // 當(dāng)前節(jié)點值小于父節(jié)點的值就是存左邊,記得二叉樹的特點吧,如果真是小于父節(jié)點,那么就說明該節(jié)點屬于,該父節(jié)點的左子樹。 current = current.left; if (current == null) { parent.left = n; break; } // 其實上面這樣寫不好理解,可以等價于下面的代碼: // start if(current.left == null){ // 若果左節(jié)點空,那么這個空的節(jié)點就是我們要插入的位置 current.left = n; break; }else{ // 不空則繼續(xù)往下一層找空節(jié)點(插入的位置) current = current.left; } // end } else { // 右節(jié)點的邏輯代碼個左節(jié)點的一樣的 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é):
二叉樹的實現(xiàn)的三個部件
Node對象
function Node(data, left, right) { ... }
BST對象
function BST() { ... }
插入節(jié)點函數(shù)
function insert(data) { ... }
以上是“javascript如何實現(xiàn)二叉樹”這篇文章的所有內(nèi)容,感謝各位的閱讀!相信大家都有了一定的了解,希望分享的內(nèi)容對大家有所幫助,如果還想學(xué)習(xí)更多知識,歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道!