溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點(diǎn)擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

JavaScript中二叉樹如何實(shí)現(xiàn)查找最小值、最大值、給定值算法

發(fā)布時(shí)間:2021-07-22 14:30:57 來源:億速云 閱讀:336 作者:小新 欄目:web開發(fā)

小編給大家分享一下JavaScript中二叉樹如何實(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.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上的最小值
*因?yàn)檩^小的值總是在左子節(jié)點(diǎn)上,在BST上查找最小值,只需要遍歷左子樹,直到找到最后一個(gè)節(jié)點(diǎn)。*/
function getMin(){
  var current = this.root;
  while(!(current.left == null)) {
    current = current.left;
  }
//  return current;//返回最小值所在的節(jié)點(diǎn)
  return current.data;//返回最小值
}
/*
 *查找BST上的最大值
 *因?yàn)檩^大的值總是在右子節(jié)點(diǎn)上,在BST上查找最大值,只需要遍歷右子樹,直到找到最后一個(gè)節(jié)點(diǎn)。*/
function getMax() {
  var current = this.root;
  while(!(current.right == null)) {
    current = current.right;
  }
//  return current;//返回最大值所在的節(jié)點(diǎn)
  return current.data;//返回最大值
}
/*
*查找給定值
*在BST上查找給定值,需要比較該值和當(dāng)前節(jié)點(diǎn)上的值的大小。
*通過比較,就能確定如果給定值不在當(dāng)前節(jié)點(diǎn)時(shí),該向左遍歷還是向右遍歷。*/
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());
}

運(yùn)行結(jié)果:

JavaScript中二叉樹如何實(shí)現(xiàn)查找最小值、最大值、給定值算法

看完了這篇文章,相信你對“JavaScript中二叉樹如何實(shí)現(xiàn)查找最小值、最大值、給定值算法”有了一定的了解,如果想了解更多相關(guān)知識,歡迎關(guān)注億速云行業(yè)資訊頻道,感謝各位的閱讀!

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI