原理:

叉排序树的查找过程和次优二叉树类似,通常采取二叉链表作为二叉排序树存储结构中序遍历二叉排序树可得到一个关键字的有序序列,一个无序序列可以通过构造一棵二叉排序树变成一个有序序列,构造树的过程即为对无序序列进行排序的过程。每次插入的新的结点都是二叉排序树上新的叶子结点,在进行插入操作时,不必移动其它结点,只需改动某个结点的指针,由空变为非空即可。搜索,插入,删除的复杂度等于树高,O(log(n)).

JavaScript实现:

        var BinarySearchTree = function(){
this.root = null; } BinarySearchTree.prototype = {
insert: function(key){//插入
var newNode = new this.Node(key);
if(this.root === null){
this.root = newNode;
}else{
this.insertNode(this.root, newNode)
}
console.log(this.root)
},
inOrderTraverse: function(callback){//中序查找
this.inOrderTraverseNode(this.root, callback);
},
preOrderTraverse: function(callback){//先序查找
this.preOrderTraverseNode(this.root, callback);
},
postOrderTraverse: function(callback){//后序查找
this.postOrderTraverseNode(this.root, callback);
},
min: function(){//最小值
return this.minNode(this.root)
},
max: function(){//最大值
return this.maxNode(this.root)
},
search: function(key){//查找
this.searchNode(this.root, key)
},
remove: function(key){//移除树节点
this.removeNode(this.root, key)
}, Node: function(key){
this.key = key;
this.left = null;
this.right = null;
},
insertNode: function(node, newNode){
if(newNode.key < node.key){
if(node.left === null){
node.left = newNode;
}else{
this.insertNode(node.left, newNode)
}
}else{
if(node.right === null){
node.right = newNode;
}else{
this.insertNode(node.right, newNode)
}
}
},
inOrderTraverseNode: function(node, callback){
if(node !== null){
this.inOrderTraverseNode(node.left, callback);
callback(node.key);
this.inOrderTraverseNode(node.right, callback);
}
},
preOrderTraverseNode: function(node, callback){
if(node !== null){
callback(node.key);
this.preOrderTraverseNode(node.left, callback);
this.preOrderTraverseNode(node.right, callback);
}
},
postOrderTraverseNode: function(node, callback){
if(node !== null){
this.postOrderTraverseNode(node.left, callback);
this.postOrderTraverseNode(node.right, callback);
callback(node.key);
}
},
minNode: function(node){
if(node){
while(node && node.left !== null){
node = node.left;
}
return node.key;
}
return null;
},
maxNode: function(node){
if(node){
while(node && node.right !== null){
node = node.right;
}
return node.key;
}
return null;
},
searchNode: function(node, key){
if(node === null)
return false;
if(key < node.key){
return this.searchNode(node.left, key);
}else if(key > node.key){
return this.searchNode(node.right, key);
}else{
return true;
}
},
removeNode(node, key){
if(node === null)
return null; if(key < node.key){
node.left = this.removeNode(node.left, key);
return node;
}else if(key > node.key){
node.right = this.removeNode(node.right, key);
return node;
}else{
if(node.left === null && node.right === null){
node = null;
return node;
}else if(node.left === null){
node = node.right;
return node;
}else if(node.right === null){
node = node.left;
return node;
} var aux = this.findMinNode(node.right);
node.key = aux.key;
node.right = this.removeNode(node.right, aux.key);
return node;
}
},
findMinNode: function(node){
if(node){
while(node && node.left !== null){
node = node.left;
}
return node.key;
}
return null;
}
}

数据结构之二叉搜索树(BST)--JavaScript实现的更多相关文章

  1. hdu 3791:二叉搜索树(数据结构,二叉搜索树 BST)

    二叉搜索树 Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 32768/32768K (Java/Other) Total Submiss ...

  2. C++版 - 剑指offer 面试题24:二叉搜索树BST的后序遍历序列(的判断) 题解

    剑指offer 面试题24:二叉搜索树的后序遍历序列(的判断) 题目:输入一个整数数组,判断该数组是不是某二叉搜索树的后序遍历的结果.如果是则返回true.否则返回false.假设输入的数组的任意两个 ...

  3. 萌新笔记之二叉搜索树(BST)

    前言,以前搞过线段树,二叉树觉得也就那样= =.然后数据结构的课也没怎么听过,然后下周期中考... 本来以为今天英语考完可以好好搞ACM了,然后这个数据结构期中考感觉会丢人,还是好好学习一波. 二叉搜 ...

  4. 给定一个二叉搜索树(BST),找到树中第 K 小的节点

    问题:给定一个二叉搜索树(BST),找到树中第 K 小的节点. 出题人:阿里巴巴出题专家:文景/阿里云 CDN 资深技术专家. 考察点: 1. 基础数据结构的理解和编码能力 2.  递归使用 参考答案 ...

  5. 数据结构☞二叉搜索树BST

    二叉查找树(Binary Search Tree),(又:二叉搜索树,二叉排序树)它可以是一棵空树,也可以是具有下列性质的二叉树: 若它的左子树不空,则左子树上所有结点的值均小于它的根结点的值: 若它 ...

  6. 数据结构-二叉搜索树(BST binary search tree)

    本文由@呆代待殆原创,转载请注明出处:http://www.cnblogs.com/coffeeSS/ 二叉搜索树简介 顾名思义,二叉搜索树是以一棵二叉树来组织的,这样的一棵树可以用一个链表数据结构来 ...

  7. 数据结构---二叉搜索树BST实现

    1. 二叉查找树 二叉查找树(Binary Search Tree),也称为二叉搜索树.有序二叉树(ordered binary tree)或排序二叉树(sorted binary tree),是指一 ...

  8. 看动画学算法之:二叉搜索树BST

    目录 简介 BST的基本性质 BST的构建 BST的搜索 BST的插入 BST的删除 简介 树是类似于链表的数据结构,和链表的线性结构不同的是,树是具有层次结构的非线性的数据结构. 树是由很多个节点组 ...

  9. 【算法与数据结构】二叉搜索树的Java实现

    为了更加深入了解二叉搜索树,博主自己用Java写了个二叉搜索树,有兴趣的同学可以一起探讨探讨. 首先,二叉搜索树是啥?它有什么用呢? 二叉搜索树, 也称二叉排序树,它的每个节点的数据结构为1个父节点指 ...

  10. 在二叉搜索树(BST)中查找第K个大的结点之非递归实现

    一个被广泛使用的面试题: 给定一个二叉搜索树,请找出其中的第K个大的结点. PS:我第一次在面试的时候被问到这个问题而且让我直接在白纸上写的时候,直接蒙圈了,因为没有刷题准备,所以就会有伤害.(面完的 ...

随机推荐

  1. Debian安装NVIDIA显卡驱动

    1. sudo apt-get install nvidia-detect nvidia-detect 输出信息: Detected NVIDIA GPUs: 01:00.0 VGA compatib ...

  2. SDL2 gif动态图加载

    参照 https://tieba.baidu.com/p/3569073088?tpl=5&red_tag=1777318765 使用mingw工具链 #include <stdbool ...

  3. git安装和第一次提交过程

    1,新建文件夹test,运行命令 git init 2,找到test的.git文件夹,打开之后找到config文件,在最后边加上一句话 [user] email=your email name=you ...

  4. APP——python——自动化环境搭建01

    前提:python以及pycharm安装完成. ---------------------------------------------------------------------------- ...

  5. vs2017离线包下载获取方法

    一.去官网下载所需要的版本的安装包获取程序: https://www.visualstudio.com/zh-hans/downloads/ 三个版本,对应文件名称为: 社区版:vs_Communit ...

  6. tarjan算法求scc & 缩点

    前置知识 图的遍历(dfs) 强连通&强连通分量 对于有向图G中的任意两个顶点u和v存在u->v的一条路径,同时也存在v->u的路径,我们则称这两个顶点强连通.以此类推,强连通分量 ...

  7. 调用php命令出错

    调用php -v命令.php artisan route:list等命令均出现一下错误. MIB search path: c:/usr/share/snmp/mibsCannot find modu ...

  8. ES7.x客户端的认证创建一步一步来

    前言 好久没来写博客了,还是简单的记录一下吧.今天要写的是es在7.x版本后的客户端的创建以及一些es的查询所语句到的小问题.直接先吧客户端端的代码呈上. 正文 public class ESClie ...

  9. Swoole 绑定域名 80 端口 (Nginx 反向代理)

    启动 Swoole 的 http server,可以使用 IP + 端口 进行访问 创建 Nginx 虚拟域名 vim swotp.liuguofeng.com.conf server { liste ...

  10. Pikachu靶场SQL注入刷题记录

    数字型注入 0x01 burp抓包,发送至repeater 后面加and 1=1,and 1=2 可判断存在注入 0x02 通过order by判断字段数,order by 2 和order by 3 ...