JS二叉樹(shù)的簡(jiǎn)單實(shí)現(xiàn)方法示例
本文實(shí)例講述了JS二叉樹(shù)的簡(jiǎn)單實(shí)現(xiàn)方法。分享給大家供大家參考,具體如下:
今天學(xué)習(xí)了一下 二叉樹(shù)的實(shí)現(xiàn),在此記錄一下
簡(jiǎn)單的二叉樹(shù)實(shí)現(xiàn),并且實(shí)現(xiàn)升序和降序排序輸出
function Node(data , left,right){ this.data = data; this.left = left; this.right = right; this.show = show; function show(){ return this.data; } }; function Bst(){ this.root = null; this.insert = insert;//插入 this.inOrder = inOrder;//中序遍歷(升序) this.inOrderDesc = inOrderDesc;//中序遍歷(降序) this.preOrder = preOrder;//先序遍歷 this.postOrder = postOrder;//后續(xù)遍歷 this.getMin = getMin;//最大值 this.getMax = getMax;//最小值 this.find = find;//查找值 this.remove = remove;//刪除節(jié)點(diǎn) this.count = count;//獲取節(jié)點(diǎn)數(shù)量 function insert(data){ //創(chuàng)建一個(gè)新的節(jié)點(diǎn) var newNode = new Node(data,null,null); //判斷是否存在根節(jié)點(diǎn),沒(méi)有將新節(jié)點(diǎn)存入 if(this.root == null){ this.root = newNode; }else{ //獲取根節(jié)點(diǎn) var current = this.root; var parent; while(true){ //將當(dāng)前節(jié)點(diǎn)保存為父節(jié)點(diǎn) parent = current; //將小的數(shù)據(jù)放在左節(jié)點(diǎn) if(data < current.data){ //獲取當(dāng)前節(jié)點(diǎn)的左節(jié)點(diǎn) //判斷當(dāng)前節(jié)點(diǎn)下的左節(jié)點(diǎn)是否有數(shù)據(jù) current = current.left; if(current == null){ //如果沒(méi)有數(shù)據(jù)將新節(jié)點(diǎn)存入當(dāng)前節(jié)點(diǎn)下的左節(jié)點(diǎn) parent.left = newNode; break; } }else{ current = current.right; if(current == null){ parent.right = newNode; break; } } } } } function inOrder(node){ var data = []; _inOrder(node,data); return data; } function inOrderDesc(node){ var data = []; _inOrderDesc(node,data); return data; } function preOrder(node){ var data = []; _preOrder(node,data); return data; } function postOrder(node){ var data = []; _postOrder(node,data); return data; } function _inOrder(node,data){ if(!(node == null)){ _inOrder(node.left,data); data.push(node.show()); _inOrder(node.right,data); } } function _inOrderDesc(node,data){ debugger; if(!(node == null)){ _inOrderDesc(node.right,data); data.push(node.show()); _inOrderDesc(node.left,data); } } function _preOrder(node,data){ if(!(node == null)){ data.push(node.show()); _preOrder(node.left,data); _preOrder(node.right,data); } } function _postOrder(node,data){ if(!(node == null)){ _postOrder(node.left,data); _postOrder(node.right,data); data.push(node.show()); } } function getMin(){ var current = this.root; while(!(current.left == null)){ current = current.left; } return current.data; } function getMax(){ var current = this.root; while(!(current.right == null)){ current = current.right; } return current.data; } function find(data){ var current = this.root; while(current != null){ if(data == current.data){ return current; }else if(data < current.data){ current = current.left; }else{ current = current.right; } } return null; } function getSmallest(node){ var current = node; while(!(current.left == null)){ current = current.left; } return current; } function remove(data){ root = removeNode(this.root,data); } function removeNode(node,data){ if(node == null){ return null; } if(data == node.data){ //如果沒(méi)有只節(jié)點(diǎn) if(node.left == null && node.right){ return null; } //如果沒(méi)有左節(jié)點(diǎn) if(node.left == null){ return node.right; } //如果沒(méi)有右節(jié)點(diǎn) if(node.right == null){ return node.left; } //有兩節(jié)點(diǎn) var tempNode = getSmallest(node.right); node.data = tempNode.data; node.right = removeNode(node.right,tempNode.data); return node; }else if(data < node.data){ node.left = removeNode(node.left,data); return node; }else{ node.right = removeNode(node.right,data); return node; } } function count(){ var counts = 0; var current = this.root; if(current == null){ return counts; } return _count(current,counts); } function _count(node,counts){ debugger; if(!(node == null)){ counts ++; counts = _count(node.left,counts);; counts = _count(node.right,counts); } return counts; } }
更多關(guān)于JavaScript相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《JavaScript數(shù)據(jù)結(jié)構(gòu)與算法技巧總結(jié)》、《JavaScript數(shù)學(xué)運(yùn)算用法總結(jié)》、《JavaScript排序算法總結(jié)》、《JavaScript遍歷算法與技巧總結(jié)》、《JavaScript查找算法技巧總結(jié)》及《JavaScript錯(cuò)誤與調(diào)試技巧總結(jié)》
希望本文所述對(duì)大家JavaScript程序設(shè)計(jì)有所幫助。
- JS中的二叉樹(shù)遍歷詳解
- JS實(shí)現(xiàn)的二叉樹(shù)算法完整實(shí)例
- JavaScript數(shù)據(jù)結(jié)構(gòu)和算法之二叉樹(shù)詳解
- JavaScript實(shí)現(xiàn)二叉樹(shù)的先序、中序及后序遍歷方法詳解
- JavaScript數(shù)據(jù)結(jié)構(gòu)之二叉樹(shù)的刪除算法示例
- JavaScript數(shù)據(jù)結(jié)構(gòu)之二叉樹(shù)的查找算法示例
- JavaScript數(shù)據(jù)結(jié)構(gòu)之二叉樹(shù)的遍歷算法示例
- JavaScript實(shí)現(xiàn)二叉樹(shù)定義、遍歷及查找的方法詳解
- JavaScript數(shù)據(jù)結(jié)構(gòu)之二叉樹(shù)的計(jì)數(shù)算法示例
- JavaScript數(shù)據(jù)結(jié)構(gòu)與算法之二叉樹(shù)插入節(jié)點(diǎn)、生成二叉樹(shù)示例
相關(guān)文章
firefox下對(duì)ajax的onreadystatechange的支持情況分析
firefox下對(duì)ajax的onreadystatechange的支持分析。用的到的朋友可以參考下。2009-12-12基于Bootstrap使用jQuery實(shí)現(xiàn)簡(jiǎn)單可編輯表格
這篇文章主要介紹了基于Bootstrap使用jQuery實(shí)現(xiàn)簡(jiǎn)單可編輯表格的相關(guān)資料,需要的朋友可以參考下2016-05-05JavaScript實(shí)現(xiàn)獲取本機(jī)IP地址
這篇文章主要介紹了JavaScript實(shí)現(xiàn)獲取本機(jī)IP地址方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-07-07JS實(shí)現(xiàn)的最簡(jiǎn)Table選項(xiàng)卡效果
這篇文章主要介紹了JS實(shí)現(xiàn)的最簡(jiǎn)Table選項(xiàng)卡效果,涉及簡(jiǎn)單的JavaScript響應(yīng)鼠標(biāo)事件切換樣式的技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下2015-10-10JS取request值以及自動(dòng)執(zhí)行使用示例
在網(wǎng)頁(yè)中JS函數(shù)自動(dòng)執(zhí)行常用三種方法,下面為大家詳細(xì)介紹下JS取request值以及自動(dòng)執(zhí)行使用,需要的朋友可以參考下2014-02-02基于jquery ajax的多文件上傳進(jìn)度條過(guò)程解析
這篇文章主要介紹了基于jquery ajax的多文件上傳進(jìn)度條過(guò)程解析,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2019-09-09學(xué)習(xí)JavaScript設(shè)計(jì)模式之享元模式
這篇文章主要為大家介紹了JavaScript設(shè)計(jì)模式中的享元模式,對(duì)JavaScript設(shè)計(jì)模式感興趣的小伙伴們可以參考一下2016-01-01