function BinarySearchTree(){
var cnodes = function(key){
this.key = key;
this.left = null;
this.right = null;
} var root = null; this.insert = function(key){
var nodes = new cnodes(key);
if(root === null){
root = nodes;
}else{
insertNode(root,nodes);
}
} function insertNode(rnode,newnode){
if(newnode.key < rnode.key){
if(rnode.left === null){
rnode.left = newnode;
}else{
insertNode(rnode.left,newnode);
}
}else{
if(rnode.right === null){
rnode.right = newnode;
}else{
insertNode(rnode.right , newnode );
}
}
} //中序遍历
this.inOrderTraverse = function(callback){
inOrderTraverseNode(root, callback);
};
function inOrderTraverseNode(cnode,callback){
if(cnode !== null){
inOrderTraverseNode(cnode.left,callback );
callback(cnode.key);
inOrderTraverseNode(cnode.right,callback );
}
} //先序遍历
this.preOrderTraverse = function(callback){
preOrderTraverseNode(root, callback);
};
var preOrderTraverseNode = function (node, callback) {
if (node !== null) {
callback(node.key);
preOrderTraverseNode(node.left, callback);
preOrderTraverseNode(node.right, callback);
}
}; //后序遍历
this.postOrderTraverse = function(callback){
postOrderTraverseNode(root, callback);
};
function postOrderTraverseNode(cnode, callback){
if(cnode !== null){
postOrderTraverseNode(cnode.left, callback);
postOrderTraverseNode(cnode.right, callback);
callback(cnode.key);
}
} //搜索最小值
this.min = function(){
return minNode(root);
}; function minNode(cnode){
if(cnode){
while(cnode && cnode.left !== null){
cnode = cnode.left;
}
return cnode.key;
}
return null;
} this.max = function(){
return maxNode(root);
}
function maxNode(cnode){
if(cnode){
while(cnode && cnode.right !== null){
cnode = cnode.right;
}
return cnode.key;
}
return null;
} this.search = function(key){
return searchNode(root,key);
}; function searchNode(cnode,key){
if(cnode === null){
return false;
} if(key < cnode.key){
return searchNode(cnode.left,key);
}else if(key > cnode.key){
return searchNode(cnode.right,key);
}else{
return true;
}
} //删除
this.remove = function(key){
root = removeNode(root,key);
}; function removeNode(cnode,key){
if(cnode == null){
return null;
}
if(key < cnode.key){
cnode.left = removeNode(cnode.left , key);
return cnode;
}else if(key > cnode.key){
cnode.right = removeNode(cnode.right , key);
return cnode;
}else{
//等于的时候
//第一种情况,一个叶节点
if(cnode.left === null && cnode.right === null){
cnode = null;
return cnode;
} //第二种情况,一个子节点
if(cnode.left === null){
cnode = cnode.right;
return cnode;
}else if(cnode.right === null){
cnode = cnode.left;
return cnode;
}
//第三种情况,两个子节点
//1找到要删除的节点
//2找到该节点,右侧子树中的最小节点,替换自己
//3删掉右侧子树中的最小节点
var aux = findMinNode(cnode.right);
cnode.key = aux.key;
cnode.right = removeNode(cnode.right,aux.key);
return cnode;
}
}
var findMinNode = function(node){
//右侧子树中最小节点的键去更新这个节点的值
while (node && node.left !== null) {
node = node.left;
}
return node;
};
} var tree = new BinarySearchTree();
tree.insert(11);
tree.insert(7);
tree.insert(15);
tree.insert(5);
tree.insert(3);
tree.insert(9);
tree.insert(8);
tree.insert(10);
tree.insert(13);
tree.insert(12);
tree.insert(14);
tree.insert(20);
tree.insert(18);
tree.insert(25);
tree.insert(6); tree.inOrderTraverse(function(key){
console.log(key);
});
tree.remove(15);
console.log("-----------");
tree.inOrderTraverse(function(key){
console.log(key);
});

  

js树形结构-----(BST)二叉树增删查的更多相关文章

  1. node.js+express+mongoose实现用户增删查改案例

    node.js+express+mongodb对用户进行增删查改 一.用到的相关技术 使用 Node.js 的 express 框架搭建web服务 使用 express 中间件 body-parse ...

  2. JS 树形结构与数组结构相互转换、在树形结构中查找对象

    总是有很多需求是关于处理树形结构的,所以不得不总结几个常见操作的写法.¯\_(ツ)_/¯ 首先假设有一个树形结构数据如下 var tree=[ { 'id': '1', 'name': '教学素材管理 ...

  3. js实现对数据库的增删查改

    1.查询 复制代码 代码如下: <HTML> <HEAD> <TITLE>数据查询</TITLE> <Script > var conn = ...

  4. Java创建树形结构算法实例

    在JavaWeb的相关开发中经常会涉及到多级菜单的展示,为了方便菜单的管理需要使用数据库进行支持,本例采用相关算法讲数据库中的条形记录进行相关组装和排序讲菜单组装成树形结构. 首先是需要的JavaBe ...

  5. node.js+mysql增删查改

    数据库和表: -- -- 数据库: `test` -- -- -------------------------------------------------------- -- -- 表的结构 ` ...

  6. Atitit 常见的树形结构 红黑树  二叉树   B树 B+树  Trie树 attilax理解与总结

    Atitit 常见的树形结构 红黑树  二叉树   B树 B+树  Trie树 attilax理解与总结 1.1. 树形结构-- 一对多的关系1 1.2. 树的相关术语: 1 1.3. 常见的树形结构 ...

  7. 使用ztree.js,受益一生,十分钟学会使用tree树形结构插件

    看到ztree.js,这几个字眼,毋庸置疑,那肯定就是tree树形结构了,曾经的swing年代有jtree,后来jquery年代有jstree和treeview,虽然我没写过,但是我见过,一些小功能做 ...

  8. CSS实现树形结构 + js加载数据

    看到一款树形结构,比较喜欢它的样式,就参照它的外观自己做了一个,练习一下CSS. 做出来的效果如下: li { position: relative; padding: 5px 0; margin:0 ...

  9. js中数组增删查改unshift、push、pop、shift、slice、indexOf、concat、join

    js中数组增删查改unshift.push.pop.shift.slice.indexOf.concat.join

随机推荐

  1. Thinkphp框架 表单自动验证登录注册 ajax自动验证登录注册

    说明:这里没练习静态自动验证:如果用到静态验证首先自定义一个控制器,再在Model文件夹里创建一个NiHaoModel.php 类  NiHao是自定义的,前缀可以随意,但是一定要用驼峰法(首字母大写 ...

  2. 没有上司的舞会|codevs1380|luoguP1352|树形DP|Elena

    没有上司的舞会  时间限制: 1 s  空间限制: 128000 KB  题目等级 : 钻石 Diamond   题目描述 Description Ural大学有N个职员,编号为1~N.他们有从属关系 ...

  3. day7:set和深浅copy

    1,判断字符串是不是空格isspace函数 s1 = ' ' s2 = ' ssss' print(s1.isspace()) print(s2.isspace()) 运行结果: True False ...

  4. Canonical form

    https://en.wikipedia.org/wiki/Canonical_form#Linear_algebra Suppose we have some set S of objects, w ...

  5. PHP之PSR

    PHP的PSR (PSR 称为PHP Standard Recommendations) PSR参考网址:http://www.php-fig.org/psr 在PHP中,有5个编码标准分类: ①.P ...

  6. delphi传递变量给fastreport

    delphi传递变量给fastreport   1.打开frReport报表设计.2.打开file->data dictionary加变量.这里比如加title,bm,zbr,gj,zrs3.在 ...

  7. Python字符串拼接的6种方法

    如有其他字符串拼接方法 欢迎留言提出哦 (示例版本为Py2) 1. 加号 第一种,有编程经验的人,估计都知道很多语言里面是用加号连接两个字符串,Python里面也是如此直接用 “+” 来连接两个字符串 ...

  8. ef codefirst 模型字段类型与sqlserver表字段类型对应概要

    1.sqlserver中nvarchar(50),对应EF中: [MaxLength(, ErrorMessage = "最大长度为{1}")] public string Nam ...

  9. es6 学习四 数组的学习

    1. Array.from() 语法: Array.from(arrayLike[, mapFn[, thisArg]]) arrayLike 类数组对象 mapFn 如果指定了该参数,新数组中的每个 ...

  10. 同步fifo的Verilog实现

    FIFO是一种先进先出的数据缓存器,他与普通存储器相比: 优点:没有外部读写地址线,这样使用起来非常简单: 缺点:只能顺序写入数据,顺序的读出数据, 其数据地址由内部读写指针自动加1完成,不能像普通存 ...