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

  1. function Node(data,left,right) {
  2. this.data = data;
  3. this.left = left;
  4. this.right = right;
  5. this.show = show;
  6. }
  7.  
  8. function show() {
  9. return this.data;
  10. }
  11.  
  12. function BST() {
  13. this.root = null;
  14. this.insert = insert;
  15. this.inOrder = inOrder;
  16. this.getMin = getMin;
  17. this.getMax = getMax;
  18. this.find = find;
  19. this.remove = remove;
  20. }
  21.  
  22. function insert(data) {
  23. var n = new Node(data,null,null);
  24. if(this.root == null) {
  25. this.root = n;
  26. }else {
  27. var current = this.root;
  28. var parent;
  29. while(current) {
  30. parent = current;
  31. if(data < current.data) {
  32. current = current.left;
  33. if(current == null) {
  34. parent.left = n;
  35. break;
  36. }
  37. }else {
  38. current = current.right;
  39. if(current == null) {
  40. parent.right = n;
  41. break;
  42. }
  43. }
  44. }
  45. }
  46. }
  47. // 中序遍历
  48. function inOrder(node) {
  49. if(!(node == null)) {
  50. inOrder(node.left);
  51. console.log(node.show());
  52. inOrder(node.right);
  53. }
  54. }
  55.  
  56. // 先序遍历
  57. function preOrder(node) {
  58. if(!(node == null)) {
  59. console.log(node.show());
  60. preOrder(node.left);
  61. preOrder(node.right);
  62. }
  63. }
  64.  
  65. // 后序遍历
  66. function postOrder(node) {
  67. if(!(node == null)) {
  68. postOrder(node.left);
  69. postOrder(node.right);
  70. console.log("后序遍历"+node.show());
  71. }
  72. }
  73.  
  74. // 二叉树查找最小值
  75. function getMin(){
  76. var current = this.root;
  77. while(!(current.left == null)) {
  78. current = current.left;
  79. }
  80. return current.data;
  81. }
  82.  
  83. // 二叉树上查找最大值
  84. function getMax() {
  85. var current = this.root;
  86. while(!(current.right == null)) {
  87. current = current.right;
  88. }
  89. return current.data;
  90. }
  91.  
  92. // 查找给定值
  93. function find(data) {
  94. var current = this.root;
  95. while(current != null) {
  96. if(current.data == data) {
  97. return current;
  98. }else if(data < current.data) {
  99. current = current.left;
  100. }else {
  101. current = current.right;
  102. }
  103. }
  104. return null;
  105. }
  106.  
  107. function remove(data) {
  108. root = removeNode(this.root,data);
  109. }
  110. function getSmallest(node) {
  111. if (node.left == null) {
  112. return node;
  113. }
  114. else {
  115. return getSmallest(node.left);
  116. }
  117. }
  118. function removeNode(node,data) {
  119. if(node == null) {
  120. return null;
  121. }
  122. if(data == node.data) {
  123. // 没有子节点的节点
  124. if(node.left == null && node.right == null) {
  125. return null;
  126. }
  127. // 没有左子节点的节点
  128. if(node.left == null) {
  129. return node.right;
  130. }
  131. // 没有右子节点的节点
  132. if(node.right == null) {
  133. return node.left;
  134. }
  135. // 有2个子节点的节点
  136. var tempNode = getSmallest(node.right);
  137. node.data = tempNode.data;
  138. node.right = removeNode(node.right,tempNode.data);
  139. return node;
  140. }else if(data < node.data) {
  141. node.left = removeNode(node.left,data);
  142. return node;
  143. }else {
  144. node.right = removeNode(node.right,data);
  145. return node;
  146. }
  147. }
  148. 代码初始化如下:
  149. var nums = new BST();
  150. nums.insert(23);
  151. nums.insert(45);
  152. nums.insert(16);
  153. nums.insert(37);
  154. nums.insert(3);
  155. nums.insert(99);
  156. nums.insert(22);
  157. var min = nums.getMin();
  158. console.log(min);
  159. var max = nums.getMax();
  160. console.log(max);
  161. var value = nums.find("45");
  162. console.log(value);
  163. nums.remove(23);

javascript数据结构与算法---二叉树(删除节点)的更多相关文章

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

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

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

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

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

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

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

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

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

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

  6. javascript数据结构与算法--二叉树(插入节点、生成二叉树)

    javascript数据结构与算法-- 插入节点.生成二叉树 二叉树中,相对较小的值保存在左节点上,较大的值保存在右节点中 /* *二叉树中,相对较小的值保存在左节点上,较大的值保存在右节点中 * * ...

  7. 为什么我要放弃javaScript数据结构与算法(第八章)—— 树

    之前介绍了一些顺序数据结构,介绍的第一个非顺序数据结构是散列表.本章才会学习另一种非顺序数据结构--树,它对于存储需要快速寻找的数据非常有用. 本章内容 树的相关术语 创建树数据结构 树的遍历 添加和 ...

  8. JavaScript 数据结构与算法之美 - 线性表(数组、栈、队列、链表)

    前言 基础知识就像是一座大楼的地基,它决定了我们的技术高度. 我们应该多掌握一些可移值的技术或者再过十几年应该都不会过时的技术,数据结构与算法就是其中之一. 栈.队列.链表.堆 是数据结构与算法中的基 ...

  9. JavaScript 数据结构与算法之美 - 非线性表中的树、堆是干嘛用的 ?其数据结构是怎样的 ?

    1. 前言 想学好前端,先练好内功,内功不行,就算招式练的再花哨,终究成不了高手. 非线性表(树.堆),可以说是前端程序员的内功,要知其然,知其所以然. 笔者写的 JavaScript 数据结构与算法 ...

随机推荐

  1. Le Chapitre IV

    J'avais ainsi appris une seconde chose très importante: C'est que sa planète d'origine était à peine ...

  2. Gibs抽样

    /* * Copyright (C) 2007 by * * Xuan-Hieu Phan * hieuxuan@ecei.tohoku.ac.jp or pxhieu@gmail.com * Gra ...

  3. 关于内存类型 UDIMM、RDIMM、LRDIMM 的学习结论(转)

    随着内存技术不断发展,服务器上内存的容量.密度和速度也越来越高.目前在市场上出现的内存条最高密度可以做到每条内存条 4 个 Rank,容量达到 32GB/条,最高速度达到 1.6GHz.高密度高频率也 ...

  4. VSS + Eclipse 管理源码

  5. 一个发送邮件的java类,包含多种发送方法

    import java.util.Calendar;import java.util.Date; import java.util.Properties; import javax.mail.Addr ...

  6. spring之jdbcTemplate

    spring的另一个功能模块data access对于数据库的支持 spring data access第一个helloword案例: 使用java程序实现访问配置 1.导包 2.测试案例 @Test ...

  7. Using Spring.net in console application

    Download Spring.net in http://www.springframework.net/ Install Spring.NET.exe Create a console appli ...

  8. Codeforces816B Karen and Coffee 2017-06-27 15:18 39人阅读 评论(0) 收藏

    B. Karen and Coffee time limit per test 2.5 seconds memory limit per test 512 megabytes input standa ...

  9. hdu 4135 [a,b]中n互质数个数+容斥

    http://acm.hdu.edu.cn/showproblem.php?pid=4135 给定一个数n,求某个区间[a,b]内有多少数与这个数互质. 对于一个给定的区间,我们如果能够求出这个区间内 ...

  10. Vue的配置

    一.build:打包的配置文件的文件夹 1.build.js  生产版本的配置文件,一般这个文件我们是不改的 'use strict' //调用检查版本的文件,check-versions的导出直接是 ...