【JavaScript】JavaScript数据结构与算法 —— 二叉树

来源:互联网 发布:java中的reflection 编辑:程序博客网 时间:2024/06/16 02:29

       树是计算机科学中经常用到的一种数据结构。树是非线性的数据结构,以分成的方式存储数据,树被用来存储具有层级关系的数据,比如文件系统的文件,树还被用来存储有序列表。我们要研究的是二叉树,在二叉树上查找元素非常快,为二叉树添加元素或者删除元素,也是非常快的。我们现在就来学习二叉树,二叉树是一种特殊的树,它的特征是子节点个数不超过2个,相对较少的值保存在左节点上,较大的值保存在右节点中。这一特性使得查找的效率非常高。

  • 二叉树是由节点组成的,下面对这种结构使用javascript实现增加、查询(最大、最小以及等于)、删除等操作。整体代码如下:
<!DOCTYPE html><html>    <head>        <meta charset="UTF-8">        <title></title>    </head>    <body>    </body>    <script>        function Node(data,left,right) {            this.data = data;            this.left = left;            this.right = right;            this.show = show;            this.find = find;        }        function show() {            return this.data;        }        function BST() {            this.root = null;            this.insert = insert;            this.inOrder = inOrder;            this.preOrder = preOrder;            this.postOrder = postOrder;            this.getMin = getMin;            this.getMax = getMax;            this.find = find;            this.remove = remove;        }        function insert(data) {            var n = new Node(data,null,null);            if(this.root == null) {                this.root = n;            } else {                var current = this.root;                var parent;                while(current) {                    parent = current;                    if(data <  current.data) {                        current = current.left;                        if(current == null) {                            parent.left = n;                            break;                        }                    } else {                        current = current.right;                        if(current == null) {                            parent.right = n;                            break;                        }                    }                }            }        }        function inOrder(node) {            if(!(node == null)) {                inOrder(node.left);         //先将小的输出                console.log(node.show());   //输出自己                inOrder(node.right);        //然后输出大的            }        }        function preOrder(node) {            // 先序遍历             if(!(node == null)) {                console.log(node.show());               //先输出当前节点的值                preOrder(node.left);                    //再输出左侧的值                preOrder(node.right);                   //再输出右侧的值            }        }        function postOrder(node) {            //后续访问数据            if(!(node == null)) {                postOrder(node.left);                   //先输出左侧                postOrder(node.right);                  //再输出右侧                console.log("后序遍历"+node.show());        //然后输出当前节点            }        }        function getMin(){            //获取最小值            var current = this.root;            while(!(current.left == null)) {                current = current.left;            }            return current.data;        }        // 二叉树上查找最大值        function getMax() {            var current = this.root;            while(!(current.right == null)) {                current = current.right;            }            return current.data;        }        function find(data) {            //查找给定值            var current = this.root;            while(current != null) {                if(current.data == data) {                    return current;                }else if(data < current.data) {                    current = current.left;                }else {                    current = current.right;                }            }            return null;        }        function remove(data) {            //删除节点            root = removeNode(this.root,data);            console.info(root);        }        function removeNode(node,data) {            if(node == null) {                return null;            }            if(data == node.data) {                //没有子节点的节点                if(node.left == null && node.right == null) {                    return null;                }                // 没有左子节点的节点                if(node.left == null) {                    return node.right;                }                // 没有右子节点的节点                if(node.right == null) {                    return node.left;                }                // 有2个子节点的节点                var tempNode = getSmallest(node.right);                node.data = tempNode.data;                node.right = removeNode(node.right,tempNode.data);                console.info(node.right,22222);                return node;            } else if(data < node.data) {                node.left = removeNode(node.left,data);                return node;            }else {                node.right = removeNode(node.right,data);                return node;            }        }        function getSmallest(node) {            if (node.left == null) {                return node;            } else {                return getSmallest(node.left);            }        }        var nums = new BST();        nums.insert(23);        nums.insert(45);        nums.insert(16);        nums.insert(37);        nums.insert(3);        nums.insert(99);        nums.insert(22);        //nums.inOrder(nums.root);        //nums.preOrder(nums.root);         //先序访问        //nums.postOrder(nums.root);        //后续访问        //var min = nums.getMin();        //console.info("最小值:" + min);           //最小值:3        //var max = nums.getMax();        //console.info("最大值:" + max);           //最大值:99        //var data = nums.find(23);        //console.info(data);        nums.remove(23);        console.info(nums,333333);    </script></html>
阅读全文
0 0