欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

JavaScript数据结构与算法之二叉树实现查找最小值、最大值、给定值算法示例

程序员文章站 2022-07-04 21:43:32
本文实例讲述了javascript数据结构与算法之二叉树实现查找最小值、最大值、给定值算法。分享给大家供大家参考,具体如下: function node(data...

本文实例讲述了javascript数据结构与算法之二叉树实现查找最小值、最大值、给定值算法。分享给大家供大家参考,具体如下:

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上的最小值
*因为较小的值总是在左子节点上,在bst上查找最小值,只需要遍历左子树,直到找到最后一个节点。*/
function getmin(){
  var current = this.root;
  while(!(current.left == null)) {
    current = current.left;
  }
//  return current;//返回最小值所在的节点
  return current.data;//返回最小值
}
/*
 *查找bst上的最大值
 *因为较大的值总是在右子节点上,在bst上查找最大值,只需要遍历右子树,直到找到最后一个节点。*/
function getmax() {
  var current = this.root;
  while(!(current.right == null)) {
    current = current.right;
  }
//  return current;//返回最大值所在的节点
  return current.data;//返回最大值
}
/*
*查找给定值
*在bst上查找给定值,需要比较该值和当前节点上的值的大小。
*通过比较,就能确定如果给定值不在当前节点时,该向左遍历还是向右遍历。*/
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());
}

运行结果:

JavaScript数据结构与算法之二叉树实现查找最小值、最大值、给定值算法示例

感兴趣的朋友可以使用在线html/css/javascript代码运行工具http://tools.jb51.net/code/htmljsrun测试上述代码运行效果。

更多关于javascript相关内容感兴趣的读者可查看本站专题:《javascript数学运算用法总结》、《javascript数据结构与算法技巧总结》、《javascript数组操作技巧总结》、《javascript排序算法总结》、《javascript遍历算法与技巧总结》、《javascript查找算法技巧总结》及《javascript错误与调试技巧总结

希望本文所述对大家javascript程序设计有所帮助。