Traversing way of binary tree
is the middle sequence traversal (left subtree, right subtree, currentnode), Pre-sequence traversal (current node, left dial hand tree, right subtree), post-order traversal (left subtree, right subtree, current node). The following three traversal algorithms for two-fork trees are implemented using the JavaScript language.
First, a sort binary tree is constructed (that is, a two-fork tree is satisfied that the left child node is smaller than the parent node and the right child node is larger than the parent node), and then it is sequenced, pre-ordered and sequentially traversed.
Sort the binary tree structure as shown in the diagram:
Description
8 of them are root nodes (nodes without parent nodes), 4, 7, 13 are leaf nodes (nodes with no child nodes on the last layer), 3, 10, 1, 6, 14 are intermediate nodes.
The maximum depth of the tree is 4 (4 layers).
Specific code:
<! DOCTYPE html>functionBinaryTree () {varNode =function(key) {//define the node, including the parent, left child, right child node This. key = key;//passed in element value as parent node This. left =NULL;//left Dial hand node is initially null This. right =NULL;//The right child node is initially null }; varRoot =NULL;//set root node initial value to null //Define the Insert Node function, the entry is the current node and the newly added node varInsertnode =function(node, newNode) {if(Newnode.key < Node.key) {//If the parent node value of the new node is less than the value of the parent node of the current node, go to the left half if(Node.left = = =NULL) {//If the current node has no left child node, the new node is the left child node of the current nodeNode.left =NewNode; } Else{//recursive invocation of the Insert node function if the left child node of the current node existsInsertnode (Node.left, NewNode); } } Else{//If the parent node value of the new node is not less than the parent node value of the current node, go to the right half if(Node.right = = =NULL) {//If the right child node of the current node does not exist, the new node is the right child node of the current nodeNode.right =NewNode; } Else{//If the right child node of the current node exists, the Insert node function is called recursivelyInsertnode (Node.right, NewNode); } } }; This. Insert =function(key) {//Defining the Insert node function varNewNode =NewNode (key);//define a new node if(Root = = =NULL) {//joghen node is empty, the new node is the root nodeRoot =NewNode; } Else{//joghen node exists, the Insert node function is executedInsertnode (root, NewNode); } }; //Middle Sequence Traversal: the right subtree, the current node, left dial hand tree, the result is an ascending ordered sequence varInordertraversenode =function(node, callback) {if(Node!==NULL) {//Current node existsInordertraversenode (Node.left, callback);//traversing the left sub-treeCallback (Node.key);//Execute callback functionInordertraversenode (Node.right, callback);//traversing the right sub-tree } } This. Inordertraverse =function(callback) {Inordertraversenode (root, callback); } //Pre-sequence traversal: current node--left dial hand tree, right subtree, often used to replicate a binary tree, high efficiency. varPreordertraversenode =function(node, callback) {if(Node!==NULL) {//The current node is not emptyCallback (Node.key);//Execute callback functionPreordertraversenode (Node.left, callback);//traversing the left sub-treePreordertraversenode (Node.right, callback);//traversing the right sub-tree } } This. Preordertraverse =function(callback) {Preordertraversenode (root, callback); } //Post -Post traversal: current node, left dial hand tree, right subtree, often used for file system traversal varPostordertraversenode =function(node, CALLBAKC) {if(Node!==NULL) {//The current node is not emptyPostordertraversenode (Node.left, callback);//traversing the left sub-treePostordertraversenode (Node.right, callback);//traversing the right sub-treeCallback (Node.key);//Execute callback function } } This. Postordertraverse =function(callback) {Postordertraversenode (root, callback); } } //test binary tree sorting varnodes = [8, 3, 10, 1, 6, 14, 4, 7, 13]; varBinaryTree =NewBinaryTree ();//instantiate a new two-fork treeNodes.foreach (function(key) {//iterates through all elements in an array, performs a binary tree sort operation, generates a sort binary treeBinarytree.insert (key); }); //callback function varcallback =function(key) {Console.log (key);//Prints the current node value } //Middle sequence Traversal testBinarytree.inordertraverse (callback); //Pre-sequence traversal testBinarytree.preordertraverse (callback); //Post- test traversal testsBinarytree.postordertraverse (callback); </script></body>JavaScript implements two-fork tree algorithm