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. API(一)之Serialization

    virtualenv is a tool to create isolated Python environments. 建立一个新的环境 Before we do anything else we' ...

  2. ATM JAVA实现 部分代码

    package score;//信1705-2 20173600 王重阳 import java.util.Scanner; public class Main { public static voi ...

  3. 【紫书】Ordering Tasks UVA - 10305 拓扑排序:dfs到底再输出。

    题意:给你一些任务1~n,给你m个数对(u,v)代表做完u才能做v 让你给出一个做完这些任务的合理顺序. 题解:拓扑排序版题 dfs到底再压入栈. #define _CRT_SECURE_NO_WAR ...

  4. ASP.NET MVC 系统过滤器、自定义过滤器

    一.系统过滤器使用说明 1.OutputCache过滤器 OutputCache过滤器用于缓存你查询结果,这样可以提高用户体验,也可以减少查询次数.它有以下属性: Duration:缓存的时间,以秒为 ...

  5. 2015年蓝桥杯省赛A组c++第5题(回溯算法填空)

    /* 1,2,3…9 这九个数字组成一个分数,其值恰好为1/3,如何组法? 下面的程序实现了该功能,请填写划线部分缺失的代码. */ #include <stdio.h> void tes ...

  6. oplog

    参考资料:https://www.cnblogs.com/ruizhang3/p/6539730.html http://www.jb51.net/article/113432.htm :insert ...

  7. Python 常用的日期时间命令

    今天用到自动添加当前时间,居然把之前的知识忘了,特整理常用的日期时间命令 代码: # 获取当前时间# import time# localtime = time.localtime(time.time ...

  8. 注解之@CookieValue

    @RequestHeader以及@CookieValue这两个注解用法类似,属性也相同,所以,写在一起.二者属性和RequestParam的属性一样,用法也几乎一样. 作用 @RequestHeade ...

  9. Advising controllers with the @ControllerAdvice annotation

    The @ControllerAdvice annotation is a component annotation allowing implementation classes to be aut ...

  10. Code once, debug everywhere.

    1.通常语言调用一个函数会出exception的情况,在javascript里面返回的是undefined.等到程序运行不正常的时候,你看到数据结构的有些地方为什么是undefined,只能哭了. 2 ...