• Listen to a special audio message from Bill Roper to the Hive Workshop community (Bill is a former Vice President of Blizzard Entertainment, Producer, Designer, Musician, Voice Actor) 🔗Click here to hear his message!
  • Read Evilhog's interview with Gregory Alper, the original composer of the music for WarCraft: Orcs & Humans 🔗Click here to read the full interview.

[JASS] Is this loop good, or can it be done better?

Status
Not open for further replies.
It's a deep first post order loop for a tree. The tree does not point to root nodes, hence my crazy arrays =P.

JASS:
local boolean array d_1     //was one side looked inside of?
local $STRUCT$ array r_1  //root node of current node
local $STRUCT$ c_1         //current total depth
local $STRUCT$ n_1         //current node

set n_1 = $THIS$ //first initialize current node to this
set c_1 = 0         //and set depth to 0

if (n_1.in != 0) //if the tree isn't empty, loop
loop
    //go to lowest position
    loop
        //if can't go down anymore, exit
        exitwhen n_1.next == 0 and n_1.in == 0
        //go down because can't go right
        if (n_1.in == 0) then
            //if already went down, exit
            if (d_1[c_1]) then
                exitwhen true
            else
                //set went down to true
                set d_1[c_1] = true
                set c_1 = c_1 + 1    //increase depth
                set r_1[c_1] = n_1   //store current node as root of down node
                set n_1 = n_1.next   //go to down node
            endif
        //go right
        elseif (not d_1[c_1]) then
            set d_1[c_1] = true      //set went right to true
            set c_1 = c_1 + 1        //increase depth
            set r_1[c_1] = n_1       //store current node as root of right node
            set n_1 = n_1.in          //go to right node
        else
            set d_1[c_1] = false    //already went right, so go down
            set c_1 = c_1 + 1       //increase depth by 1
            set r_1[c_1] = n_1.next  //store the down node of current node as root of current node
            exitwhen true
        endif
    endloop
    
    //if the current node is actually a node and not a pointer, use it
    if (not n_1.pointer) then
        set $THIS$ = n_1
    endif
    
    //go up 1
    set d_1[c_1] = false    //first unflag
    set n_1 = r_1[c_1]      //go to root node
    set c_1 = c_1 - 1       //decrease depth by 1
    
    exitwhen n_1 == 0      //exit when the current node is null
endloop
endif

Tree
Code:
1	6
	7
	8
	9
	10

2	11
	12
	13
	14
	15

3	21
	22
	23
	24
	25

4	26
	27
	28
	29
	30

Output
Code:
10
9
8
7
6	1

15
14
13
12
11	2

20
19
18
17
16	3

25
24
23
22
21	4

30
29
28
27
26	5
 
Level 22
Joined
Sep 24, 2005
Messages
4,821
How come the output for the child nodes goes from bottom-up? Sorry just passing by, I have no knowledge about these things.
 
Level 18
Joined
Jan 21, 2006
Messages
2,552
I would say no, because JASS sucks. In addition I would support Bribe's comment as it is impossible to read your code accurately with names like d_1 and n_1. I would also like to ask what $STRUCT$ is and $THIS$ - is this a text-macro? Why not just include the definition...
 
Level 18
Joined
Jan 21, 2006
Messages
2,552
I wasn't under the impression you were posting pseudo-code. Your "pseudo-names" are more specific and explanatory than your "actual" variables. I think the confusion is fairly justified.
 
Status
Not open for further replies.
Top