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. Workbox 缓存

    介绍 https://developers.google.cn/web/tools/workbox/guides/get-started 先注册一个service worker <script& ...

  2. pycharm更新之后显示问题

    pycharm更新之后显示问题 在新版pycharm中等号和其他符号会连在一块,下面是解决方法 添加公众号:

  3. 在 .NET Framework Data Provider for Microsoft SQL Server Compact 3.5 中发生错误

    32位机器删除 HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\VisualStudio\version\DataProviders\{7C602B5B-ACCB-4acd ...

  4. Nordic NRF51822 从零开始系列(一)开发环境的搭建

    硬件准备     (1)nrf51822 开发板一块(此处使用的是青云系列的,自带jlijnk ob+usb串口芯片)或者使用nrf51822模块+jlink_ob                 ( ...

  5. 如何迁移完整SQL数据库到另外一台服务器

    如何迁移完整SQL数据库到另外一台服务器: https://jingyan.baidu.com/album/9f7e7ec080d1b36f28155422.html?picindex=1

  6. ubuntu下hadoop0.20.2报错/dfs/name is in an inconsistent state

    Hadoop0.20.2在关机重启后,namenode启动报错: 用bin/hadoop namenode -format重新格式化一下就好了.这个问题已经出现了两次.每次都格式化,显然不是一个专业的 ...

  7. xss 学习记录

    反射型和持久型 一些简单的xss例子: <script>alert(/xss/)</script> 嵌入到textarea的,需要先关闭textarea标签 </text ...

  8. Concurrent Execution

    Concurrent Execution — Python 3.7.2 documentation https://docs.python.org/3/library/concurrency.html

  9. static的含义

    static的含义:(1)设置变量的存储域,函数体内static变量的作用范围为该函数体,不同于auto变量,该变量的内存只被分配一次,因此其值在下次调用时仍持上次的值:(2)限制变量的作用域,在模块 ...

  10. [python2] python 打印表格 prettytable

    rpm包: [root@D129 cli]# yum info python-prettytable Loaded plugins: fastestmirror Loading mirror spee ...