How to use the stack to perform breadth-first search on the tree (Drill-down)

Source: Internet
Author: User
Tags requires

Brief introduction

This article describes how to use the stack to perform breadth-first search (Drill-down) on a tree, either by using a procedure-allocated stack or by using a separate stack data structure. BFS is a kind of tree scanning method which takes the breadth as a priority. The first node that is printed is the root node, followed by its immediate child node, and then the next-level child node. Here, the root node is at level 1th and its child nodes are at level 2nd. Next, you print the grandchild node on level 3rd. BFS will continue to print this way until it reaches the final level. The following tree is an example.

 (a) |      V---------------------------------------------|      |      |      |     |         |
          |      V v v v V (0) (1) (2) (3) (4) (5) (6) |      |      |      |     |         |
          |      V | V--+ v------+ V-------------|  ------------- | ------------- |    -------------
| | | | | | |  | | | | | | | | |
| | | | | | | | | | | | | | | A B C D E F G |  A B C D E F G | A B C D E F G |               A B C D E F G |                |
                 |  V v v---------------------------------------| | |   | | | | | | |
              | | | | | | | A b c d E F g A B c D E F g A B c D E F g 

This will print as "a 0 1 2 3 4 5 6 a B c D E F g A B c D E F g A B c D f g A b C" E "*" "a" D "," "" B "B" "" a "D E F g" B "C" F "c E F G ".

Scheme

A known solution to this problem is to use an additional queue. When a node is encountered, its child nodes are pushed to the queue. Continuously move the queue up to print the node and push all its child nodes into the queue.

Procedure BFS (root:node*);
var myqueue:queue;
var node:node*;
BEGIN
push (myqueue,root);
while (Myqueue don't empty) do
begin
Node=pop (myqueue);
print node;
For [All Children of node] do
push (Myqueue,children);
End End

In this solution, we have a memory problem, especially when the tree is not a binary tree or it is a tree with a balanced type (the number of nodes grows exponentially). For example, in the tree above, level 3rd itself has 49 nodes. In this case, if the 2nd-level End node (' 6 ') is printed, then the queue will contain all 49 entries. The nth level is now evaluated, and it will contain 7^ (N-1) nodes. So, if a tree has a level 11, then there will be 300 million entries, and if each entry has a 4 byte address pointer, the cumulative will occupy 1 MB of memory. This is even worse when the function is reentrant (re-entrant) and accessed through multiple threads.

Suggested Solutions

With the new solution, we can sacrifice a certain amount of time in exchange for space advantage. If a recursive node is found, the space used will depend solely on the series, whereas in the balance tree it will be log (n), where ' n ' is the total number of nodes. The pseudo code for the solution is shown below.

Procedure BFS (root:node*);
 var target=0;
 var node=root;
 Begin for each level in the tree do
 begin
 Printtree (node,target,0);
 target=target+1;
 End
 End
     
    
 procedure Printtree (node:node*, Target:int, level:int);
 Begin
 if (Target > level) then the begin for each
 children the
 node do
 printtree (child,target,level+1 );
 End
 Else
 print node;
 End

In a 32-bit system, if the stack size occupied during each function call is computed, it will be the ' argument + return address + current stack pointer + current base address pointer '. The size is usually 4*3+4+4+4=20 bytes. If there is a total of 11 levels, the size will be 220 bytes, much smaller than 1 MB.

Working principle

There are two entities involved: a helper and a tree scanner. In the design, the helper contains a tree scanner.

+------+         +------------+

| helper|<>------>| Tree scanner|

|         |            | |

+------+         +------------+

<>--------> Aggregation

The

Helper requires the tree scanner to print all nodes at a specific level. The tree scanner finds nodes at each level and asks whether they belong to the level that the helper is looking for. If the node belongs to this particular level, it is validated and returned. Then print all nodes at this particular level, and the helper requires the tree scanner to print the next node until it reaches the last level. In the following example, a tree helper requires that all nodes of level 3rd be printed.

           +--> (0) +--> (A) |--> (E) |--> (1) |--> (B)-->|--> (f) |--> (2)-       ->|--> (C) |--> (h) (a)----->|.
           |.                           | .       |--> (i) ^ |.       +--> (D)------------>|--> (j) |                 +--> (6)-->|--> (E) |--> (a) |--> (k) |--> (F)-->|--> (b) |
    
    
|--> (G) |--> (c) +--> (H) |
    
    
    
           Tree scanner checks at a, if it does not match to level number 3 it proceeds to next level +--> (0) +--> (A) |--> (E) |--> (1) |--> (B)-->|--> (f) |--> (2)-->|-       -> (C) |--> (h) (a)----->|.
           |.                           | .       |--> (i) |.
                     +--> (D)------------>|--> (j) +--> (6)-->|--> (E) |--> (a) |--> (k) |--> (F)-->|--> (b) ^ |--> (G) |--> (c) | +--> (H)//scanner checks at second level, next if it does not match 3 It proceeds to next level +--> (0) +--> (A) |--> (E) |--> (1) |       -> (B)-->|--> (f) |--> (2)-->|--> (C) |--> (h) (a)----->|.
           |.                           | .       |--> (i) |.
                     +--> (D)------------>|--> (j) +--> (6)-->|--> (E) |--> (a) |--> (k)
    
                          |--> (F)-->|--> (b) |--> (G) |--> (c) +--> (H)
                         ^        
                          |  //scanner checks at third level next and it does match to level Number 3 It prints nodes to next

Other advantages
Beautify

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.