Tracking recursive method progress

I am writing an application using a tree structure, so of course I have some recursive methods that will process each node of the tree and do something. The problem is that sometimes it takes some time, and I would rather show a progress bar of some kind to the user, and then stop answering the call for a certain period of time.

If I repeat through a flat list, I know how many elements are in the list to start with it, so that it is easy to track what cycle it is on and update the progress bar accordingly.

But with the recursive method repeating the tree structure, I don’t necessarily know how many nodes the tree has at the beginning. Do I have to read through the tree recursively first and just count all the nodes before running a real recursive method that does everything I want? Or maybe just keep track of the current quantity as nodes are added or removed from the tree? Is there a better option?

+5
source share
6 answers

- , , . , Firefox - , - , - , , . , , " , ..."

+3

. , node , node. :

  • , ( ).

  • , k- n k/n * 100 . .

+1

, , , - , , ...

, org, , org, . , , , Sue, , - .

Currently processing Sue\Joe\Bob\David

, node. , Application.DoEvents() , . , , , , . , , .

, Application.DoEvents() , , , , , , "".

+1

, , , , . .

, , ( , ), - , 50 1:

: Level1 - 1/50

2 1, , :

: Level1 - 1/50, Level2 - 1/25

, , , , . , 2 :

: Level1 - 1/50, Level2 - 2/25

2 1 , messsage :

: Level1 - 2/50

, , 1 , . , , , , .

, . , .

, . ( , ) ToString(), .

, , - , . , , .

+1

( ). , , , - , MaxValue ( , ). , , , .

Microsoft, , .

0

, .

, , , .

99% . , ( ..), , . , 60% 100% . B

     A
    / \
   /   \
 (B)    F
 /|\   /|\
C D E G H I

:

[Node] [True Progress] [Displayed]
A           10%           10%
 B          20%           20%
 F          60%           30%
  G         70%           40%
  H         80%           50%
  I         90%           60%
  -        100%          100%

. 200 . 200, ( 100 ). . 400 , 200 . , 1 4 ..

, 100/200 . , ~ 120 , . . , . , ( № 84, 84/120 = 70%.

, , .

, .

, 5

              Node Landmarks Steps Comments
              A    [A----]   1     -
              B    [AB---]   1     -
     A        C    [ABC--]   1     -
    / \       D    [ABCD-]   1     -
   /   \      E    [ABCDE]   1     -
 (B)    F     F    [BDF--]   2     Cleanup of A&C&E
 /|\   /|\    G    [BDF--]   2     G is skipped
C D E G H I   H    [BDFH-]   2     -
              I    [BDFH-]   2     Done

5 , 20%

( B) :

[Node] [True Progress] [Displayed]
A           10%            0%
 B          20%           20%
 F          60%           60%
  G         70%           60%
  H         80%           80%
  I         90%           80%
  -        100%          100%

You can track the smallest common ancestor (LCA) of two consecutive landmarks. If the operation leaves the LCA, it can conclude that it has passed the next landmark, which gives some information about the progress, even if landmark 85 was omitted. The behavior changes between entering and leaving the node: you register by entering the landmark and exiting the LCA

              Node Landmarks  LCA   Steps Comments
              A    [A----]   [----] 1     -
              B    [AB---]   [A---] 1     A is LCA of A&B
     A        C    [ABC--]   [AB--] 1     B is LCA of B&C
    / \       D    [ABCD-]   [ABB-] 1     B is LCA of C&D
   /   \      E    [ABCDE]   [ABBB] 1     B is LCA of D&E
 (B)    F     F    [BDF--]   [BA--] 2     Cleanup, recompute LCAs
 /|\   /|\    G    [BDF--]   [BA--] 2     Skipped
C D E G H I   H    [BDFH-]   [BAF-] 2     F is LCA of F&H
              I    [BDFHI]   [BAFF] 2     Should be skipped, is Registered as last node of the tree (F is LCA of H&I)

Now, when performing an operation (which completely skips B), the progress bar will indicate:

  [Node] [True Progress] [Displayed] [Tracked ] [Tracked] [Comments]
                                     [Landmark] [  LCA  ]
->A           10%            0%        B          -        Tracking landmark B...
   ->B        20%           20%        D          B        Found Tracked Landmark B, Now tracking D (LCA of D,B is B)
    (B)       20%           20%        D          B        (Process skips Nodes C/D/E)
   <-B        60%           40%        F          A        Exiting LCA B, must have jumped over Landmark D, now tracking F
  A           60%           40%        F          A        -
   ->F        60%           60%        H          F        Entering Landmark F, now tracking H (LCA of F,H is F)
      ->G     70%           60%        H          F        -
      ->H     80%           80%        I          F        Found Tracked Landmark H, now tracking I (LCA if H, I is F)
      ->I     90%          100%                            Found Tracked Landmark I (last item), operation should be complete
0
source

All Articles