js二叉树,前序/中序/后序(最大最小值,排序)
function Node(data,left,right) {
this.left=left
this.right=right
this.data=data
}
function Btr() {
this.root = null;
}
// D:根节点 L:左子节点 R:右子节点
Btr.prototype.insert=function (data) { //生成排序的 二叉树
if(this.root==null){
this.root = new Node(data,null,null)
}else {
var current = this.root;
while(true){
if(data<current.data){
if(current.left != null){
current = current.left
}else {
current.left = new Node(data,null,null)
break
}
}else {
if(current.right != null){
current = current.right
}else {
current.right = new Node(data,null,null)
break
}
}
}
}
}
Btr.prototype.CenterScan=function () { //中序 ( LDR)
var list=[],root = this.root,left,right
while (root){
if(root.left){
left = root.left
list.unshift(root)
root.left=null
root = left
}else {
console.log(root.data)
if(root.right){
right = root.right
root.right=null
root = right
}else {
root = list.shift()
}
}
}
}
Btr.prototype.FrontScan=function () { //前序 (DLR)
var list=[],root = this.root,left,right
while (root){
if(root.data) console.log(root.data)
left = root.left
right = root.right
if(left){
root.left=null
root.data=null
list.unshift(root)
root = left
}else if(right){
root = right
}else {
root = list.shift()
}
}
}
Btr.prototype.BackScan=function () { //后序 (LRD)
var list=[],root = this.root,left,right
while (root){
left = root.left
right = root.right
if(left){
root.left=null
list.unshift(root)
root = left
}else {
if(right){
root.right = null
list.unshift(root)
root = right
}else {
console.log(root.data)
root = list.shift()
}
}
}
}
Btr.prototype.Max=function () {
var root = this.root,right
while (root){
right = root.right
if(right){
root = right
}else {
console.log(root.data)
root = null
}
}
}
Btr.prototype.Min=function () {
var root = this.root,left
while (root){
left = root.left
if(left){
root = left
}else {
console.log(root.data)
root = null
}
}
}
var btr = new Btr();
btr.insert(6)
btr.insert(2)
btr.insert(1)
btr.insert(4)
btr.insert(3)
btr.insert(5)
btr.insert(9)
btr.insert(8)
btr.insert(10)
btr.CenterScan()
js二叉树,前序/中序/后序(最大最小值,排序)的更多相关文章
- 算法进阶面试题03——构造数组的MaxTree、最大子矩阵的大小、2017京东环形烽火台问题、介绍Morris遍历并实现前序/中序/后序
接着第二课的内容和带点第三课的内容. (回顾)准备一个栈,从大到小排列,具体参考上一课.... 构造数组的MaxTree [题目] 定义二叉树如下: public class Node{ public ...
- (原)neuq oj 1022给定二叉树的前序遍历和后序遍历确定二叉树的个数
题目描述 众所周知,遍历一棵二叉树就是按某条搜索路径巡访其中每个结点,使得每个结点均被访问一次,而且仅被访问一次.最常使用的有三种遍历的方式: 1.前序遍历:若二叉树为空,则空操作:否则先访问根结点, ...
- 前、中、后序遍历随意两种是否能确定一个二叉树?理由? && 栈和队列的特点和区别
前序和后序不能确定二叉树理由:前序和后序在本质上都是将父节点与子结点进行分离,但并没有指明左子树和右子树的能力,因此得到这两个序列只能明确父子关系,而不能确定一个二叉树. 由二叉树的中序和前序遍历序列 ...
- 二叉树 遍历 先序 中序 后序 深度 广度 MD
Markdown版本笔记 我的GitHub首页 我的博客 我的微信 我的邮箱 MyAndroidBlogs baiqiantao baiqiantao bqt20094 baiqiantao@sina ...
- 前序+中序->后序 中序+后序->前序
前序+中序->后序 #include <bits/stdc++.h> using namespace std; struct node { char elem; node* l; n ...
- SDUT OJ 数据结构实验之二叉树八:(中序后序)求二叉树的深度
数据结构实验之二叉树八:(中序后序)求二叉树的深度 Time Limit: 1000 ms Memory Limit: 65536 KiB Submit Statistic Discuss Probl ...
- SDUT 1489 求二叉树的先序遍历 (中序后序还原二叉树)
求二叉树的先序遍历 Time Limit: 1000MS Memory Limit: 65536KB Submit Statistic Discuss Problem Description 已知一 ...
- SDUT-2804_数据结构实验之二叉树八:(中序后序)求二叉树的深度
数据结构实验之二叉树八:(中序后序)求二叉树的深度 Time Limit: 1000 ms Memory Limit: 65536 KiB Problem Description 已知一颗二叉树的中序 ...
- URAL 1136 Parliament 二叉树水题 BST后序遍历建树
二叉树水题,特别是昨天刚做完二叉树用中序后序建树,现在来做这个很快的. 跟昨天那题差不多,BST后序遍历的特型,找到最后那个数就是根,向前找,比它小的那块就是他的左儿子,比它大的那块就是右儿子,然后递 ...
随机推荐
- navicat mysql报错误:2013 Lost connection to MySQL server during query
好像是MySQL的navicat UI界面跟数据的连接问题,如果直接用命令导入数据的话,或许能规避这个问题.
- 因Window服务器自动更新并重启导致WebSphere服务停止服务故障一例
最近公司购买了两台Windows Server 2008 R2服务器用于提供提供Web服务,A机器安装了IHS+DM+WAS8.5集群,B机器安装了Oracle11gR2用于数据存储,两台机器均可连接 ...
- 无法收集统计信息,怎样优化SQL。
特殊情况如下 客户的统计信息是固定的,没办法收集统计信息 . SQL profile 是最后考虑方案,因为同样写法sql 比较多,几十条. Parallle 并行客户一般不考虑接受,OLTP 系统. ...
- Spring-security自定义过滤器
定义过滤器 public class TokenAuthenticationFilter extends AbstractPreAuthenticatedProcessingFilter { publ ...
- Selenium常用方法及函数
新建实例driver = webdriver.Chrome() 1.获取当前页面Url的函数方法:current_url实例:driver.current_url 2.表单的提交方法:submit解释 ...
- 浏览器 chrome 360等 加载本地json 或者xml 文件
添加启动参数 --allow-file-access-from-files 来自为知笔记(Wiz)
- VC++函数只被调用一次
如何保证某个函数只被调用一次 一个函数caller会在其内部调用另外一个函数callee,现在的情况是,caller可能会在多个地方被多次调用,而你希望callee只在第一次被调用时被调用一次.一 ...
- SpringBoot+Mybatis 自动创建数据表(适用mysql)
Mybatis用了快两年了,在我手上的发展史大概是这样的 第一个阶段 利用Mybatis-Generator自动生成实体类.DAO接口和Mapping映射文件.那时候觉得这个特别好用,大概的过程是这样 ...
- vue(数据改变,DOM不渲染问题)
1.组件内部,属性值地址空间内引用地址改变,DOM不能渲染. 问题举例:this.items = [[],[],[],[]] 1.在items 中,修改任意一项数组中的值,DOM是不会更新的,2.解决 ...
- 我的第一次"闭包"应用
结论: 闭包可以当作强类型语言如C++.Java的全局变量使用,非常巧妙 需求: ssm项目,使用pagehelper分页,在写前一页.后一页.第一页.最后一页等页面跳转时,遇到了问题,如果查询全部的 ...