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

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

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

  1. function Node (data, left, right) {
  2. this.data = data;
  3. this.left = left;
  4. this.right = right;
  5. this.show = function () {
  6. return this.data;
  7. };
  8. }
  9.  
  10. function BST () {
  11. this.root = null;
  12. this.insert = function (data) {
  13. var node = new Node(data, null, null);
  14. if (this.root === null) {
  15. this.root = node;
  16. } else {
  17. var current = this.root;
  18. var parent;
  19. while (true) {
  20. parent = current;
  21. if (data < current.data) {
  22. current = current.left;
  23. if (current === null) {
  24. parent.left = node;
  25. break;
  26. }
  27. } else {
  28. current = current.right;
  29. if(current === null) {
  30. parent.right = node;
  31. break;
  32. }
  33. }
  34. }
  35. }
  36. };
  37. // 中序遍历
  38. this.inOrder = function (node) {
  39. if (node !== null) {
  40. this.inOrder(node.left);
  41. console.log(node.show());
  42. this.inOrder(node.right);
  43. }
  44. };
  45. // 先序遍历
  46. this.preOrder = function (node) {
  47. if (node !== null) {
  48. console.log(node.show());
  49. this.preOrder(node.left);
  50. this.preOrder(node.right);
  51. }
  52. };
  53. // 后序遍历
  54. this.afterOrder = function (node) {
  55. if (node !== null) {
  56. this.afterOrder(node.left);
  57. this.afterOrder(node.right);
  58. console.log(node.show());
  59. }
  60. };
  61.  
  62. this.getMin = function () {
  63. var current = this.root;
  64. while (current.left !== null) {
  65. current = current.left;
  66. }
  67. return current.data;
  68. };
  69.  
  70. this.getMax = function () {
  71. var current = this.root;
  72. while (current.right !== null) {
  73. current = current.right;
  74. }
  75. return current.data;
  76. };
  77.  
  78. this.find = function (data) {
  79. var current = this.root;
  80. while (current !== null) {
  81. if (data < current.data) {
  82. current = current.left;
  83. } else if (data > current.data) {
  84. current = current.right;
  85. } else {
  86. return current;
  87. }
  88. }
  89. return null;
  90. };
  91.  
  92. this.remove = function (data) {
  93. this.root = this._removeNode(this.root, data); //将根节点转换
  94. };
  95.  
  96. this._getSmallest = function (node) {
  97. while(node.left!=null){
  98. node=node.left;
  99. }
  100. return node;
  101. };
  102.  
  103. this._removeNode = function (node, data) {
  104. if (node === null) {
  105. return null;
  106. }
  107. if (data === node.data) {
  108. // 如果没有子节点
  109. if (node.right === null && node.left === null) {
  110. return null;
  111. }
  112. // 如果没有左子节点
  113. if (node.left === null) {
  114. return node.right;//直接指向其右节点
  115. }
  116. // 如果没有右子节点
  117. if (node.right === null) {
  118. return node.left;
  119. }
  120. // 如果有两个节点
  121. if (node.right !== null && node.left !== null) {
  122. var tempNode = this._getSmallest(node.right); // 找到最小的右节点
  123. node.data = tempNode.data;
  124. node.right = this._removeNode(node.right, tempNode.data); // 依次寻找
  125. return node;
  126. }
  127. } else if (data < node.data){
  128. node.left = this._removeNode(node.left, data);
  129. return node;
  130. } else {
  131. node.right = this._removeNode(node.right, data);
  132. return node;
  133. }
  134. };
  135. }

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

  1. var bst = new BST ();
  2. bst.insert(40);
  3. bst.insert(20);
  4. bst.insert(70);
  5. bst.insert(60);
  6. bst.insert(75);
  7. bst.insert(71);
  8. bst.insert(73);
  9.  
  10. bst.inOrder(bst.root);
  11. bst.remove(70);
  12. console.log("----------------");
  13. bst.inOrder(bst.root);
  14. console.log("----------------");
  15. 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. python - class类 (五) 继承补充-子类继承父类属性/函数方法

    子类继承父类属性/函数方法: #方式一:(原生方式,不建议使用) class Dongwu(object): def __init__(self,name,sex,old): self.name = ...

  2. SpringBoot定制错误的Json数据

    (1)自定义异常处理&返回定制Json数据 @ControllerAdvice public class MyExceptionHandler { @ResponseBody @Excepti ...

  3. linux kernel的cmdline参数解析原理分析【转】

    转自:https://blog.csdn.net/skyflying2012/article/details/41142801 版权声明:本文为博主kerneler辛苦原创,未经允许不得转载. htt ...

  4. eclipse 反编译

    Eclipse Class Decompiler安装此插件,可以编译源代码且调试

  5. java web path

    1,request.getRealPath("/");这个方法已不推荐用 2,在Servlet 里用this.getServletContext().getRealPath(&qu ...

  6. 将本地光盘做成yum源

    环境:vmware 1.将centos6.5光盘挂载在虚拟机上 2.将光盘挂载在/mnt/cdrom目录下 root#  mkdir /mnt/cdrom root # mount /mnt/cdro ...

  7. lamp环境搭建之配置apache与fpm方式的php

    配置apache-2.4.9与fpm方式的php-5.4.26 一.apache.MySQL的安装参考<编译安装lamp环境> http://blog.csdn.net/reblue520 ...

  8. 单点登录SSO+鉴权

    一.单点登录原理 1.登录 2.注销 --------------------------------------------------------------------------------- ...

  9. [How to]HBase集群备份方法--Replication机制

    1.简介 HBase备份的方法在[How to]HBase集群备份方法文章中已经有些介绍,但是这些方法都不是HBase本身的特性在支持,都是通过MR计算框架结合HBase客户端的方式,或者直接拷贝HB ...

  10. Ex 6_14 布料剪裁问题_第八次作业

    子问题定义: 定义p[i][j]为布料宽为i,高为j的最优产出,每次剪下一块布料,剩余布料最多形成三块矩阵面料.每次剪裁会有两种情况,水平切割布料,其次是将布料旋转90度后在切割布料. 递归关系: 初 ...