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. PS 怎么去掉图片上的文字

    第一步:打开需要去掉文字的图片. 第二步:在左侧工具栏中选择“吸管工具”. 第三步:在文字附近选取颜色. 第四步:在左侧工具栏中选择“矩形选框工具”,并选中要消除的文字. 第五步:在菜单栏“编辑”中选 ...

  2. vue.js实战——props数据验证(自定义构造器检测)

    Vue.component('my-component',{ props:{ //必须是数字类型 propA:Number, //必须是字符串或数字类型 propB:[String,Number], ...

  3. ORACLE数据闪回

    ALTER TABLE SPM_CON_PAYMENT_RECEIPT ENABLE ROW MOVEMENT;   -- 表名 FLASHBACK TABLE SPM_CON_PAYMENT_REC ...

  4. [BZOJ 1095] [ZJOI 2007] 捉迷藏

    Description 传送门 Solution 先将原树转化成点分树: 然后维护三个堆: \(c[i]\) 保存点分树中子树 \(i\) 中的黑色节点到 \(fa[i]\) 的距离: \(b[i]\ ...

  5. XMLHttpRequest的使用

    XMLHttpRequest的使用 标签(空格分隔): JavaScript 前端 编程 function sendAjax() { //构造表单数据 var formData = new FormD ...

  6. css:a:visited限制

    :active 对于:active伪类可以在div上生效.没有限制 :visited使用限制 :visited只适用于带href的a标签.如果给a标签绑定了click事件,那跳转的url必须跟href ...

  7. CF271D 【Good Substrings】

    定义哈希函数 \(H(c)=\sum_{i = 1} ^ m c_i*b^{m-i}\) \(H(C,K+1)=H(C,K)*b+C_{K+1}\)(K个坏字母) 用long long #includ ...

  8. 超越村后端开发(3:安装djangorestframework+序列化+API开发前期准备)

    1.安装djangorestframework 1.安装djangorestframework及其依赖包markdown.django-filter. pip install djangorestfr ...

  9. Apache的安装与配置

    apahe官网 http://www.apache.org/ 安装及配置https://blog.csdn.net/liyang4534/article/details/78036591 常见问题的处 ...

  10. 将图片转为base64

    DEMO: <input type="file" id="file" multiple="multiple"> <div ...