js树形结构-----二叉树增删查

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);
		});

  

原文地址:https://www.cnblogs.com/muamaker/p/9204258.html

时间: 2024-08-06 01:33:35

js树形结构-----二叉树增删查的相关文章

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

node.js+express+mongodb对用户进行增删查改 一.用到的相关技术 使用 Node.js 的 express 框架搭建web服务 使用 express 中间件 body-parse 解析表单 post 请求体 使用 art-template 模板引擎渲染页面 使用第三方包 mongoose 来操作 MongoDB 数据库 二.在命令行用 npm 执行相关的命令 初始化项目,在命令行执行 npm init 然后一路回车就行了(或者直接 npm init -y)生成 package

Atitit 常见的树形结构 红黑树 &#160;二叉树 &#160;&#160;B树 B+树 &#160;Trie树&#160;attilax理解与总结

Atitit 常见的树形结构 红黑树  二叉树   B树 B+树  Trie树 attilax理解与总结 1.1. 树形结构-- 一对多的关系1 1.2. 树的相关术语: 1 1.3. 常见的树形结构 红黑树  二叉树   B树 B+树  Trie树2 1.4. 满二叉树和完全二叉树..完全二叉树说明深度达到完全了.2 1.5. 属的逻辑表示 树形比奥死,括号表示,文氏图,凹镜法表示3 1.6. 二叉树是数据结构中一种重要的数据结构,也是树表家族最为基础的结构.3 1.6.1. 3.2 平衡二叉

【JS 设计模式 】用组合模式来实现树形导航--JS代码结构思路分析(二)

[JS 设计模式 ]用组合模式来实现树形导航--代码结构思路分析(一) 根据上一节中的HTML代码结构我们通过JS来渲染HTML代码,我们先提供一下JS的代码片段,这代码代码不是一个完整的代码是经过简化的.通过JS代码来分析如何组装HTML的 Composite类型的代码: function TreeComposite(id, name, total, level, last) { var root = document.createDocumentFragment(); var panel =

利用多叉树实现Ext JS中的无限级树形菜单(一种构建多级有序树形结构JSON的方法)

一.问题研究的背景和意义 目前在Web应用程序开发领域,Ext JS框架已经逐渐被广泛使用,它是富客户端开发中出类拔萃的框架之一.在Ext的UI控件中,树形控件无疑是最为常用的控件之一,它用来实现树形结构的菜单.TreeNode用来实现静态的树形菜单,AsyncTreeNode用来实现动态的异步加载树形菜单,后者最为常用,它通过接收服务器端返回来的JSON格式的数据,动态生成树形菜单节点.动态生成树有两种思路:一种是一次性生成全部树节点,另一种是逐级加载树节点(利用AJAX,每次点击节点时查询下

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

看到一款树形结构,比较喜欢它的样式,就参照它的外观自己做了一个,练习一下CSS. 做出来的效果如下: ul,li { list-style-type: none } .tree { display: block; position: relative; padding: 5px 15px } .tree span { display: inline-block; height: 30px; line-height: 28px; min-width: 60px; text-align: cente

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

js中数组增删查改unshift.push.pop.shift.slice.indexOf.concat.join 原文地址:https://www.cnblogs.com/mahmud/p/10301590.html

SpringMVC+hibernate整合小例子,用户的增删查改

SpringMVC+hibernate整合小例子,用户的增删查改 对于使用框架写项目,我的第一反应的把所有需要的jar文件引入. 因为我用的是JDK1.8,当使用spring3.2 注解的时候会出现问题,所以最终使用的spring4.0.  hibernate使用的版本为4.0 .至于一些依赖包的版本就是看别人的资料拿的. 然后看下整体的项目结构 第一步写的是web.xml配置文件,当然有写东西是后面加上去的,不是一步到位的,比如说控制post方式的乱码.以及无法时候JS文件等一些静态文件,后面

jQuery+zTree加载树形结构菜单

jQuery+zTree加载树形结构菜单 由于项目中需要设计树形菜单功能,经过一番捣腾之后,终于给弄出来了,所以便记下来,也算是学习zTree的一个总结吧. zTree的介绍: 1.zTree 是利用 JQuery 的核心代码,实现一套能完成大部分常用功能的 Tree 插件 2.zTree v3.0 将核心代码按照功能进行了分割,不需要的代码可以不用加载 3.采用了 延迟加载 技术,上万节点轻松加载,即使在 IE6 下也能基本做到秒杀 4.兼容 IE.FireFox.Chrome.Opera.S

5.在MVC中使用泛型仓储模式和工作单元来进行增删查改

原文链接:http://www.c-sharpcorner.com/UploadFile/3d39b4/crud-operations-using-the-generic-repository-pattern-and-uni/ 系列目录: Relationship in Entity Framework Using Code First Approach With Fluent API[[使用EF Code-First方式和Fluent API来探讨EF中的关系]] Code First Mig