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数据结构与算法---二叉树(查找最小值、最大值、给定值)的更多相关文章

  1. javascript数据结构与算法-- 二叉树

    javascript数据结构与算法-- 二叉树 树是计算机科学中经常用到的一种数据结构.树是一种非线性的数据结构,以分成的方式存储数据,树被用来存储具有层级关系的数据,比如文件系统的文件,树还被用来存 ...

  2. javascript数据结构与算法---二叉树(删除节点)

    javascript数据结构与算法---二叉树(删除节点) function Node(data,left,right) { this.data = data; this.left = left; t ...

  3. javascript数据结构与算法--二叉树遍历(后序)

    javascript数据结构与算法--二叉树遍历(后序) 后序遍历先访问叶子节点,从左子树到右子树,再到根节点. /* *二叉树中,相对较小的值保存在左节点上,较大的值保存在右节点中 * * * */ ...

  4. javascript数据结构与算法--二叉树遍历(先序)

    javascript数据结构与算法--二叉树遍历(先序) 先序遍历先访问根节点, 然后以同样方式访问左子树和右子树 代码如下: /* *二叉树中,相对较小的值保存在左节点上,较大的值保存在右节点中 * ...

  5. javascript数据结构与算法--二叉树遍历(中序)

    javascript数据结构与算法--二叉树遍历(中序) 中序遍历按照节点上的键值,以升序访问BST上的所有节点 代码如下: /* *二叉树中,相对较小的值保存在左节点上,较大的值保存在右节点中 * ...

  6. javascript数据结构与算法--二叉树(插入节点、生成二叉树)

    javascript数据结构与算法-- 插入节点.生成二叉树 二叉树中,相对较小的值保存在左节点上,较大的值保存在右节点中 /* *二叉树中,相对较小的值保存在左节点上,较大的值保存在右节点中 * * ...

  7. javascript数据结构与算法---检索算法(顺序查找、最大最小值、自组织查询)

    javascript数据结构与算法---检索算法(顺序查找.最大最小值.自组织查询) 一.顺序查找法 /* * 顺序查找法 * * 顺序查找法只要从列表的第一个元素开始循环,然后逐个与要查找的数据进行 ...

  8. javascript数据结构与算法---检索算法(二分查找法、计算重复次数)

    javascript数据结构与算法---检索算法(二分查找法.计算重复次数) /*只需要查找元素是否存在数组,可以先将数组排序,再使用二分查找法*/ function qSort(arr){ if ( ...

  9. javascript数据结构与算法--散列

    一:javascript数据结构与算法--散列  一:什么是哈希表? 哈希表也叫散列表,是根据关键码值(key,value)而直接进行访问的数据结构,它是通过键码值映射到表中一个位置来访问记录的,散列 ...

随机推荐

  1. Codeforces Round #542(Div. 2) CDE 思维场

    C https://codeforces.com/contest/1130/problem/C 题意 给你一个\(n*m\)(n,m<=50)的矩阵,每个格子代表海或者陆地,给出在陆地上的起点终 ...

  2. IntelliJ IDEA 2017版 spring-boot2.0.2 自动配置Condition

    描述: 编译器修改参数      -Dfile.encoding=GBK     -Dstr.encoding=GBK Condition位置: 某一个类或注解存在的时候,装配,否则不装配 相关代码: ...

  3. Calendar 得到前一天当前时间

    @Test public void test(){ //因为Calendar的构造方法是私有的,所以实例化一个Calendar对象用getInstance方法 Calendar calendar = ...

  4. Silverlight中关于ComboBox的各种使用

    前端放置了几个ComboBox的控件. <Grid x:Name="LayoutRoot" Background="White"> <Comb ...

  5. 2.3.1关键字volatile与死循环

    关键字volatile的主要作用是使变量在多个线程间可见. 测试如下 package com.cky.test; /** * Created by edison on 2017/12/9. */ pu ...

  6. linux下如何编写shell脚本

    我对shell脚本的认识,除了执行过同事写的shell 脚本外,其他一无所知,为了让自己强大,我决定自己研究shell脚本,也许在你看来很简答,没必要说这么多废话,但是我希望在我的技术log里记录下来 ...

  7. 【python3+request】python3+requests接口自动化测试框架实例详解教程

    转自:https://my.oschina.net/u/3041656/blog/820023 [python3+request]python3+requests接口自动化测试框架实例详解教程 前段时 ...

  8. stm32f103_高级定时器——输入捕获/输出比较中断+pwm=spwm生成

    ****************************首选我们了解一下它们的功能吧********************************************************** ...

  9. js-随机图片

    <!DOCTYPE html><html lang="en"> <head> <meta charset="UTF-8" ...

  10. SRM472

    这次是rng_58出的题目,思维难度还是相当大的的..很值得一做. 250pt: 题意:盒子里有n 个 potatoes,甲乙两个人,每次只能拿4的幂次方数(1,4,16...),最后不能拿的输.求谁 ...