I think this society, also not bad money many people, may be a lot of people also not bad power, but I think can get this kind of satisfaction is not Much. – Guoxiaoping < Linfen Red Ribbon School Principals >
? A tree is a data structure that is often used in computer science. A tree is a non-linear data structure that stores data in a hierarchical manner. is used to store hierarchical or ordered data, such as files in a file System.
Two-fork Tree
Binary tree, Each node has a maximum of two subtree tree structure. A binary tree is a special kind of tree, and it is also a non-circular graph with Connectivity.
Binary search Tree
? Binary search tree is a special two-fork tree whose relatively small values are saved in the left node, and the larger values are saved in the right Node. This feature makes it highly efficient to find.
Implement a Two-fork Find tree
? If the node to be inserted is less than (greater Than) the current node, and the left (right) node of the current node is null, the insertion node is inserted into the left (right) node position of the current node, ending the loop, or the left (right) section of the current node continues the next cycle as the current Node.
/** * Node definition * @param data * @param left left dial hand tree * @param right sub-tree * @constructor * / function Node(data, left, right) { this. data = data; this. left = left; this. right = right;} Node.prototype.show = function() { return this. data;};/** * Binary search tree definition * @constructor */ function BST() { this. root =NULL;}/** * Insert node * @constructor */BST.prototype.insert = function(data) { //to Insert a node varnode =NewNode (data,NULL,NULL);if( this. root = = =NULL) { this. root = node; }Else{varCurrentNode = this. root;varParent while(true) {parent = currentnode;//to Insert a node smaller than the current node if(data < Currentnode.data) {//leave its left sub-tree as the current nodeCurrentNode = currentnode.left;if(currentnode = = =NULL) {parent.left = node; break; } }Else{//use its right subtree as the current nodeCurrentNode = currentnode.right;if(currentnode = = =NULL) {parent.right = node; break; } } } }return this;//support Chaining calls};
- Middle Order: first access the left subtree, then access the root node, and finally access the right word count, in ascending order to access all the nodes in bst; ( left ==> root ==> right )
- First order: access the root node first, and then access the Saozi right subtree in the same way; ( root ==> left ==> right )
- Post-sequential: first visit the leaf node, from the left subtree to the right subtree, then to the root Node. ( left ==> right ==> root )
BST.prototype.order = function(node, type) { Switch(type) { case "inorder"://middle Order if(node! =NULL) {/ * Left ==> root ==> right * / this. Order (node.left, type); Console.log (node.show ()); this. Order (node.right, type); } break; case "preorder"://pre-order if(node! =NULL) {/ * Root ==> left ==> right * /Console.log (node.show ()); this. Order (node.left, type); this. Order (node.right, type); } break; case "postorder":// post if(node! =NULL) {/ * Left ==> right ==> * / this. Order (node.left, type); this. Order (node.right, type); Console.log (node.show ()); } break; }};
Test
varnew BST();bst.insert(32).insert(11).insert(2) .insert(13).insert(75) .insert(66).insert(88"inorder"// 中序"preorder"// 先序"postorder"// 后序
Querying min and Max values
- Minimum value: traverse the left subtree until the last node is found;
- Maximum: traverses the right subtree until the last node is found.
/** * Gets the minimum value: the last node of the Zuozi */BST.prototype.getMin = function(node) { varCurrentNode = Node | | this. root; while(currentnode.left!==NULL) {currentnode = currentnode.left; }returncurrentnode;};/** * Gets the minimum value: the last node of the right subtree */BST.prototype.getMax = function(node) { varCurrentNode = Node | | this. root; while(currentnode.right!==NULL) {currentnode = currentnode.right; }returncurrentnode;}; Console.log (bst.getmin (). data);//2Console.log (bst.getmax (). data);//
Find a Node
/** * Find a node * @param data * *BST.prototype.find = function(data) { varCurrentNode = this. root; while(currentnode!==NULL) {if(data = = = Currentnode.data) {returncurrentnode; } CurrentNode = (data < currentnode.data)? currentNode.left:currentNode.right; }return NULL;}; Console.log (bst.find ( the));//Node {data:66, left:null, right:null}Console.log (bst.find ( about));//null
Delete a node
- If the node to be deleted is a leaf node, delete it directly;
- If the node to be deleted has only one child node, the parent node of the node to be deleted is directed to its child nodes;
- If the node to be deleted contains two sub-nodes, we select the minimum value on the right subtree to create a temporary node, then copy to the pending deletion point, and then delete the minimum value Node.
/** * Remove the specified data node * @param data */BST.prototype.remove = function(node, data) {node = node | | this. root;if(data = = =NULL) {//the node to be deleted does not exist returnNode }if(node.data = = = Data) {//no child Nodes if(node.left = = =NULL&& Node.right = = =NULL) {return NULL; }//only Right node, no left node if(node.left = = =NULL) {returnnode.right; }//only left node, no right node if(node.right = = =NULL) {returnnode.left; }//presence of two nodes varMinnode = this. Getmin (node.right); Console.log (minnode); Node.data = minnode.data; Node.right = this. Remove (node.right, minnode.data); }Else if(node.data > Data) {node.left = this. Remove (node.left, data); }Else if(node.data < Data) {node.right = this. Remove (node.right, data); }returnnode;};
Summarize
? trees, which are very much embodied in computer science. For example, we are familiar with the DOM tree, the database is often used at the bottom of the b-tree and so On. Tree can be a good guarantee dictionary order, storage Dictionary Space compression rate is high, can do prefix Search. abstractly, a tree can be applied where there is a sequence, because the tree structure is a sequential index structure .
JavaScript data Structure-tree