数据结构中,二叉树的使用频率非常高,这得益于二叉树优秀的性能。

二叉树是非线性的数据结构,用以存储带有层级的数据,其用于查找的删除的性能非常高。

二叉树 数据结构的实现方法如下:

function Node (data, left, right) {
this.data = data;
this.left = left;
this.right = right;
this.show = function () {
return this.data;
};
} function BST () {
this.root = null;
this.insert = function (data) {
var node = new Node(data, null, null);
if (this.root === null) {
this.root = node;
} else {
var current = this.root;
var parent;
while (true) {
parent = current;
if (data < current.data) {
current = current.left;
if (current === null) {
parent.left = node;
break;
}
} else {
current = current.right;
if(current === null) {
parent.right = node;
break;
}
}
}
}
};
// 中序遍历
this.inOrder = function (node) {
if (node !== null) {
this.inOrder(node.left);
console.log(node.show());
this.inOrder(node.right);
}
};
// 先序遍历
this.preOrder = function (node) {
if (node !== null) {
console.log(node.show());
this.preOrder(node.left);
this.preOrder(node.right);
}
};
// 后序遍历
this.afterOrder = function (node) {
if (node !== null) {
this.afterOrder(node.left);
this.afterOrder(node.right);
console.log(node.show());
}
}; this.getMin = function () {
var current = this.root;
while (current.left !== null) {
current = current.left;
}
return current.data;
}; this.getMax = function () {
var current = this.root;
while (current.right !== null) {
current = current.right;
}
return current.data;
}; this.find = function (data) {
var current = this.root;
while (current !== null) {
if (data < current.data) {
current = current.left;
} else if (data > current.data) {
current = current.right;
} else {
return current;
}
}
return null;
}; this.remove = function (data) {
this.root = this._removeNode(this.root, data); //将根节点转换
}; this._getSmallest = function (node) {
while(node.left!=null){
node=node.left;
}
return node;
}; this._removeNode = function (node, data) {
if (node === null) {
return null;
}
if (data === node.data) {
// 如果没有子节点
if (node.right === null && node.left === null) {
return null;
}
// 如果没有左子节点
if (node.left === null) {
return node.right;//直接指向其右节点
}
// 如果没有右子节点
if (node.right === null) {
return node.left;
}
// 如果有两个节点
if (node.right !== null && node.left !== null) {
var tempNode = this._getSmallest(node.right); // 找到最小的右节点
node.data = tempNode.data;
node.right = this._removeNode(node.right, tempNode.data); // 依次寻找
return node;
}
} else if (data < node.data){
node.left = this._removeNode(node.left, data);
return node;
} else {
node.right = this._removeNode(node.right, data);
return node;
}
};
}

二叉树  数据结构的使用方法如下:

var bst = new BST ();
bst.insert(40);
bst.insert(20);
bst.insert(70);
bst.insert(60);
bst.insert(75);
bst.insert(71);
bst.insert(73); bst.inOrder(bst.root);
bst.remove(70);
console.log("----------------");
bst.inOrder(bst.root);
console.log("----------------");
console.log(bst.find(73))

js数据结构之二叉树的详细实现方法的更多相关文章

  1. js数据结构之集合的详细实现方法

    数据结构中的集合,类似于数学中常说的集合,是一类数据的群组.集合与集合之间还存在交集,并集,补集的运算. ***集合为无序,集合内元素不重复 ***js的set基于数组, 使用SetClass为类名, ...

  2. js数据结构之列表的详细实现方法

    * 列表用于存放数据量较少的数据结构* 当数据量较大时,不需要对其进行查找.排序的情况下,使用列表也比较方便. 本数据结构在node环境下运行,需要对node有个基本是了解. 1. listSize: ...

  3. 如何发布一个自定义Node.js模块到NPM(详细步骤,附Git使用方法)

    咱们闲话不多说,直接开始! 由于我从没有使用过MAC,所以我不保证本文中介绍的操作与MAC一致. 文章开始我先假定各位已经在window全局安装了Node.js,下面开始进行详细步骤介绍: 本文本着, ...

  4. 如何发布一个自定义Node.js模块到NPM(详细步骤)

    咱们闲话不多说,直接开始! 由于我从没有使用过MAC,所以我不保证本文中介绍的操作与MAC一致. 文章开始我先假定各位已经在window全局安装了Node.js,下面开始进行详细步骤介绍: 本文本着, ...

  5. jquery jtemplates.js模板渲染引擎的详细用法第三篇

    jquery jtemplates.js模板渲染引擎的详细用法第三篇 <span style="font-family:Microsoft YaHei;font-size:14px;& ...

  6. jquery jtemplates.js模板渲染引擎的详细用法第二篇

    jquery jtemplates.js模板渲染引擎的详细用法第二篇 关于jtemplates.js的用法在第一篇中已经讲过了,这里就直接上代码,不同之处是绑定模板的方式,这里讲模板的数据专门写一个t ...

  7. jquery jtemplates.js模板渲染引擎的详细用法第一篇

    jquery jtemplates.js模板渲染引擎的详细用法第一篇 Author:ching Date:2016-06-29 jTemplates是一个基于JQuery的模板引擎插件,功能强大,有了 ...

  8. js刷新页面有哪几种方法

    js刷新页面有哪几种方法 一.总结 一句话总结:location属性的reload方法即可:document.location.reload() 1.页面刷新有哪常见的8种方法? 1,history. ...

  9. JS数据结构第三篇---双向链表和循环链表之约瑟夫问题

    一.双向链表 在上文<JS数据结构第二篇---链表>中描述的是单向链表.单向链表是指每个节点都存有指向下一个节点的地址,双向链表则是在单向链表的基础上,给每个节点增加一个指向上一个节点的地 ...

随机推荐

  1. Linux下锁定账号,禁止登录系统的设置总结【转】

    在我们运维工作中,会经常要求一些用户不允许登陆系统,以加固系统安全.今天这里介绍下锁定账号登陆的几种方法: (推荐使用)这种方式会更加人性化一点,因为不仅可以禁止用户登录,还可以在禁用登陆时给提示告诉 ...

  2. python3爬虫中文乱码之请求头‘Accept-Encoding’:br 的问题

    当用python3做爬虫的时候,一些网站为了防爬虫会设置一些检查机制,这时我们就需要添加请求头,伪装成浏览器正常访问. header的内容在浏览器的开发者工具中便可看到,将这些信息添加到我们的爬虫代码 ...

  3. jQuery 实现添加表格行,删除行,调用日期控件

    $(function () { getdatepicker(); getdatetimepicker(); }); $(document).on('click','#addTable',addTr); ...

  4. python各种post上传文件

    1.带证书上传文件 filename = '/tmp/test.cert'hash_v = 'assumethisisahash' #这是一种流式上传的方式with open(filename, 'r ...

  5. 026_关于shell中的特殊变量$0 $n $* $@ $! $?

    一. $n:获取当前执行的shell脚本的第N个参数,n=1..9,当n为0时表示脚本的文件名,如果n大于9,用大括号括起来like${10}. $*:获取当前shell的所有参数,将所有的命令行参数 ...

  6. shell脚本中冒号

    格式:: your comment here 格式:# your comment here 写代码注释(单行注释). 例如: 格式:: 'comment line1 comment line2 mor ...

  7. [学习笔记]JS 数组Array push相关问题

    前言: 今天用写了一个二维数组,都赋值为零,然后更新其中一个值,结果和预期是不一样,会整列的相同位置都是同一个值. 1.用Chrome的控制台样例如下: arrs[2][2] =1的赋值,竟然是三个数 ...

  8. 服务发现之consul的介绍、部署和使用

    什么是服务发现 微服务的框架体系中,服务发现是不能不提的一个模块.我相信了解或者熟悉微服务的童鞋应该都知道它的重要性.这里我只是简单的提一下,毕竟这不是我们的重点.我们看下面的一幅图片:     图中 ...

  9. Android Menu用法全面讲解

    说明:本文只介绍Android3.0及以上的Menu知识点. 菜单的分类 菜单是Android应用中非常重要且常见的组成部分,主要可以分为三类:选项菜单.上下文菜单/上下文操作模式以及弹出菜单.它们的 ...

  10. 基于MFC的ActiveX控件开发教程------------浏览器插件之ActiveX开发

    浏览器插件之ActiveX开发(一) 一般的Web应用对于浏览器插件能不使用的建议尽量不使用,因为其涉及到安全问题以及影响用户安装(或自动下载注册安装)体验问题.在有特殊需求(如涉及数据安全的金融业务 ...