Two-fork tree traversal in JS detailed _javascript tips

Source: Internet
Author: User

The binary tree is composed of the root node, the left subtree and the right subtree, and the Zuozi and the friend subtree are a binary tree respectively.
This article is mainly in JS to implement the two-fork tree traversal.

Example of a binary tree

var tree = {
 value:1, left
 : {
  value:2, left
  : {
   value:4
  }
 }, right
 : {
  value:3,< C11/>left: {
   value:5, left
   : {
    value:7
   }, right
   : {
    value:8
   }
  },
  right : {
   value:6
  }
 }
}

Breadth-First traversal
breadth-first traversal begins with the first layer (root node) of the two-fork tree and traverses from top to bottom, and in the same layer, the node is accessed sequentially from left to right.
Realize:
<!--more-->
Use arrays to simulate queues. First, the root node is grouped into the queue. When the queue is not empty, execution loop: takes out a node of the queue, and if the left subtree of the node is Non-null, the left subtree of the node is queued, and if the right subtree of the node is non-null, the right subtree of that node is queued.
(The description is a bit unclear, just look at the code.) )

var levelordertraversal = function (node) { 
 if (!node) {  
  throw new Error (' Empty tree ')
 } 
 var que = []
   que.push (node) while 
 (que.length!== 0) {
  node = que.shift ()  
  console.log (node.value)  
  if ( Node.left) Que.push (node.left)  
  if (node.right) Que.push (node.right)
 }
}

Recursive traversal
I think three ways to use these letters to represent recursive traversal are good:
D: Accessing the root node, L: Traversing the left subtree of the root node, R: Traversing the right subtree of the root node.
First-order traversal: DLR
In-sequence traversal: LDR
Subsequent traversal: LRD

The meaning of the alphabet is the sequence of traversal ^ ^

These 3 types of traversal are recursive traversal, or depth-first traversal (Depth-first Search,dfs), because it always
is a priority to deep access.

Recursive algorithm for first-order traversal:

var preorder = function (node) { 
 if (node) {  
  console.log (node.value);
  Preorder (node.left);
  Preorder (node.right);
 }

Recursive algorithm for sequence traversal:

var inorder = function (node) { 
 if (node) {
  inorder (node.left);  
  Console.log (node.value);
  Inorder (node.right);
 }

Recursive algorithm for subsequent traversal:

var postorder = function (node) { 
 if (node) {
  postorder (node.left);
  Postorder (node.right);  
  Console.log (Node.value);
 }

Non-recursive depth first traversal
In fact, I don't know who belongs to these concepts. In some books, the two-fork tree is traversed by only three recursive traversal. Some are divided into breadth-first traversal and depth-first traversal, the recursive traversal is divided into depth traversal, some of the recursive traversal and non-recursive traversal of two, not recursive traversal includes breadth-first traversal and the following traversal. The individual thinks how points actually unimportant, grasps the method and the use is good:

The queue is just used in the breadth-first traversal, and we use the stack in this recursive depth-first traversal. In JS or use an array to simulate it.
Here we only say preface:
Well, I've tried to describe the algorithm, but it doesn't make sense, and you know it by walking the code.

var preorderunrecur = function (node) { 
 if (!node) {  
  throw new Error (' Empty tree ')
 } 
 var stack = []
 STA Ck.push (node) while 
 (stack.length!== 0) {
  node = stack.pop ()  
  console.log (node.value)  
  if (node.right ) Stack.push (node.right)  
  if (node.left) Stack.push (node.left)
 }
}

Read this article, found the algorithm is not recursive sequence, so here the recursive traversal method to complement the complete.
non-recursive middle order
First push the left node of the number into the stack, then take out, and then push the right node.

var inorderunrecur = function (node) { 
 if (!node) {  
  throw new Error (' Empty tree ')
 } 
 var stack = [] 
 whil E (stack.length!== 0 | | node) {  
  if (node) {
   Stack.push (node)
   node = Node.left
  } else {
   node = Stack.po P ()   
   Console.log (node.value)
   node = node.right
  }
 }
}

Non-recursive sequence (using a stack)
here a temporary variable is used to record the last time a node was in the stack/out stack. The idea is to first push the root node and left tree into the stack, and then take out the left tree, and then push into the right tree, take out, and finally pick up the node.

var posorderunrecur = function (node) { 
 if (!node) {  
  throw new Error (' Empty tree ')
 } 
 var stack = []
   stack.push (node) 
 var tmp = null while
 (stack.length!== 0) {
  tmp = stack[stack.length-1]  
  if (  Tmp.left && node!== tmp.left && node!== tmp.right) {
   Stack.push (tmp.left)
  } else if (tmp.right && node!== tmp.right) {
   Stack.push (tmp.right)
  } else {   
   console.log (Stack.pop (). Value)
   node = tmp}}}

Non-recursive sequence (using two stacks)
the idea of this algorithm is similar to the one above, S1 a bit like a temporary variable.

var posorderunrecur = function (node) { 
 if (node) {  
  var S1 = []  
  var s2 = []
  s1.push (node) while  
  (S1.leng Th!== 0) {
   node = s1.pop ()
   S2.push (node)   
   if (node.left) {
    S1.push (node.left)
   }   
   if ( Node.right) {
    S1.push (node.right)
   }
  } while  
  (s2.length!== 0) {   
   console.log (S2.pop (). Value );
  }
 }
}

Morris traversal
This method is not recursive without the stack implementation of three depth traversal, space complexity of O (1) (This concept I am not particularly clear org)
(These three algorithms I put first, free to study)
Morris First Preface:

var morrispre = function (head) { 
 if (!head) {  
  return
 } 
 var cur1 = head,
   CUR2 = null while
 (CUR1) { C7/>CUR2 = Cur1.left  
  if (CUR2) {while   
   (cur2.right && cur2.right!= cur1) {
    cur2 =
   cur2.right}< C12/>if (!cur2.right) {
    cur2.right = cur1    
    console.log (cur1.value)
    cur1 = Cur1.left    
    Continue
   else {
    cur2.right = null
   }
  } else {   
    console.log (cur1.value)
  }
  cur1 = cur1.right< c24/>}
}

Morris in order:

var morrisin = function (head) { 
 if (!head) {  
  return
 } 
 var cur1 = head,
   CUR2 = null while
 ( CUR1) {
  CUR2 = Cur1.left  
  if (CUR2) {while   
   (cur2.right && cur2.right!==) {
    Cur1 = Cur2 Ght
   }   
   if (!cur2.right) {
    cur2.right = cur1
    cur1 = Cur1.left    
    Continue
   } else {
    Cur2.right = null
   }
  }  
  console.log (cur1.value)
  cur1 = Cur1.right
 }
}

Morris after:

var morrispost = function (head) { 
 if (!head) {  
  return
 } 
 var cur1 = head,
   CUR2 = null while
 (CUR1) {
  CUR2 = Cur1.left  
  if (CUR2) {while   
   (cur2.right && cur2.right!== cur1) {
    CUR2 = cur2.right
   }   
   if (!cur2.right) {
    cur2.right = cur1
    cur1 = Cur1.left    
    Continue
   } else {
    cur2.right = Null
    Printedge (cur1.left)
   }
  }
  cur1 = Cur1.right
 }
 Printedge (head)
}
var Printedge = function (head) { 

The above is the entire content of this article, I hope to help you learn.

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.