JavaScript實現排序二叉樹的相關演算法

來源:互聯網
上載者:User

標籤:中序   reac   param   pos   style   前序走訪   最大   div   return   

1.建立排序二叉樹的建構函式

/**         * 建立排序二叉樹的建構函式         * @param valArr 排序二叉樹中節點的值         * @constructor         */        function BinaryTree(valArr) {            function Node(val) {                this.value = val;                this.left = null;                this.right = null;            }            var root = null;           valArr.forEach(function (val) {                var newNode = new Node(val);                if(root === null){                    root = newNode;                } else {                    this.insetNode(root,newNode);                }           },this);            this.root = root;        }

2.向排序二叉樹中插入節點

// 向二叉樹中插入節點        BinaryTree.prototype.insetNode = function (node, newNode) {            if(newNode.value > node.value){                if(node.right === null){                    node.right = newNode;                } else {                    this.insetNode(node.right,newNode);                }            } else {                if(node.left === null){                    node.left = newNode;                } else {                    this.insetNode(node.left,newNode);                }            }        };

3.中序遍曆

  // 中序遍曆BinaryTree.prototype.LFR = function () {            const result = [];            function computed(node) {                if(node !== null ){                    computed(node.left);                    result.push(node.value);                    computed(node.right);                }            }            computed(this.root);            return result;        };

4.前序走訪

 // 前序走訪        BinaryTree.prototype.FLR = function () {            const result = [];            function computed(node) {                if(node !== null ){                    result.push(node.value);                    computed(node.left);                    computed(node.right);                }            }            computed(this.root);            return result        };

5.後序遍曆

 // 後序遍曆        BinaryTree.prototype.RFL = function () {            const result = [];            function computed(node) {                if(node !== null ){                    computed(node.right);                    result.push(node.value);                    computed(node.left);                }            }            computed(this.root);            return result        };

6.擷取最小值

// 擷取二叉樹中的最小值        BinaryTree.prototype.getMin = function (node) {            var min = null;            function computed(node) {                if(node){                    if(node.left){                        computed(node.left);                    } else {                        min = node.value;                    }                }            }            computed(node || this.root);            return min;        };

7.擷取最大值

 // 擷取二叉樹中的最大值        BinaryTree.prototype.getMax = function (node) {            var Max = null;            function computed(node) {                if(node){                    if(node.right){                        computed(node.right);                    } else {                        Max = node.value;                    }                }            }            computed(node || this.root);            return Max;        };

8.尋找給定的值

 // 尋找給定值        BinaryTree.prototype.findVal = function (val,node) {            function find(node) {                if(node){                    if(node.value === val) return true;                    else if(val > node.value) return find(node.right);                    else {                        return find(node.left);                    }                } else {                    return false;                }            }            return find(node || this.root);        };

9.刪除節點

// 刪除節點        BinaryTree.prototype.removeNode = function (val,node) {            function remove(val,node) {                if(!node) return null;                if(val > node.value) {                    node.right =  remove.call(this,val,node.right);                } else if(val < node.value){                    node.left =  remove.call(this,val,node.left);                } else {                    // 要刪除的節點沒有左孩子也沒有右孩子                    if(node.right === null && node.left === null){                        return null;                    }                    // 只有右孩子沒有左孩子                    else if(node.right && node.left === null){                        return node.right;                    }                    // 只有左孩子沒有右孩子                    else if (node.left && node.right === null) {                        return node.left;                    }                    // 有左孩子也有右孩子                    else {                        var min = this.getMin(node.right);                        node.value = min;                        node.right =  remove.call(this,min, node.right);                        return node;                    }                }                return node;            }            remove.call(this,val,node || this.root);        };

10.使用上面的方法

var binaryTree = new BinaryTree([10,4,2,14,3,15,13,12,6,9]);        console.log(‘中序遍曆‘,binaryTree.LFR());        console.log(‘前序走訪‘,binaryTree.FLR());        console.log(‘後序遍曆‘,binaryTree.RFL());        console.log(‘最小值‘,binaryTree.getMin());        console.log(‘最大值‘,binaryTree.getMax());        console.log(binaryTree.findVal(4));        binaryTree.removeNode(3);

 

JavaScript實現排序二叉樹的相關演算法

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.