JavaScript數(shù)據(jù)結(jié)構(gòu)與算法之二叉樹實現(xiàn)查找最小值、最大值、給定值算法示例
本文實例講述了JavaScript數(shù)據(jù)結(jié)構(gòu)與算法之二叉樹實現(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.preOrder = preOrder;
this.inOrder = inOrder;
this.postOrder = postOrder;
this.getMin = getMin;//查找最小值
this.getMax = getMax;//查找最大值
this.find = find;//查找給定值
}
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());
}
}
/*
*查找BST上的最小值
*因為較小的值總是在左子節(jié)點上,在BST上查找最小值,只需要遍歷左子樹,直到找到最后一個節(jié)點。*/
function getMin(){
var current = this.root;
while(!(current.left == null)) {
current = current.left;
}
// return current;//返回最小值所在的節(jié)點
return current.data;//返回最小值
}
/*
*查找BST上的最大值
*因為較大的值總是在右子節(jié)點上,在BST上查找最大值,只需要遍歷右子樹,直到找到最后一個節(jié)點。*/
function getMax() {
var current = this.root;
while(!(current.right == null)) {
current = current.right;
}
// return current;//返回最大值所在的節(jié)點
return current.data;//返回最大值
}
/*
*查找給定值
*在BST上查找給定值,需要比較該值和當(dāng)前節(jié)點上的值的大小。
*通過比較,就能確定如果給定值不在當(dāng)前節(jié)點時,該向左遍歷還是向右遍歷。*/
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;
}
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 find = nums.find("88");
console.log( find);
if(find != null){
console.log("給定值為: " + find.data);
console.log("給定值為: " + find.show());
}
var find = nums.find("37");
console.log( find);
if(find != null){
console.log("給定值為: " + find.data);
console.log("給定值為: " + find.show());
}
運行結(jié)果:

感興趣的朋友可以使用在線HTML/CSS/JavaScript代碼運行工具:http://tools.jb51.net/code/HtmlJsRun測試上述代碼運行效果。
更多關(guān)于JavaScript相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《JavaScript數(shù)學(xué)運算用法總結(jié)》、《JavaScript數(shù)據(jù)結(jié)構(gòu)與算法技巧總結(jié)》、《JavaScript數(shù)組操作技巧總結(jié)》、《JavaScript排序算法總結(jié)》、《JavaScript遍歷算法與技巧總結(jié)》、《JavaScript查找算法技巧總結(jié)》及《JavaScript錯誤與調(diào)試技巧總結(jié)》
希望本文所述對大家JavaScript程序設(shè)計有所幫助。
- JavaScript數(shù)據(jù)結(jié)構(gòu)與算法之二叉樹遍歷算法詳解【先序、中序、后序】
- JavaScript數(shù)據(jù)結(jié)構(gòu)與算法之二叉樹添加/刪除節(jié)點操作示例
- JS中的算法與數(shù)據(jù)結(jié)構(gòu)之二叉查找樹(Binary Sort Tree)實例詳解
- js實現(xiàn)無限層級樹形數(shù)據(jù)結(jié)構(gòu)(創(chuàng)新算法)
- javascript將扁平的數(shù)據(jù)轉(zhuǎn)為樹形結(jié)構(gòu)的高效率算法
- 淺談JavaScript構(gòu)造樹形結(jié)構(gòu)的一種高效算法
- JavaScript樹結(jié)構(gòu)深度優(yōu)先算法
相關(guān)文章
javascript實現(xiàn)在網(wǎng)頁任意處點左鍵彈出隱藏菜單的方法
這篇文章主要介紹了javascript實現(xiàn)在網(wǎng)頁任意處點左鍵彈出隱藏菜單的方法,設(shè)計鼠標(biāo)事件及css樣式操作的相關(guān)技巧,需要的朋友可以參考下2015-05-05
js入門之Function函數(shù)的使用方法【新手必看】
本篇文章主要介紹js Function函數(shù)的使用方法,應(yīng)該對初學(xué)Js的朋友們會有所幫助,下面就隨小編一起來看下吧2016-11-11
Javascript ParentNode和ChildNode接口原理解析
這篇文章主要介紹了Javascript ParentNode和ChildNode接口原理解析,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下2020-03-03
Javascript使用uploadify來實現(xiàn)多文件上傳
本篇文章主要介紹了Javascript使用uploadify來實現(xiàn)多文件上傳,具有一定的參考價值,有需要的可以了解一下。2016-11-11

