function BinaryTree(){
var Node = function(key){
this.key = key; //值
this.left = null; //左箭头
this.right = null; //右箭头
}
//根节点
var root = null; var insertNode = function(oldNode,newNode){
if(newNode.key < oldNode.key){
if(oldNode.left === null){
oldNode.left = newNode
}else{
insertNode(oldNode.left,newNode)//递归查找
}
}else{
if(oldNode.right === null){
oldNode.right = newNode
}else{
insertNode(oldNode.right,newNode);
}
}
} //插入节点
this.insert = function(key) {
var newNode = new Node(key);
if(root === null){
root = newNode;
}else{
insertNode(root,newNode)
}
} //中序排列
this.inOrderTraverse = function(callback){
inOrderTraverseNode(root,callback)
} //中序排序辅助函数 var inOrderTraverseNode = function(node,callback){
if(node !== null){
inOrderTraverseNode(node.left,callback);//遍历左节点
callback(node.key);//遍历根节点,中节点
inOrderTraverseNode(node.right,callback);//遍历右节点
}
} //先序遍历,先访问节点本身在遍历左节点,最后遍历右节点 this.preOrderTraverse = function(callback){
preOrderTraverseNode(root,callback);
} var preOrderTraverseNode = (node,callback)=>{
if(node !== null){
callback(node.key);
preOrderTraverseNode(node.left,callback);
preOrderTraverseNode(node.right,callback);
}
} //后序遍历,先访问节点的后代节点,再访问节点本身 this.postOrderTraverse = function(callback){
postOrderTraverseNode(root,callback);
} var postOrderTraverseNode = (node,callback) =>{
if(node !== null){
postOrderTraverseNode(node.left,callback);
postOrderTraverseNode(node.right,callback);
callback(node.key);
}
} //搜索最大值,最小值
this.min = function(){
return minNode(root);
} var minNode = (node)=>{
if(node){
while(node && node.left !== null){
node = node.left;
}
return node.key
}
return null;
} this.max = function(){
return maxNode(root);
} var maxNode = (node) => {
if(node){
while(node && node.right !== null){
node = node.right;
}
return node.key
}
return null;
}
//搜索一个特定的值 this.search = function(key){
return searchNode(root,key);
} var searchNode = (node,key) =>{
if(node === null){
return false;
}
if(key < node.key){
return searchNode(node.left,key)
} else if( key > node.key){
return searchNode(node.right,key);
}else{
return true;
}
} //移除一个节点 this.remove = function(key){
return removeNode(root,key);
} var removeNode = (node,key) =>{
if(node === null){
return false
}
if(key < node.key){
node.left = removeNode(node.left,key);
return node
}else if(key > node.key){
node.right = removeNode(node.right,key);
return node;
}else{
//第一种情况 一个叶子节点
if(node.left === null && node.right === null){
node = null;
return node;
}
//第二种情况 一个只有一个子节点的节点 if(node.left === null){
node = node.right;
return node;
}else if(node.right === null){
node = node.left;
return node;
} //第三种情况 一个有两个子节点的节点
var aux = findMinNode(node.right);
node.key = aux.key;
node.right = removeNode(node.right,aux.key);
return node;
}
} var findMinNode = (node)=>{
if(node){
while(node && node.left !== null){
node = node.left;
}
return node
}
return null;
} } let node = [8,3,10,1,6,14,4,7,13];
var binaryTree = new BinaryTree();
node.forEach((key)=>{
binaryTree.insert(key);
}) var printNode = (val) => {
console.log(val)
}
binaryTree.inOrderTraverse(printNode) //中序遍历
binaryTree.preOrderTraverse(printNode) //先序遍历
binaryTree.postOrderTraverse(printNode) //后序遍历 console.log(binaryTree.min() + ': min')
console.log(binaryTree.max() + ': max')
console.log(binaryTree.search(8) + ': search')
console.log(binaryTree.remove(8) )

js数据结构与算法——二叉树的更多相关文章

  1. JS数据结构与算法 - 二叉树(一)基本算法

    仅供JavaScript刷题参考用. 二叉查找树和平衡二叉树 其它树:满二叉树.完全二叉树.完美二叉树.哈弗曼树.二叉查找树BST.平衡二叉树AVL 了解:红黑树,是一种特殊的二叉树.这种树可以进行高 ...

  2. javascript数据结构与算法-- 二叉树

    javascript数据结构与算法-- 二叉树 树是计算机科学中经常用到的一种数据结构.树是一种非线性的数据结构,以分成的方式存储数据,树被用来存储具有层级关系的数据,比如文件系统的文件,树还被用来存 ...

  3. javascript数据结构与算法---二叉树(删除节点)

    javascript数据结构与算法---二叉树(删除节点) function Node(data,left,right) { this.data = data; this.left = left; t ...

  4. javascript数据结构与算法---二叉树(查找最小值、最大值、给定值)

    javascript数据结构与算法---二叉树(查找最小值.最大值.给定值) function Node(data,left,right) { this.data = data; this.left ...

  5. javascript数据结构与算法--二叉树遍历(后序)

    javascript数据结构与算法--二叉树遍历(后序) 后序遍历先访问叶子节点,从左子树到右子树,再到根节点. /* *二叉树中,相对较小的值保存在左节点上,较大的值保存在右节点中 * * * */ ...

  6. javascript数据结构与算法--二叉树遍历(先序)

    javascript数据结构与算法--二叉树遍历(先序) 先序遍历先访问根节点, 然后以同样方式访问左子树和右子树 代码如下: /* *二叉树中,相对较小的值保存在左节点上,较大的值保存在右节点中 * ...

  7. javascript数据结构与算法--二叉树遍历(中序)

    javascript数据结构与算法--二叉树遍历(中序) 中序遍历按照节点上的键值,以升序访问BST上的所有节点 代码如下: /* *二叉树中,相对较小的值保存在左节点上,较大的值保存在右节点中 * ...

  8. JS数据结构与算法——栈

    JS数据结构与算法--栈 1.栈结构概念 栈(Stack)是一种先进后出(LIFO Last in First out)的线性表,先进栈的将会比后进栈的先出栈. 栈的限制是仅允许在一端进行插入和删除运 ...

  9. JS数据结构与算法-概述

    JS数据结构与算法概述 数据结构: 计算机存储, 组织数据的方式, 就像锅碗瓢盆 算法: 一系列解决问题的清晰指令, 就像食谱 两者关系: 程序 = 数据结构 + 算法 邂逅数据结构与算法 什么是数据 ...

随机推荐

  1. Linux(Ubuntu)使用日记------为程序添加桌面快捷方式

     我们Ubuntu中的所以的程序的快捷方式都放在了/usr/share/applications文件夹下,都是以.desktop结尾的文件.我们可以在这个文件夹下创建我们的快捷方式,然后复制到桌面即可 ...

  2. react-redux的基本用法

    注意:读懂本文需要具备redux基础知识, 注明:本文旨在说明如何在实际项目中快速使用react-redux,限于篇幅,本文对具体的原理并未做分析,请参考redux官网 我一直以为我写了一篇关于rea ...

  3. -bash: fork: Cannot allocate memory 问题的处理

    今天生产机器突然无法登录了,正好有一个用top挂着,但是退出top,执行任何命令都报-bash: fork: Cannot allocate memory,但是查看内存还是有很多空闲,然后在百度上查了 ...

  4. KDJ计算公式

    计算方法编辑KDJ的计算比较复杂,首先要计算周期(n日.n周等)的RSV值,即未成熟随机指标值,然后再计算K值.D值.J值等.以n日KDJ数值的计算为例,其计算公式为n日RSV=(Cn-Ln)/(Hn ...

  5. CF1153D Pigeon d'Or

    Description 给一棵树,每个点是子节点的最大值或最小值,将叶子节点填上整数,使这棵树的根最大. Solution 明显的\(dp\)题,代码很短. 分类讨论如下: 1.如果是叶子节点,\(d ...

  6. 计算指定文件的MD5值

    /// <summary> /// 计算指定文件的MD5值 /// </summary> /// <param name="fileName"> ...

  7. python爬虫学习笔记

    爬虫的分类 1.通用爬虫:通用爬虫是搜索引擎(Baidu.Google.Yahoo等)“抓取系统”的重要组成部分.主要目的是将互联网上的网页下载到本地,形成一个互联网内容的镜像备份. 简单来讲就是尽可 ...

  8. 理解依赖注入,laravel IoC容器

    在看laravel文档的时候,有一个服务容器(IoC)的概念.它是这样介绍的:Laravel 服务容器是一个用于管理类依赖和执行依赖注入的强大工具.依赖注入听上去很花哨,其实质是通过构造函数或者某些情 ...

  9. docker etcd

    etcd是CoreOS团队于2013年6月发起的开源项目,它的目标是构建一个高可用的分布式键值(key-value)数据库,用于配置共享和服务发现 etcd内部采用raft协议作为一致性算法,etcd ...

  10. [源码分析]Java1.8中StringJoiner的使用以及源码分析

    [源码分析]StringJoiner的使用以及源码分析 StringJoiner是Java里1.8新增的类, 或许有一部分人没有接触过. 所以本文将从使用例子入手, 分析StringJoiner的源码 ...