JavaScript-based algorithm for sorting binary tree

Source: Internet
Author: User

1. Create a constructor for the sort binary tree

/** * Create a sort binary tree constructor * @param valarr The values of the nodes in the binary tree * @constructor*/        functionBinaryTree (Valarr) {functionNode (val) { This. Value =Val;  This. left =NULL;  This. right =NULL; }            varRoot =NULL; Valarr.foreach (function(val) {varNewNode =NewNode (Val); if(Root = = =NULL) {root=NewNode; } Else {                     This. Insetnode (Root,newnode); }           }, This);  This. root =Root; }

2. Inserting nodes into the sort binary tree

//inserting nodes into the binary treeBinaryTree.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. Middle Sequence traversal

  // Middle Sequence Traversal function  () {            = [];             function computed (node) {                ifnull  ) {                    computed (node.left);                    Result.push (node.value);                    Computed (node.right);                }            }            Computed (this. root);             return result;        };

4. Pre-sequence traversal

// Pre-sequence traversal        function  () {            = [];             function computed (node) {                ifnull  ) {                    result.push (node.value);                    Computed (node.left);                    Computed (node.right);                }            }            Computed (this. root);             return result        };

5. Post-Traversal

// Post-post traversal        function  () {            = [];             function computed (node) {                ifnull  ) {                    computed (node.right);                    Result.push (node.value);                    Computed (node.left);                }            }            Computed (this. root);             return result        };

6. Get the minimum value

//get the minimum value in a binary treeBinaryTree.prototype.getMin =function(node) {varMin =NULL; functioncomputed (node) {if(node) {if(node.left) {computed (node.left); } Else{min=Node.value; }}} computed (node|| This. Root); returnmin; };

7. Get the maximum value

 //get the maximum value in a binary treeBinaryTree.prototype.getMax =function(node) {varMax =NULL; functioncomputed (node) {if(node) {if(node.right) {computed (node.right); } Else{Max=Node.value; }}} computed (node|| This. Root); returnMax; };

8. Find the given value

 //find a given valueBinaryTree.prototype.findVal =function(val,node) {functionFind (node) {if(node) {if(Node.value = = val)return true; Else if(Val > Node.value)returnfind (Node.right); Else {                        returnfind (Node.left); }                } Else {                    return false; }            }            returnFind (Node | | This. Root); };

9. Delete a node

//Delete a nodeBinaryTree.prototype.removeNode =function(val,node) {functionRemove (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 {                    //the node to be removed has no left child and no right child                    if(Node.right = = =NULL&& Node.left = = =NULL){                        return NULL; }                    //only the right child has no left child                    Else if(node.right && Node.left = = =NULL){                        returnNode.right; }                    //only the left child has no right child                    Else if(node.left && Node.right = = =NULL) {                        returnNode.left; }                    //There's a left child and a right child .                    Else {                        varMin = This. Getmin (Node.right); Node.value=min; Node.right= Remove.call ( This, Min, node.right); returnnode; }                }                returnnode; } remove.call ( This, Val,node | | This. Root); };

10. Use the method above

var New BinaryTree ([10,4,2,14,3,15,13,12,6,9]);        Console.log (' Middle sequence traversal ', Binarytree.lfr ());        Console.log (' pre-sequence traversal ', BINARYTREE.FLR ());        Console.log (' post-sequential traversal ', BINARYTREE.RFL ());        Console.log (' minimum value ', Binarytree.getmin ());        Console.log (' maximum value ', Binarytree.getmax ());        Console.log (Binarytree.findval (4));        Binarytree.removenode (3);

JavaScript-based algorithm for sorting binary tree

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.