js二叉树,前序/中序/后序(最大最小值,排序)

简介: function Node(data,left,right) { this.left=left this.right=right this.
    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()

 

相关文章
|
4月前
egg.js 24.13sequelize模型-字段限制排序分页
egg.js 24.13sequelize模型-字段限制排序分页
67 1
egg.js 24.13sequelize模型-字段限制排序分页
|
28天前
|
JavaScript
js实现模糊搜索和排序
js实现模糊搜索和排序
9 0
|
3月前
|
JavaScript
JS数组排序看懂这篇就够了
JS数组排序看懂这篇就够了
29 1
|
3月前
|
JavaScript 前端开发 数据管理
使用Sortable.js库 实现Vue3 elementPlus 的 el-table 拖拽排序
使用Sortable.js库 实现Vue3 elementPlus 的 el-table 拖拽排序
982 1
|
2月前
|
算法 JavaScript
JS 【详解】二叉树(含二叉树的前、中、后序遍历技巧和算法实现)
JS 【详解】二叉树(含二叉树的前、中、后序遍历技巧和算法实现)
32 0
|
2月前
|
JavaScript
JS 【详解】双指针排序 -- 数组合并后递增排序
JS 【详解】双指针排序 -- 数组合并后递增排序
20 0
|
2月前
|
前端开发 JavaScript
前端 JS 经典:最近距离排序
前端 JS 经典:最近距离排序
16 0
|
2月前
|
JavaScript 搜索推荐
js 混合排序(同时存在数字、字母、汉字等)
js 混合排序(同时存在数字、字母、汉字等)
132 0
|
2月前
|
JavaScript
js 排序—— sort() 对普通数组、对象数组(单属性/多属性)排序
js 排序—— sort() 对普通数组、对象数组(单属性/多属性)排序
21 0
|
3月前
|
JavaScript 搜索推荐 算法
JS的三种排序方法,它们的原理
JS的三种排序方法,它们的原理
20 0