js二叉树的实现练手

来源:互联网 发布:哈登总得分最新数据 编辑:程序博客网 时间:2024/06/07 02:50

js二叉树的实现练手

二叉树的好处就不用说了,本实例主要实现了排序、查找、删除、新增,有兴趣的同学可以继续完善。

<!DOCTYPE html><html><head>    <title>js二叉树</title></head><body>    <script type="text/javascript">        /*            ===yancheng@infogo.com.cn======            主要实现:二叉树排序、查找、删除、新增            原始数据:[8,4,1,6,5,7,15,14,13,16]            二叉树结构:左小右大方式                            8                   ----第一层                4                       15      ----第二层            1       6               14      16  ----第三层                5       7       13              ----第四层            二叉树满足条件:                (1)若左子树不空,则左子树上所有结点的值均小于它的根结点的值;                (2)若右子树不空,则右子树上所有结点的值均大于或等于它的根结点的值;                (3)左、右子树也分别为二叉排序树;                (4)没有键值相等的节点。        */        //二进制树        function binaryTree(valarr){            var rootNode = null;            //创建节点对象方法            function nodeTree(val){                this.val = val;                this.left = null;                this.right = null;            }            //设置节点            var setNode = function(node,val){                var newNode = new nodeTree(val);                if(rootNode===null){                    rootNode = newNode;                }else if(val<node.val){                    if(node.left!==null){                        setNode(node.left, val);                    }else{                        node.left = newNode;                    }                }else if(val>node.val){                    if(node.right!==null){                        setNode(node.right, val);                    }else{                        node.right = newNode;                    }                }            }            //根据值返回节点            var getNode = function(val){                var searchval = parseFloat(val);                if(rootNode!==null && searchval){                       return (function fn(node, searchval){                            if(searchval == node.val){                                return node;                            }else if(searchval<node.val && node.left!==null){                                return fn(node.left, searchval);                            }else if(searchval>node.val && node.right!==null){                                return fn(node.right, searchval);                            }else{                                return false;                            }                    })(rootNode, searchval)                }                return false;            }            //获取最小节点            var getMinNode = function(node){                if(node!==null){                    return (function fn(node){                        if(node.left!==null){                            return fn(node.left);                                                   }                        return node;                    })(node)                }                return null;            }            //前序遍历            this.preorderTraversal = function(){                if(rootNode!==null){                    var orderBefer = [];                    return (function fn(node){                        if(node!==null){                            orderBefer.push(node.val);                            fn(node.left);                            fn(node.right);                            return orderBefer;                        }                    })(rootNode)                }                return null;            }            //中序遍历            this.inorderTraversal = function(){                if(rootNode!==null){                    var orderBefer = [];                    return (function fn(node){                        if(node!==null){                            fn(node.left);                            orderBefer.push(node.val);                            fn(node.right);                            return orderBefer;                        }                    })(rootNode)                }                return null;            }            //后续遍历            this.postorderTraversal  = function(){                if(rootNode!==null){                    var orderBefer = [];                    return (function fn(node){                        if(node!==null){                            fn(node.right);                            orderBefer.push(node.val);                            fn(node.left);                            return orderBefer;                        }                    })(rootNode)                }                return null;            }            //获取最大值            this.getMaxVal = function(){                if(rootNode!==null){                    return (function fn(node){                        if(node.right!==null){                            return fn(node.right);                                                  }                        return node.val;                    })(rootNode)                }                return null;            }            //获取最小值            this.getMinVal = function(){                if(rootNode!==null){                    var minNode = getMinNode(rootNode);                    if(minNode!=null){                        return minNode.val;                    }                }                return null;            }            //查找某值是否存在            this.searcheVal = function(val){                var node = getNode(val);                return typeof node == 'object' ? true :false;            }            //删除某个值            this.removeVal = function(val){                var searchval = parseFloat(val);                if(rootNode!==null && searchval){                    return (function fn(node, val){                        if(node===null){return null;}                        if(val < node.val){                            node.left = fn(node.left, val);                            return node;                        }else if(val > node.val){                            node.right = fn(node.right, val);                            return node;                        }else{//找到                            if(node.left!=null && node.right==null){                                node = node.left;                                return node;                            }else if(node.left==null && node.right!=null){                                node = node.right                                return node;                            }else if(node.left!=null && node.right!=null){                                //如果是中间节点且有左右子树,则查找右边最小节点,替换当前节点                                var rightMinNode = getMinNode(node.right);                                node.val = rightMinNode.val;                                node.right = fn(node.right, rightMinNode.val);                                return node;                            }else{                                node = null;                                return node;                            }                        }                    })(rootNode, val);                }                return false;            }            //添加值            this.insertVal = function(val){                setNode(rootNode,val);            }            //初始化数据            return (function(_this){                for (var i = 0; i < valarr.length; i++) {                    setNode(rootNode, valarr[i]);                }                return _this;            })(this);        }        //启动        var BT = new binaryTree([8,4,1,6,5,7,15,14,13,16]);        console.log(BT);        var predata = BT.preorderTraversal();            console.log('前序:', predata);        var indata = BT.inorderTraversal();            console.log('中序:', indata);        var postdata = BT.postorderTraversal();            console.log('后序:', postdata);        var isSearch = BT.searcheVal(5);            console.log('搜索5:', isSearch);        var minVal = BT.getMinVal();            console.log('最小值:', minVal);        var maxVal = BT.getMaxVal();            console.log('最大值:', maxVal);        var removeVal = BT.removeVal(8);            console.log('删除8后 前序:', BT.preorderTraversal());        var insertdata = BT.insertVal(9);            console.log('插入9 前序:', BT.preorderTraversal());    </script></body></html>

简单测试过,欢迎吐槽。

原创粉丝点击