• 技术文章 >web前端 >js教程

    关于js二叉树查询遍历插入翻转的代码

    不言不言2018-07-14 17:27:28原创972
    这篇文章主要介绍了关于js二叉树查询遍历插入翻转的代码,有着一定的参考价值,现在分享给大家,有需要的朋友可以参考一下

    function BST(){
            this.root = null
            this.insert = insert
            this.find = find
            this.mirror = mirror;
            
        }
        function Node(data,left,right){
            this.data = data
            this.left = left
            this.right = right
            this.show = show
        }
        function show() {
           return this.data;
        }
        function mirror(root){
            if(root == null){
                return
            }
            if(root.left == null && root.right == null){
                return
            }
            let temp = root.left;
            root.left = root.right;
            root.right = temp;
            mirror(root.left)
            mirror(root.right)
        }
        function insert(data){
            var n = new Node(data,null,null)
            if(this.root == null){
                this.root = n
            }else{
                var current = this.root
                while(true){
                    if(data < current.data){
                        if(current.left == null){
                            current.left = n
                            break
                        }
                        current = current.left
                    }else{
                        if(current.right == null){
                            current.right = n
                            break
                        }
                        current = current.right
                    }
                }
            }
        }
        function find(data){
            var current = this.root
            while(true){
                if(data == current.data){
                    return current
                }
                current = data < current.data ? current.left : current.right
                if(current == null){
                    return null
                }
            }
        }
        function inorder(node){
            if(!(node == null)){
                inorder(node.left)
                console.log(node.show())
                inorder(node.right)
            }
        }
        var nums = new BST()
        nums.insert(23)
        nums.insert(22)
        nums.insert(16)
        nums.insert(5)
        nums.insert(3)
        nums.insert(99)
        nums.insert(22)
        inorder(nums.root)  
        mirror(nums.root)
        console.log(nums.root)

    很简单的 看就完了。

    以上就是本文的全部内容,希望对大家的学习有所帮助,更多相关内容请关注PHP中文网!

    相关推荐:

    关于js的模板方法模式的讲解

    如何通过Vue.js使用Font Awesome实现小图标

    关于js数组filter的用法

    以上就是关于js二叉树查询遍历插入翻转的代码的详细内容,更多请关注php中文网其它相关文章!

    声明:本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn核实处理。
    上一篇:使用实现js中打地鼠的游戏 下一篇:如何使用JS求数组差集的方法
    千万级数据并发解决方案

    相关文章推荐

    • 浅析nodejs项目中的package.json的常见配置属性• angular学习之聊聊两种类型的表单• JavaScript怎么实现基础类型和对象一样有属性和方法• 简单总结JavaScript事件• 完全掌握JavaScript的Date对象
    1/1

    PHP中文网