Js二叉树(数据结构)

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>

</body>
<script>
    function Node(element,left,right){
        this.element=element;
        this.left=left;
        this.right=right;
        this.show=function (data) {

        }
    }
    function BST() {
        this.root=null
        this.insert=function (element) {
            var node=new Node(element,null,null);
            if(this.root==null){
                this.root=node;
            }else{
                var buffer=this.root;
                var parent;
                while(true){
                    parent=buffer;
                    if(element>buffer.element){
                        buffer=buffer.right;
                        if(buffer==null){
                            parent.right=node;
                            break;
                        }

                    }else {
                            buffer=buffer.left;
                            if(buffer==null){
                                parent.left=node;
                                break;
                            }
                    }

                }
            }

        }

    }
    var bst=new BST();
    bst.insert(12);
    bst.insert(9);
    bst.insert(233);
    bst.insert(666);
    bst.insert(12450);
    bst.insert(9);
    console.log(bst.root);

</script>
</html>

二叉树

猜你喜欢

转载自blog.csdn.net/Drifterkiten/article/details/81173476