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后序遍历的特型,找到最后那个数就是根,向前找,比它小的那块就是他的左儿子,比它大的那块就是右儿子,然后递 ...
随机推荐
- C#模拟百度登录并到指定网站评论回帖(四)
基本的实现功能前面已经全部讲完,基本上可以复制黏贴完成登录百度的过程了 今天的这一贴就说说怎么获取百度的验证码 内容回顾:还记得前面第一贴说的如果登录发生异常,百度会发回2个值吗?是的,就是codeT ...
- 由ibatis向mybatis的转变
我将项目引用的ibatis换成mybatis 过程中遇到一个问题:org.apache.ibatis.datasource.DataSourceException: Unknown DataSourc ...
- vue 父子组件双向绑定
vue组件有2大特性: 1.全局组件和局部组件 2.父子组件的数据传递 接下来直接用demo直接看如何传值(静态传值) father.vue <template> <div> ...
- Wrapping calls to the Rational Functional Tester API——调用Rational Functional Tester封装的API
转自:http://www.ibm.com/developerworks/lotus/library/rft-api/index.html The Rational GUI automation to ...
- LDA主题模型(理解篇)
何谓“主题”呢?望文生义就知道是什么意思了,就是诸如一篇文章.一段话.一个句子所表达的中心思想.不过从统计模型的角度来说, 我们是用一个特定的词频分布来刻画主题的,并认为一篇文章.一段话.一个句子是从 ...
- CAD与用户互在图面上得到一个矩形框(com接口VB语言)
主要用到函数说明: MxDrawXCustomFunction::ExApp_CutDwg 与用户互在图面上得到一个矩形框,详细说明如下: 参数 说明 IN DOUBLE dX1 保存范围的左下角位置 ...
- CAD绘制多行文字
在CAD设计时,需要绘制多行文字,用户可以设置设置绘制文字的高度等属性. 主要用到函数说明: _DMxDrawX::DrawMText 绘制一个多行文字.详细说明如下: 参数 说明 DOUBLE dP ...
- N18_二叉树的镜像
题目描述 操作给定的二叉树,将其变换为源二叉树的镜像. 输入描述: 二叉树的镜像定义:源二叉树 8 / \ 6 10 / \ / \ 5 7 9 11 镜像二叉树 8 / \ 10 6 / \ / \ ...
- Python中使用SQLite
参考原文 廖雪峰Python教程 使用SQLite SQLite是一种嵌入式数据库,它的数据库就是一个文件.由于SQLite本身是用C写的,而且体积很小,所以经常被集成到各种应用程序中,甚至在IOS和 ...
- Python的import module与form module import的区别
import moduleName 如果要使用moduleName模块中的方法时,是moduleName.method(点方法), 比如moduleName中有个方法是set,则使用的是moduleN ...