亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

JavaScript數(shù)據(jù)結(jié)構(gòu)與算法之二叉樹(shù)實(shí)現(xiàn)查找最小值、最大值、給定值算法示例

 更新時(shí)間:2019年03月01日 11:48:51   作者:白楊-M  
這篇文章主要介紹了JavaScript數(shù)據(jù)結(jié)構(gòu)與算法之二叉樹(shù)實(shí)現(xiàn)查找最小值、最大值、給定值算法,涉及javascript二叉樹(shù)定義、賦值、遍歷、查找等相關(guān)操作技巧,需要的朋友可以參考下

本文實(shí)例講述了JavaScript數(shù)據(jù)結(jié)構(gòu)與算法之二叉樹(shù)實(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上查找最小值,只需要遍歷左子樹(shù),直到找到最后一個(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上查找最大值,只需要遍歷右子樹(shù),直到找到最后一個(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)上的值的大小。
*通過(guò)比較,就能確定如果給定值不在當(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é)果:

感興趣的朋友可以使用在線HTML/CSS/JavaScript代碼運(yùn)行工具http://tools.jb51.net/code/HtmlJsRun測(cè)試上述代碼運(yùn)行效果。

更多關(guān)于JavaScript相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《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ì)有所幫助。

相關(guān)文章

最新評(píng)論