JS實(shí)現(xiàn)二叉查找樹(shù)的建立以及一些遍歷方法實(shí)現(xiàn)
二叉查找樹(shù)是由節(jié)點(diǎn)和邊組成的。
我們可以定義一個(gè)節(jié)點(diǎn)類(lèi)Node,里面存放節(jié)點(diǎn)的數(shù)據(jù),及左右子節(jié)點(diǎn),再定義一個(gè)用來(lái)顯示數(shù)據(jù)的方法:
//以下定義一個(gè)節(jié)點(diǎn)類(lèi) function Node(data,left,right){ // 節(jié)點(diǎn)的鍵值 this.data = data; // 左節(jié)點(diǎn) this.left = left; // 右節(jié)點(diǎn) this.right = left; // 顯示該節(jié)點(diǎn)的鍵值 this.show = show; } // 實(shí)現(xiàn)show方法 function show(){ return this.data; }
再定義一個(gè)二叉查找樹(shù)類(lèi)BST,該類(lèi)中有定義樹(shù)的根節(jié)點(diǎn),初始化為null,然后定義插入節(jié)點(diǎn)的方法,還有一邊遍歷的方法:
// 二叉查找樹(shù)BST // 有一個(gè)節(jié)點(diǎn)屬性,還有一些其他的方法,以下定義一個(gè)二叉查找樹(shù)BST類(lèi) function BST(){ // 根節(jié)點(diǎn)初始化為空 this.root = null; // 方法 // 插入 this.insert = insert; // 中序遍歷 this.inorder = inorder; // 先序遍歷 this.preorder = preorder; // 后序遍歷 this.postorder = postorder; } //實(shí)現(xiàn)insert插入方法 function insert(data){ // 創(chuàng)建一個(gè)節(jié)點(diǎn)保存數(shù)據(jù) var node = new Node(data,null,null); // 下面將節(jié)點(diǎn)node插入到樹(shù)中 // 如果樹(shù)是空的,就將節(jié)點(diǎn)設(shè)為根節(jié)點(diǎn) if(!this.root){ this.root = node; }else{ //樹(shù)不為空 // 判斷插在父節(jié)點(diǎn)的左邊還是右邊 // 所以先要保存一下父節(jié)點(diǎn) // var parent = this.root; var current = this.root; var parent; // 如果要插入的節(jié)點(diǎn)鍵值小于父節(jié)點(diǎn)鍵值,則插在父節(jié)點(diǎn)左邊, // 前提是父節(jié)點(diǎn)的左邊為空,否則要將父節(jié)點(diǎn)往下移一層, // 然后再做判斷 while(true){ // data小于父節(jié)點(diǎn)的鍵值 parent = current; if(data < parent.data){ // 將父節(jié)點(diǎn)往左下移(插入左邊) // parent = parent.left; current = current.left; // 如果節(jié)點(diǎn)為空,則直接插入 if(!current){ // ?。。〈颂幪貏e注意,如果就這樣把parent賦值為node,也僅僅只是parent指向node, // 而并沒(méi)有加到父元素的左邊?。?!根本沒(méi)有加到樹(shù)中去。所以要先記住父元素,再把當(dāng)前元素加入進(jìn)去 parent.left = node; break; } }else{ // 將父節(jié)點(diǎn)往右下移(插入右邊) current = current.right; if(!current){ parent.right = node; break; } } } } } //實(shí)現(xiàn)inorder遍歷方法(左中右) function inorder(node){ if(node){ inorder(node.left); console.log(node.show()); inorder(node.right); } } // 先序遍歷(中左右) function preorder(node){ if(node){ console.log(node.show()); preorder(node.left); preorder(node.right); } } // 后序遍歷(左右中) function postorder(node){ if(node){ preorder(node.left); preorder(node.right); console.log(node.show()); } }
測(cè)試:
// 后序遍歷(左右中) function postorder(node){ if(node){ postorder(node.left); postorder(node.right); console.log(node.show()); } } // 實(shí)例化一個(gè)BST樹(shù) var tree = new BST(); // 添加節(jié)點(diǎn) tree.insert(30); tree.insert(14); tree.insert(35); tree.insert(12); tree.insert(17); // 中序遍歷 tree.inorder(tree.root); // 先序遍歷 tree.preorder(tree.root); // 后序遍歷 tree.postorder(tree.root);
結(jié)果:
中序遍歷:
先序遍歷:
后序遍歷:
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- JavaScript數(shù)據(jù)結(jié)構(gòu)之二叉查找樹(shù)的定義與表示方法
- JavaScript數(shù)據(jù)結(jié)構(gòu)與算法之二叉樹(shù)實(shí)現(xiàn)查找最小值、最大值、給定值算法示例
- JavaScript實(shí)現(xiàn)二叉樹(shù)定義、遍歷及查找的方法詳解
- JavaScript數(shù)據(jù)結(jié)構(gòu)之二叉樹(shù)的查找算法示例
- JS實(shí)現(xiàn)的二叉樹(shù)算法完整實(shí)例
- JavaScript實(shí)現(xiàn)二叉樹(shù)的先序、中序及后序遍歷方法詳解
- javascript實(shí)現(xiàn)二叉樹(shù)遍歷的代碼
- Javascript實(shí)現(xiàn)從小到大的數(shù)組轉(zhuǎn)換成二叉搜索樹(shù)
- JavaScript數(shù)據(jù)結(jié)構(gòu)之二叉樹(shù)的刪除算法示例
- JavaScript實(shí)現(xiàn)的DOM樹(shù)遍歷方法詳解【二叉DOM樹(shù)、多叉DOM樹(shù)】
- JavaScript數(shù)據(jù)結(jié)構(gòu)之二叉樹(shù)的遍歷算法示例
- JS中的算法與數(shù)據(jù)結(jié)構(gòu)之二叉查找樹(shù)(Binary Sort Tree)實(shí)例詳解
相關(guān)文章
詳解JavaScript閉包的優(yōu)缺點(diǎn)和作用
閉包是指在 JavaScript 中,內(nèi)部函數(shù)可以訪問(wèn)其外部函數(shù)作用域中的變量,即使外部函數(shù)已經(jīng)執(zhí)行完畢,這種特性被稱(chēng)為閉包,本文將給大家介紹一下JavaScript閉包的優(yōu)缺點(diǎn)和作用,需要的朋友可以參考下2023-09-09js獲取修改title與jQuery獲取修改title的方法
這篇文章主要介紹了js獲取修改title與jQuery獲取修改title的方法,本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2023-02-02網(wǎng)頁(yè)前端登錄js按Enter回車(chē)鍵實(shí)現(xiàn)登陸的兩種方法
下面小編就為大家?guī)?lái)一篇網(wǎng)頁(yè)前端登錄js按Enter回車(chē)鍵實(shí)現(xiàn)登陸的兩種方法。小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考2016-05-05一個(gè)簡(jiǎn)單的js漸顯(fadeIn)漸隱(fadeOut)類(lèi)
最近發(fā)現(xiàn)項(xiàng)目用的表單驗(yàn)證不好使,干脆一邊參考人家的一邊自己寫(xiě)了一個(gè)。在驗(yàn)證有錯(cuò)誤返回提示信息用到漸顯(fadeIn)漸隱(fadeOut)過(guò)渡(因?yàn)闉g覽器的效率實(shí)在太高了,一下就蹦了出來(lái)~~);2010-06-06