zoukankan      html  css  js  c++  java
  • 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()
    
  • 相关阅读:
    oracle11g 卸载和安装(win7,32位)
    MySQL忘记密码解决办法
    GPIO硬件资源的申请,内核空间和用户空间的数据交换,ioctl(.....),设备文件的自动创建
    模块参数,系统调用,字符设备编程重要数据结构,设备号的申请与注册,关于cdev的API
    开发环境的搭建,符合导出,打印优先级阈值
    定时器中断
    Linux系统移植的重要文件
    linux 相关指令
    linux各文件夹含义和作用
    外部中断实验
  • 原文地址:https://www.cnblogs.com/jiebba/p/7694104.html
Copyright © 2011-2022 走看看