Home > Web Front-end > JS Tutorial > body text

Code about js binary tree query traversal, insertion and flipping

不言
Release: 2018-07-14 17:27:28
Original
1846 people have browsed it

This article mainly introduces the code about js binary tree query traversal, insertion and flipping. It has a certain reference value. Now I share it with everyone. Friends in need can refer to it.

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)
Copy after login

It’s very simple and it’s done. .

The above is the entire content of this article. I hope it will be helpful to everyone's study. For more related content, please pay attention to the PHP Chinese website!

Related recommendations:

Explanation on the template method pattern of js

How to use Font Awesome through Vue.js Small icon

#About the usage of js array filter

The above is the detailed content of Code about js binary tree query traversal, insertion and flipping. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template