1. function Node(data,left,right) {
  2. this.left=left
  3. this.right=right
  4. this.data=data
  5. }
  6. function Btr() {
  7. this.root = null;
  8. }
  9. // D:根节点 L:左子节点 R:右子节点
  10. Btr.prototype.insert=function (data) { //生成排序的 二叉树
  11. if(this.root==null){
  12. this.root = new Node(data,null,null)
  13. }else {
  14. var current = this.root;
  15. while(true){
  16. if(data<current.data){
  17. if(current.left != null){
  18. current = current.left
  19. }else {
  20. current.left = new Node(data,null,null)
  21. break
  22. }
  23. }else {
  24. if(current.right != null){
  25. current = current.right
  26. }else {
  27. current.right = new Node(data,null,null)
  28. break
  29. }
  30. }
  31. }
  32. }
  33. }
  34. Btr.prototype.CenterScan=function () { //中序 ( LDR)
  35. var list=[],root = this.root,left,right
  36. while (root){
  37. if(root.left){
  38. left = root.left
  39. list.unshift(root)
  40. root.left=null
  41. root = left
  42. }else {
  43. console.log(root.data)
  44. if(root.right){
  45. right = root.right
  46. root.right=null
  47. root = right
  48. }else {
  49. root = list.shift()
  50. }
  51. }
  52. }
  53. }
  54. Btr.prototype.FrontScan=function () { //前序 (DLR)
  55. var list=[],root = this.root,left,right
  56. while (root){
  57. if(root.data) console.log(root.data)
  58. left = root.left
  59. right = root.right
  60. if(left){
  61. root.left=null
  62. root.data=null
  63. list.unshift(root)
  64. root = left
  65. }else if(right){
  66. root = right
  67. }else {
  68. root = list.shift()
  69. }
  70. }
  71. }
  72. Btr.prototype.BackScan=function () { //后序 (LRD)
  73. var list=[],root = this.root,left,right
  74. while (root){
  75. left = root.left
  76. right = root.right
  77. if(left){
  78. root.left=null
  79. list.unshift(root)
  80. root = left
  81. }else {
  82. if(right){
  83. root.right = null
  84. list.unshift(root)
  85. root = right
  86. }else {
  87. console.log(root.data)
  88. root = list.shift()
  89. }
  90. }
  91. }
  92. }
  93. Btr.prototype.Max=function () {
  94. var root = this.root,right
  95. while (root){
  96. right = root.right
  97. if(right){
  98. root = right
  99. }else {
  100. console.log(root.data)
  101. root = null
  102. }
  103. }
  104. }
  105. Btr.prototype.Min=function () {
  106. var root = this.root,left
  107. while (root){
  108. left = root.left
  109. if(left){
  110. root = left
  111. }else {
  112. console.log(root.data)
  113. root = null
  114. }
  115. }
  116. }
  117. var btr = new Btr();
  118. btr.insert(6)
  119. btr.insert(2)
  120. btr.insert(1)
  121. btr.insert(4)
  122. btr.insert(3)
  123. btr.insert(5)
  124. btr.insert(9)
  125. btr.insert(8)
  126. btr.insert(10)
  127. btr.CenterScan()

js二叉树,前序/中序/后序(最大最小值,排序)的更多相关文章

  1. 算法进阶面试题03——构造数组的MaxTree、最大子矩阵的大小、2017京东环形烽火台问题、介绍Morris遍历并实现前序/中序/后序

    接着第二课的内容和带点第三课的内容. (回顾)准备一个栈,从大到小排列,具体参考上一课.... 构造数组的MaxTree [题目] 定义二叉树如下: public class Node{ public ...

  2. (原)neuq oj 1022给定二叉树的前序遍历和后序遍历确定二叉树的个数

    题目描述 众所周知,遍历一棵二叉树就是按某条搜索路径巡访其中每个结点,使得每个结点均被访问一次,而且仅被访问一次.最常使用的有三种遍历的方式: 1.前序遍历:若二叉树为空,则空操作:否则先访问根结点, ...

  3. 前、中、后序遍历随意两种是否能确定一个二叉树?理由? && 栈和队列的特点和区别

    前序和后序不能确定二叉树理由:前序和后序在本质上都是将父节点与子结点进行分离,但并没有指明左子树和右子树的能力,因此得到这两个序列只能明确父子关系,而不能确定一个二叉树. 由二叉树的中序和前序遍历序列 ...

  4. 二叉树 遍历 先序 中序 后序 深度 广度 MD

    Markdown版本笔记 我的GitHub首页 我的博客 我的微信 我的邮箱 MyAndroidBlogs baiqiantao baiqiantao bqt20094 baiqiantao@sina ...

  5. 前序+中序->后序 中序+后序->前序

    前序+中序->后序 #include <bits/stdc++.h> using namespace std; struct node { char elem; node* l; n ...

  6. SDUT OJ 数据结构实验之二叉树八:(中序后序)求二叉树的深度

    数据结构实验之二叉树八:(中序后序)求二叉树的深度 Time Limit: 1000 ms Memory Limit: 65536 KiB Submit Statistic Discuss Probl ...

  7. SDUT 1489 求二叉树的先序遍历 (中序后序还原二叉树)

    求二叉树的先序遍历 Time Limit: 1000MS Memory Limit: 65536KB Submit Statistic Discuss Problem Description  已知一 ...

  8. SDUT-2804_数据结构实验之二叉树八:(中序后序)求二叉树的深度

    数据结构实验之二叉树八:(中序后序)求二叉树的深度 Time Limit: 1000 ms Memory Limit: 65536 KiB Problem Description 已知一颗二叉树的中序 ...

  9. URAL 1136 Parliament 二叉树水题 BST后序遍历建树

    二叉树水题,特别是昨天刚做完二叉树用中序后序建树,现在来做这个很快的. 跟昨天那题差不多,BST后序遍历的特型,找到最后那个数就是根,向前找,比它小的那块就是他的左儿子,比它大的那块就是右儿子,然后递 ...

随机推荐

  1. Using 10053 Trace Events and get outline

    When it comes to performance tuning, we can spend time on one or both ends of the problem. On the &q ...

  2. C# 文件压缩方法

    using System; using System.IO; using System.IO.Packaging; namespace Utility { public class ZipHelper ...

  3. [译]HTTP POSTing

    HTTP POSTing We get many questions regarding how to issue HTTP POSTs with libcurl the proper way. Th ...

  4. WCF学习笔记(2)-WCF的通讯过程

    一.WCF中的ABC 场景:公司让你送一份合同文件,送文件的过程你可以选择的交通方式有打的,地铁或公交. 到了对方公司后,你要找到某负责人,并且要一份收到合同文件的回执和相应文件 要完成这项工作任务主 ...

  5. datagrid 选中某行,翻页再翻回来,发现选中的行没有选中

    不管有没有设置复选框,其实都是一样的,都是idField属性没有设置,加上去即可. $(function(){ $('#dg').datagrid({ url:'ContactServlet', to ...

  6. Farseer.net轻量级开源框架 入门篇:删除数据详解

    导航 目   录:Farseer.net轻量级开源框架 目录 上一篇:Farseer.net轻量级开源框架 入门篇: 修改数据详解 下一篇:Farseer.net轻量级开源框架 入门篇: 查询数据详解 ...

  7. javascript按钮点击事件问题

    今天弄了个小测试,结果发现了点问题. 就是有一个按钮的点击事件,页面加载时候没反应,只有F12启用开发人员工具的时候才有反应.后来反复测试发现名字起的不太合理 function onclick(){ ...

  8. Java线程的sleep方法

    sleep方法的签名: public static void sleep (long millis) sleep方法是Thread类的一个方法,作用是:在指定的毫秒内让正在执行的线程休眠(暂停执行) ...

  9. 扩增子分析解读6进化树 Alpha Beta多样性

    分析前准备 # 进入工作目录 cd example_PE250 上一节回顾:我们的OTU获得了物种注释,并学习OTU表的各种操作————添加信息,格式转换,筛选信息.   接下来我们学习对OTU序列的 ...

  10. 如何在mybatis中引用java中的常量和方法

    转自:http://www.68idc.cn/help/jiabenmake/qita/20140821125261.html 在mybatis的映射xml文件调用java类的方法: 1. SELEC ...