JavaScript數(shù)據(jù)結(jié)構(gòu)與算法之二叉樹(shù)添加/刪除節(jié)點(diǎn)操作示例
本文實(shí)例講述了JavaScript數(shù)據(jù)結(jié)構(gòu)與算法之二叉樹(shù)添加/刪除節(jié)點(diǎ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.getMin = getMin; this.getMax = getMax; this.find = find; this.remove = remove; } 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(current) { 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 inOrder(node) { if(!(node == null)) { inOrder(node.left); console.log(node.show()); inOrder(node.right); } } // 先序遍歷 function preOrder(node) { if(!(node == null)) { console.log(node.show()); preOrder(node.left); preOrder(node.right); } } // 后序遍歷 function postOrder(node) { if(!(node == null)) { postOrder(node.left); postOrder(node.right); console.log("后序遍歷"+node.show()); } } // 二叉樹(shù)查找最小值 function getMin(){ var current = this.root; while(!(current.left == null)) { current = current.left; } return current.data; } // 二叉樹(shù)上查找最大值 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(current.data == data) { return current; }else if(data < current.data) { current = current.left; }else { current = current.right; } } return null; } function remove(data) { root = removeNode(this.root,data); } function getSmallest(node) { if (node.left == null) { return node; } else { return getSmallest(node.left); } } function removeNode(node,data) { if(node == null) { return null; } if(data == node.data) { // 沒(méi)有子節(jié)點(diǎn)的節(jié)點(diǎn) if(node.left == null && node.right == null) { return null; } // 沒(méi)有左子節(jié)點(diǎn)的節(jié)點(diǎn) if(node.left == null) { return node.right; } // 沒(méi)有右子節(jié)點(diǎn)的節(jié)點(diǎn) if(node.right == null) { return node.left; } // 有2個(gè)子節(jié)點(diǎn)的節(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; } } //代碼初始化如下: var nums = new BST(); nums.insert(23); nums.insert(45); nums.insert(16); nums.insert(37); nums.insert(3); nums.insert(99); nums.insert(22); var min = nums.getMin(); console.log(min); var max = nums.getMax(); console.log(max); var value = nums.find("45"); console.log(value); nums.remove(23);
運(yùn)行結(jié)果:
感興趣的朋友可以使用在線(xiàn)HTML/CSS/JavaScript代碼運(yùn)行工具:http://tools.jb51.net/code/HtmlJsRun測(cè)試上述代碼運(yùn)行效果。
更多關(guān)于JavaScript相關(guān)內(nèi)容感興趣的讀者可查看本站專(zhuān)題:《JavaScript數(shù)學(xué)運(yùn)算用法總結(jié)》、《JavaScript數(shù)據(jù)結(jié)構(gòu)與算法技巧總結(jié)》、《JavaScript數(shù)組操作技巧總結(jié)》、《JavaScript排序算法總結(jié)》、《JavaScript遍歷算法與技巧總結(jié)》、《JavaScript查找算法技巧總結(jié)》及《JavaScript錯(cuò)誤與調(diào)試技巧總結(jié)》
希望本文所述對(duì)大家JavaScript程序設(shè)計(jì)有所幫助。
- JavaScript數(shù)據(jù)結(jié)構(gòu)與算法之二叉樹(shù)遍歷算法詳解【先序、中序、后序】
- JavaScript數(shù)據(jù)結(jié)構(gòu)與算法之二叉樹(shù)實(shí)現(xiàn)查找最小值、最大值、給定值算法示例
- JS中的算法與數(shù)據(jù)結(jié)構(gòu)之二叉查找樹(shù)(Binary Sort Tree)實(shí)例詳解
- js實(shí)現(xiàn)無(wú)限層級(jí)樹(shù)形數(shù)據(jù)結(jié)構(gòu)(創(chuàng)新算法)
- javascript將扁平的數(shù)據(jù)轉(zhuǎn)為樹(shù)形結(jié)構(gòu)的高效率算法
- 淺談JavaScript構(gòu)造樹(shù)形結(jié)構(gòu)的一種高效算法
- JavaScript樹(shù)結(jié)構(gòu)深度優(yōu)先算法
相關(guān)文章
360doc網(wǎng)站不登錄就無(wú)法復(fù)制內(nèi)容的解決方法
這篇文章主要介紹了360doc網(wǎng)站不登錄就無(wú)法復(fù)制內(nèi)容的解決方法,需要的朋友可以參考下2018-01-01javascript得到XML某節(jié)點(diǎn)的子節(jié)點(diǎn)個(gè)數(shù)的腳本
得到XML某節(jié)點(diǎn)(pnode)的子節(jié)點(diǎn)個(gè)數(shù)(客戶(hù)端)2008-10-10根據(jù)服務(wù)器時(shí)間作為起始,顯示時(shí)鐘的小程序
一般的網(wǎng)頁(yè)都有這種功能:在頁(yè)面上動(dòng)態(tài)顯示當(dāng)前時(shí)間,這個(gè)的實(shí)現(xiàn)也很簡(jiǎn)單,基本上一行代碼就實(shí)現(xiàn)了2009-06-06原生JavaScript創(chuàng)建不可變對(duì)象的方法簡(jiǎn)單示例
這篇文章主要介紹了原生JavaScript創(chuàng)建不可變對(duì)象的方法,結(jié)合簡(jiǎn)單實(shí)例形式分析了基于原生JavaScript創(chuàng)建不可變對(duì)象的相關(guān)原理、實(shí)現(xiàn)方法與操作注意事項(xiàng),需要的朋友可以參考下2020-05-05使用JavaScript實(shí)現(xiàn)一個(gè)物理模擬
最近掌門(mén)人在寫(xiě)3D游戲,對(duì)于其中的物理效果很感興趣,今天我將使用純JavaScript來(lái)實(shí)現(xiàn)一個(gè)簡(jiǎn)易的物理模擬,其中包括碰撞檢測(cè)與響應(yīng)、摩擦力與空氣阻力、以及物體的破壞效果,文中通過(guò)代碼示例講解的非常詳細(xì),需要的朋友可以參考下2024-01-01javascript的offset、client、scroll使用方法詳解
javascript的offset、client、scroll在使用過(guò)程中非常頻繁,接下來(lái)將對(duì)此進(jìn)行一一介紹,需要了解的朋友可以詳細(xì)參考下2012-12-12微信小程序?qū)崿F(xiàn)團(tuán)購(gòu)或秒殺批量倒計(jì)時(shí)
這篇文章主要為大家詳細(xì)介紹了微信小程序?qū)崿F(xiàn)團(tuán)購(gòu)或秒殺批量倒計(jì)時(shí),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-07-07快速獲取/設(shè)置iframe內(nèi)對(duì)象元素的幾種js實(shí)現(xiàn)方法
下面小編就為大家?guī)?lái)一篇快速獲取/設(shè)置iframe內(nèi)對(duì)象元素的幾種js實(shí)現(xiàn)方法。小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2016-05-05