Binary tree of javascript--data structure and algorithm

Source: Internet
Author: User

A tree is a non-linear data structure that stores data in a hierarchical manner.
Binary tree: Find very fast, and the binary tree to add or delete elements is also very fast.
An image can be described as an organization chart that describes the structure of an organization. A tree is made up of points that are connected by edges.
Some basic concepts of the tree:
Root node: The top node of a tree.
Parent node: A node that is connected to multiple nodes, which is the parent node.
Child node: The node below the parent node. (a node can have 0, 1, or more child nodes)
Leaf node: a node without any child nodes.
Path: This set of edges from one node to another node.
Tree traversal: Accesses all nodes in the tree in a particular order.
Hierarchy of a tree: the root node is a layer 0, and its child nodes are the first layer and so on.
Depth of the tree: the number of layers in the book is depth.
Key: Each node has a value associated with it.
Binary tree and two-fork search tree
Binary tree: A special tree, no more than two sub-nodes limit the number of subtree nodes to 2, you can write efficient programs in the tree to insert, find, delete data.
Left and right node: Two child nodes of the parent node.
Binary search tree: is a special two-fork tree, a relatively small node exists left node, the large existence of the right node. This feature is very efficient for finding.
  1, implement two-fork find tree:  
Nodes, the first object defined is node, and the node object of the linked list is similar. The
Node object can save both saved data and other node links (left,right), and Show () is used to display the data stored in the node.
        function Node (data,left,right) {            this. data = data;              this. left = left ;             this. Right = right ;             // this.show = Show;         }        function() {            returnthis. data;        };

Binary tree creation: binary search tree

Binary tree creation: binary search tree
The class has only one data member, which represents the node object of the binary tree to find the root nodes, initializing null;
BST has an insert () method, which is a bit more complex to insert a new node, first creating a Node object and passing the object into the data. Check if BST has a root node, or a new tree if it is not, the node is the root. Otherwise, the inserted node is not the root node, and you need to traverse BST to find the appropriate location for the insertion. The process is similar to traversing a linked list. A variable is used to store the current node, and the BST is traversed one layer at a point. After entering BST, the next step is to decide where to place the node and find the correct insertion point.
1) Set the root node to the current node.
2) If the data to be inserted is less than the current node, the new current node is set to the left node of the original node, whereas 4)
3) If the left node of the current node is null, the new node is inserted into the position, exiting the loop, and the next loop is executed.
4) Set the new current node to the right node of the original node.
5) If the right node of the current node is null, the new node is inserted in this position, the loop is rolled out, and the next loop is executed;
        functionBST () { This. root =NULL; //This.insert = insert;             This. preorder = preorder;//First Order Traversal             This. inorder = inorder;//Middle Sequence Traversal             This. postorder = Postorder;//Post-post traversal} BST.prototype.insert=function(data) {var_node =NewNode (data,NULL,NULL); if( This. root = =NULL) {                 This. root =_node; }Else{                var_current = This. Root; var_parent;  while(true) {_parent=_current; if(Data <_current.data) {_current=_current.left; if(_current = =NULL) {_parent.left=_node;  Break; }                    }Else{_current=_current.right; if(_current = =NULL) {_parent.right=_node;  Break; }                    }                }            }        };
2. Traverse the binary tree
Method: First order, middle order, post sequence. (both of which are accessed as a root reference)
First Order: Access the root node first, and then access the Saozi right subtree in ascending order.
Middle order: In ascending order, the right-to-left is accessed.
Post-sequential: first visit the leaf node, from the left subtree to the right subtree to the root node.
        //First Order traversal preorder        functionPreorder (node) {if(! (node = =NULL) {Console.log (Node.show ());                Preorder (Node.left);            Preorder (node.right); }        }        //test ...        varBST =NewBST (); Bst.insert (23); Bst.insert (45); Bst.insert (16); Bst.insert (37); Bst.insert (3); Bst.insert (99); Bst.insert (22);        Preorder (bst.root); //Middle sequence Traversal inorder        functioninorder (node) {if(! (node = =NULL) {inorder (node.left);                Console.log (Node.show ());            Inorder (Node.right); }} console.log ("--------------------");        Inorder (Bst.root); //Post- traversal inorder        functionpostorder (node) {if(! (node = =NULL) {postorder (node.left);                Postorder (Node.right);            Console.log (Node.show ()); }} console.log ("--------------------"); Postorder (bst.root);
Full code:
~(function() {            functionNode (data,left,right) { This. data =data;  This. left =Left ;  This. right =Right ; //this.show = Show;} Node.prototype.show=function() {                return  This. Data;            }; functionBST () { This. root =NULL; //This.insert = insert;                 This. preorder = preorder;//First Order Traversal                 This. inorder = inorder;//Middle Sequence Traversal                 This. postorder = Postorder;//Post-post traversal} BST.prototype.insert=function(data) {var_node =NewNode (data,NULL,NULL); if( This. root = =NULL) {                     This. root =_node; }Else{                    var_current = This. Root; var_parent;  while(true) {_parent=_current; if(Data <_current.data) {_current=_current.left; if(_current = =NULL) {_parent.left=_node;  Break; }                        }Else{_current=_current.right; if(_current = =NULL) {_parent.right=_node;  Break;            }                        }                    }                }            }; //First Order traversal preorder            functionPreorder (node) {if(! (node = =NULL) {Console.log (Node.show ());                    Preorder (Node.left);                Preorder (node.right); }            }            //Middle sequence Traversal inorder            functioninorder (node) {if(! (node = =NULL) {inorder (node.left);                    Console.log (Node.show ());                Inorder (Node.right); }            }            //Post- traversal inorder            functionpostorder (node) {if(! (node = =NULL) {postorder (node.left);                    Postorder (Node.right);                Console.log (Node.show ()); }            }        })();

3, binary tree on the search:

1) Find the minimum value;
2) Find the maximum value;
3) Find the given value.
The maximum minimum value is easier to find, as long as you traverse to the left and right.
BST.prototype.getMin =function() {            var_current = This. Root;  while(! (_current.left = =NULL) ) {_current=_current.left; }            return_current.data;        }; BST.prototype.getMax=function () {            var_current = This. Root;  while(! (_current.right = =NULL) ) {_current=_current.right; }            return_current.data;        }; Console.log ("-----------");        Console.log (Bst.getmin ()); Console.log (Bst.getmax ());
Find a given value: find ();  The value and the size of the value on the current node need to be compared. Determines whether a left or right traversal is done by comparing the size.
BST.prototype.find =function(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;//returned null not found        }; Console.log ("-----------"); Console.log (Bst.find (99));

Binary tree of javascript--data structure and algorithm

Related Article

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.