Tree Drawing & Recursion

How to Draw a Tree

A Five Level Iterative Tree

A Five Level Iterative Tree

A Five Level Iterative Tree

A Five Level Iterative Tree

A Five Level Iterative Tree

A Five Level Recursive Tree

drawTree(5, up)
1 to drawTree(n, direction)
2   if n = 0 
3     draw a leaf
4   otherwise
5     draw rectangleHW(5n, n)
6     drawTree(n-1, random left)
7     drawTree(n-1,random right)

n=5

n=0

n=4

n=1

n=3

n=2

n=0

n=1

n=0

n=0

n=0

n=1

n=0

n=1

n=0

n=0

n=2

n=3

drawTree(5, up)
1 to drawTree(n, direction)
2   if n = 0 
3     draw a leaf
4   otherwise
5     draw rectangleHW(5n, n)
6     drawTree(n-1, random left)
7     drawTree(n-1,random right)
drawTree(5, d), line 6
drawTree(4, d), line 6
drawTree(3, d), line 6
drawTree(2, d), line 6
drawTree(1, d), line 6
drawTree(1, d), line 7

POP

PUSH

POP

PUSH

POP

PUSH

PUSH

PUSH

PUSH

n=5

n=4

n=3

n=2

n=1

n=0

n=3

drawTree(1, d), line 6

PUSH

n=0

1 to drawTree(5, direction)
2   if n = 0 
3     draw a leaf
4   otherwise
5     draw rectangleHW(25, 5)
6     drawTree(5-1, random left)
7     drawTree(5-1,random right)
1 to drawTree(5, direction)
2   if n = 0 
3     draw a leaf
4   otherwise
5     draw rectangleHW(25, 5)
6     drawTree(5-1, random left)
7     drawTree(5-1,random right)
1 to drawTree(5, direction)
2   if n = 0 
3     draw a leaf
4   otherwise
5     draw rectangleHW(25, 5)
6     drawTree(5-1, random left)
7     drawTree(5-1,random right)
1 to drawTree(5, direction)
2   if n = 0 
3     draw a leaf
4   otherwise
5     draw rectangleHW(25, 5)
6     drawTree(5-1, random left)
7     drawTree(5-1,random right)
1 to drawTree(5, direction)
2   if n = 0 
3     draw a leaf
4   otherwise
5     draw rectangleHW(25, 5)
6     drawTree(5-1, random left)
7     drawTree(5-1,random right)

PUSH CODE ONTO STACK

1 to drawTree(4, direction)
2   if n = 0 
3     draw a leaf
4   otherwise
5     draw rectangleHW(20, 4)
6     drawTree(4-1, random left)
7     drawTree(4-1,random right)
1 to drawTree(4, direction)
2   if n = 0 
3     draw a leaf
4   otherwise
5     draw rectangleHW(20, 4)
6     drawTree(4-1, random left)
7     drawTree(4-1,random right)
1 to drawTree(4, direction)
2   if n = 0 
3     draw a leaf
4   otherwise
5     draw rectangleHW(20, 4)
6     drawTree(4-1, random left)
7     drawTree(4-1,random right)
1 to drawTree(4, direction)
2   if n = 0 
3     draw a leaf
4   otherwise
5     draw rectangleHW(20, 4)
6     drawTree(4-1, random left)
7     drawTree(4-1,random right)
1 to drawTree(4, direction)
2   if n = 0 
3     draw a leaf
4   otherwise
5     draw rectangleHW(20, 4)
6     drawTree(4-1, random left)
7     drawTree(4-1,random right)
1 to drawTree(5, direction)
2   if n = 0 
3     draw a leaf
4   otherwise
5     draw rectangleHW(25, 5)
6     drawTree(5-1, random left)
7     drawTree(5-1,random right)
1 to drawTree(5, direction)
2   if n = 0 
3     draw a leaf
4   otherwise
5     draw rectangleHW(25, 5)
6     drawTree(5-1, random left)
7     drawTree(5-1,random right)
1 to drawTree(n, direction)
2   if n = 0 
3     draw a leaf
4   otherwise
5     draw rectangleHW(25, 5)
6     drawTree(5-1, random left)
7     drawTree(5-1,random right)
1 to drawTree(5, direction)
2   if n = 0 
3     draw a leaf
4   otherwise
5     draw rectangleHW(25, 5)
6     drawTree(5-1, random left)
7     drawTree(5-1,random right)
1 to drawTree(5, direction)
2   if n = 0 
3     draw a leaf
4   otherwise
5     draw rectangleHW(25, 5)
6     drawTree(5-1, random left)
7     drawTree(5-1,random right)

Re-enter code with N=4

Push code onto stack

1 to drawTree(4, direction)
2   if n = 0 
3     draw a leaf
4   otherwise
5     draw rectangleHW(20, 4)
6     drawTree(4-1, random left)
7     drawTree(4-1,random right)
1 to drawTree(4, direction)
2   if n = 0 
3     draw a leaf
4   otherwise
5     draw rectangleHW(20, 4)
6     drawTree(4-1, random left)
7     drawTree(4-1,random right)
1 to drawTree(4, direction)
2   if n = 0 
3     draw a leaf
4   otherwise
5     draw rectangleHW(20, 4)
6     drawTree(4-1, random left)
7     drawTree(4-1,random right)
1 to drawTree(4, direction)
2   if n = 0 
3     draw a leaf
4   otherwise
5     draw rectangleHW(20, 4)
6     drawTree(4-1, random left)
7     drawTree(4-1,random right)
1 to drawTree(4, direction)
2   if n = 0 
3     draw a leaf
4   otherwise
5     draw rectangleHW(20, 4)
6     drawTree(4-1, random left)
7     drawTree(4-1,random right)
1 to drawTree(5, direction)
2   if n = 0 
3     draw a leaf
4   otherwise
5     draw rectangleHW(25, 5)
6     drawTree(5-1, random left)
7     drawTree(5-1,random right)
1 to drawTree(5, direction)
2   if n = 0 
3     draw a leaf
4   otherwise
5     draw rectangleHW(25, 5)
6     drawTree(5-1, random left)
7     drawTree(5-1,random right)
1 to drawTree(n, direction)
2   if n = 0 
3     draw a leaf
4   otherwise
5     draw rectangleHW(25, 5)
6     drawTree(5-1, random left)
7     drawTree(5-1,random right)
1 to drawTree(5, direction)
2   if n = 0 
3     draw a leaf
4   otherwise
5     draw rectangleHW(25, 5)
6     drawTree(5-1, random left)
7     drawTree(5-1,random right)
1 to drawTree(5, direction)
2   if n = 0 
3     draw a leaf
4   otherwise
5     draw rectangleHW(25, 5)
6     drawTree(5-1, random left)
7     drawTree(5-1,random right)

Re-enter code with N=3

Push code onto stack

1 to drawTree(3, direction)
2   if n = 0 
3     draw a leaf
4   otherwise
5     draw rectangleHW(15, 3)
6     drawTree(3-1, random left)
7     drawTree(3-1,random right)
1 to drawTree(3, direction)
2   if n = 0 
3     draw a leaf
4   otherwise
5     draw rectangleHW(15, 3)
6     drawTree(3-1, random left)
7     drawTree(3-1,random right)
1 to drawTree(3, direction)
2   if n = 0 
3     draw a leaf
4   otherwise
5     draw rectangleHW(15, 3)
6     drawTree(3-1, random left)
7     drawTree(3-1,random right)
1 to drawTree(3, direction)
2   if n = 0 
3     draw a leaf
4   otherwise
5     draw rectangleHW(15, 3)
6     drawTree(3-1, random left)
7     drawTree(3-1,random right)
1 to drawTree(3, direction)
2   if n = 0 
3     draw a leaf
4   otherwise
5     draw rectangleHW(15, 3)
6     drawTree(3-1, random left)
7     drawTree(3-1,random right)
1 to drawTree(4, direction)
2   if n = 0 
3     draw a leaf
4   otherwise
5     draw rectangleHW(20, 4)
6     drawTree(4-1, random left)
7     drawTree(4-1,random right)
1 to drawTree(4, direction)
2   if n = 0 
3     draw a leaf
4   otherwise
5     draw rectangleHW(20, 4)
6     drawTree(4-1, random left)
7     drawTree(4-1,random right)
1 to drawTree(4, direction)
2   if n = 0 
3     draw a leaf
4   otherwise
5     draw rectangleHW(20, 4)
6     drawTree(4-1, random left)
7     drawTree(4-1,random right)
1 to drawTree(4, direction)
2   if n = 0 
3     draw a leaf
4   otherwise
5     draw rectangleHW(20, 4)
6     drawTree(4-1, random left)
7     drawTree(4-1,random right)
1 to drawTree(4, direction)
2   if n = 0 
3     draw a leaf
4   otherwise
5     draw rectangleHW(20, 4)
6     drawTree(4-1, random left)
7     drawTree(4-1,random right)
1 to drawTree(5, direction)
2   if n = 0 
3     draw a leaf
4   otherwise
5     draw rectangleHW(25, 5)
6     drawTree(5-1, random left)
7     drawTree(5-1,random right)
1 to drawTree(5, direction)
2   if n = 0 
3     draw a leaf
4   otherwise
5     draw rectangleHW(25, 5)
6     drawTree(5-1, random left)
7     drawTree(5-1,random right)
1 to drawTree(n, direction)
2   if n = 0 
3     draw a leaf
4   otherwise
5     draw rectangleHW(25, 5)
6     drawTree(5-1, random left)
7     drawTree(5-1,random right)
1 to drawTree(5, direction)
2   if n = 0 
3     draw a leaf
4   otherwise
5     draw rectangleHW(25, 5)
6     drawTree(5-1, random left)
7     drawTree(5-1,random right)
1 to drawTree(5, direction)
2   if n = 0 
3     draw a leaf
4   otherwise
5     draw rectangleHW(25, 5)
6     drawTree(5-1, random left)
7     drawTree(5-1,random right)

Re-enter code with N=2

Push code onto stack

1 to drawTree(3, direction)
2   if n = 0 
3     draw a leaf
4   otherwise
5     draw rectangleHW(15, 3)
6     drawTree(3-1, random left)
7     drawTree(3-1,random right)
1 to drawTree(3, direction)
2   if n = 0 
3     draw a leaf
4   otherwise
5     draw rectangleHW(15, 3)
6     drawTree(3-1, random left)
7     drawTree(3-1,random right)
1 to drawTree(3, direction)
2   if n = 0 
3     draw a leaf
4   otherwise
5     draw rectangleHW(15, 3)
6     drawTree(3-1, random left)
7     drawTree(3-1,random right)
1 to drawTree(3, direction)
2   if n = 0 
3     draw a leaf
4   otherwise
5     draw rectangleHW(15, 3)
6     drawTree(3-1, random left)
7     drawTree(3-1,random right)
1 to drawTree(3, direction)
2   if n = 0 
3     draw a leaf
4   otherwise
5     draw rectangleHW(15, 3)
6     drawTree(3-1, random left)
7     drawTree(3-1,random right)
1 to drawTree(2, direction)
2   if n = 0 
3     draw a leaf
4   otherwise
5     draw rectangleHW(10, 2)
6     drawTree(2-1, random left)
7     drawTree(2-1,random right)
1 to drawTree(2, direction)
2   if n = 0 
3     draw a leaf
4   otherwise
5     draw rectangleHW(10, 2)
6     drawTree(2-1, random left)
7     drawTree(2-1,random right)
1 to drawTree(2, direction)
2   if n = 0 
3     draw a leaf
4   otherwise
5     draw rectangleHW(10, 2)
6     drawTree(2-1, random left)
7     drawTree(2-1,random right)
1 to drawTree(2, direction)
2   if n = 0 
3     draw a leaf
4   otherwise
5     draw rectangleHW(10, 2)
6     drawTree(2-1, random left)
7     drawTree(2-1,random right)
1 to drawTree(2, direction)
2   if n = 0 
3     draw a leaf
4   otherwise
5     draw rectangleHW(10, 2)
6     drawTree(2-1, random left)
7     drawTree(2-1,random right)
1 to drawTree(4, direction)
2   if n = 0 
3     draw a leaf
4   otherwise
5     draw rectangleHW(20, 4)
6     drawTree(4-1, random left)
7     drawTree(4-1,random right)
1 to drawTree(4, direction)
2   if n = 0 
3     draw a leaf
4   otherwise
5     draw rectangleHW(20, 4)
6     drawTree(4-1, random left)
7     drawTree(4-1,random right)
1 to drawTree(4, direction)
2   if n = 0 
3     draw a leaf
4   otherwise
5     draw rectangleHW(20, 4)
6     drawTree(4-1, random left)
7     drawTree(4-1,random right)
1 to drawTree(4, direction)
2   if n = 0 
3     draw a leaf
4   otherwise
5     draw rectangleHW(20, 4)
6     drawTree(4-1, random left)
7     drawTree(4-1,random right)
1 to drawTree(4, direction)
2   if n = 0 
3     draw a leaf
4   otherwise
5     draw rectangleHW(20, 4)
6     drawTree(4-1, random left)
7     drawTree(4-1,random right)
1 to drawTree(5, direction)
2   if n = 0 
3     draw a leaf
4   otherwise
5     draw rectangleHW(25, 5)
6     drawTree(5-1, random left)
7     drawTree(5-1,random right)
1 to drawTree(5, direction)
2   if n = 0 
3     draw a leaf
4   otherwise
5     draw rectangleHW(25, 5)
6     drawTree(5-1, random left)
7     drawTree(5-1,random right)
1 to drawTree(n, direction)
2   if n = 0 
3     draw a leaf
4   otherwise
5     draw rectangleHW(25, 5)
6     drawTree(5-1, random left)
7     drawTree(5-1,random right)
1 to drawTree(5, direction)
2   if n = 0 
3     draw a leaf
4   otherwise
5     draw rectangleHW(25, 5)
6     drawTree(5-1, random left)
7     drawTree(5-1,random right)
1 to drawTree(5, direction)
2   if n = 0 
3     draw a leaf
4   otherwise
5     draw rectangleHW(25, 5)
6     drawTree(5-1, random left)
7     drawTree(5-1,random right)

Re-enter code with N=1

Push code onto stack

1 to drawTree(3, direction)
2   if n = 0 
3     draw a leaf
4   otherwise
5     draw rectangleHW(15, 3)
6     drawTree(3-1, random left)
7     drawTree(3-1,random right)
1 to drawTree(3, direction)
2   if n = 0 
3     draw a leaf
4   otherwise
5     draw rectangleHW(15, 3)
6     drawTree(3-1, random left)
7     drawTree(3-1,random right)
1 to drawTree(3, direction)
2   if n = 0 
3     draw a leaf
4   otherwise
5     draw rectangleHW(15, 3)
6     drawTree(3-1, random left)
7     drawTree(3-1,random right)
1 to drawTree(3, direction)
2   if n = 0 
3     draw a leaf
4   otherwise
5     draw rectangleHW(15, 3)
6     drawTree(3-1, random left)
7     drawTree(3-1,random right)
1 to drawTree(3, direction)
2   if n = 0 
3     draw a leaf
4   otherwise
5     draw rectangleHW(15, 3)
6     drawTree(3-1, random left)
7     drawTree(3-1,random right)
1 to drawTree(2, direction)
2   if n = 0 
3     draw a leaf
4   otherwise
5     draw rectangleHW(10, 2)
6     drawTree(2-1, random left)
7     drawTree(2-1,random right)
1 to drawTree(2, direction)
2   if n = 0 
3     draw a leaf
4   otherwise
5     draw rectangleHW(10, 2)
6     drawTree(2-1, random left)
7     drawTree(2-1,random right)
1 to drawTree(2, direction)
2   if n = 0 
3     draw a leaf
4   otherwise
5     draw rectangleHW(10, 2)
6     drawTree(2-1, random left)
7     drawTree(2-1,random right)
1 to drawTree(2, direction)
2   if n = 0 
3     draw a leaf
4   otherwise
5     draw rectangleHW(10, 2)
6     drawTree(2-1, random left)
7     drawTree(2-1,random right)
1 to drawTree(2, direction)
2   if n = 0 
3     draw a leaf
4   otherwise
5     draw rectangleHW(10, 2)
6     drawTree(2-1, random left)
7     drawTree(2-1,random right)
1 to drawTree(1, direction)
2   if n = 0 
3     draw a leaf
4   otherwise
5     draw rectangleHW(5, 1)
6     drawTree(1-1, random left)
7     drawTree(1-1,random right)
1 to drawTree(1, direction)
2   if n = 0 
3     draw a leaf
4   otherwise
5     draw rectangleHW(5, 1)
6     drawTree(1-1, random left)
7     drawTree(1-1,random right)
1 to drawTree(1, direction)
2   if n = 0 
3     draw a leaf
4   otherwise
5     draw rectangleHW(5, 1)
6     drawTree(1-1, random left)
7     drawTree(1-1,random right)
1 to drawTree(1, direction)
2   if n = 0 
3     draw a leaf
4   otherwise
5     draw rectangleHW(5, 1)
6     drawTree(1-1, random left)
7     drawTree(1-1,random right)
1 to drawTree(1, direction)
2   if n = 0 
3     draw a leaf
4   otherwise
5     draw rectangleHW(5, 1)
6     drawTree(1-1, random left)
7     drawTree(1-1,random right)
1 to drawTree(4, direction)
2   if n = 0 
3     draw a leaf
4   otherwise
5     draw rectangleHW(20, 4)
6     drawTree(4-1, random left)
7     drawTree(4-1,random right)
1 to drawTree(4, direction)
2   if n = 0 
3     draw a leaf
4   otherwise
5     draw rectangleHW(20, 4)
6     drawTree(4-1, random left)
7     drawTree(4-1,random right)
1 to drawTree(4, direction)
2   if n = 0 
3     draw a leaf
4   otherwise
5     draw rectangleHW(20, 4)
6     drawTree(4-1, random left)
7     drawTree(4-1,random right)
1 to drawTree(4, direction)
2   if n = 0 
3     draw a leaf
4   otherwise
5     draw rectangleHW(20, 4)
6     drawTree(4-1, random left)
7     drawTree(4-1,random right)
1 to drawTree(4, direction)
2   if n = 0 
3     draw a leaf
4   otherwise
5     draw rectangleHW(20, 4)
6     drawTree(4-1, random left)
7     drawTree(4-1,random right)
1 to drawTree(5, direction)
2   if n = 0 
3     draw a leaf
4   otherwise
5     draw rectangleHW(25, 5)
6     drawTree(5-1, random left)
7     drawTree(5-1,random right)
1 to drawTree(5, direction)
2   if n = 0 
3     draw a leaf
4   otherwise
5     draw rectangleHW(25, 5)
6     drawTree(5-1, random left)
7     drawTree(5-1,random right)
1 to drawTree(n, direction)
2   if n = 0 
3     draw a leaf
4   otherwise
5     draw rectangleHW(25, 5)
6     drawTree(5-1, random left)
7     drawTree(5-1,random right)
1 to drawTree(5, direction)
2   if n = 0 
3     draw a leaf
4   otherwise
5     draw rectangleHW(25, 5)
6     drawTree(5-1, random left)
7     drawTree(5-1,random right)
1 to drawTree(5, direction)
2   if n = 0 
3     draw a leaf
4   otherwise
5     draw rectangleHW(25, 5)
6     drawTree(5-1, random left)
7     drawTree(5-1,random right)

Re-enter code with N=0

Exit and pop code from stack

1 to drawTree(3, direction)
2   if n = 0 
3     draw a leaf
4   otherwise
5     draw rectangleHW(15, 3)
6     drawTree(3-1, random left)
7     drawTree(3-1,random right)
1 to drawTree(3, direction)
2   if n = 0 
3     draw a leaf
4   otherwise
5     draw rectangleHW(15, 3)
6     drawTree(3-1, random left)
7     drawTree(3-1,random right)
1 to drawTree(3, direction)
2   if n = 0 
3     draw a leaf
4   otherwise
5     draw rectangleHW(15, 3)
6     drawTree(3-1, random left)
7     drawTree(3-1,random right)
1 to drawTree(3, direction)
2   if n = 0 
3     draw a leaf
4   otherwise
5     draw rectangleHW(15, 3)
6     drawTree(3-1, random left)
7     drawTree(3-1,random right)
1 to drawTree(3, direction)
2   if n = 0 
3     draw a leaf
4   otherwise
5     draw rectangleHW(15, 3)
6     drawTree(3-1, random left)
7     drawTree(3-1,random right)
1 to drawTree(2, direction)
2   if n = 0 
3     draw a leaf
4   otherwise
5     draw rectangleHW(10, 2)
6     drawTree(2-1, random left)
7     drawTree(2-1,random right)
1 to drawTree(2, direction)
2   if n = 0 
3     draw a leaf
4   otherwise
5     draw rectangleHW(10, 2)
6     drawTree(2-1, random left)
7     drawTree(2-1,random right)
1 to drawTree(2, direction)
2   if n = 0 
3     draw a leaf
4   otherwise
5     draw rectangleHW(10, 2)
6     drawTree(2-1, random left)
7     drawTree(2-1,random right)
1 to drawTree(2, direction)
2   if n = 0 
3     draw a leaf
4   otherwise
5     draw rectangleHW(10, 2)
6     drawTree(2-1, random left)
7     drawTree(2-1,random right)
1 to drawTree(2, direction)
2   if n = 0 
3     draw a leaf
4   otherwise
5     draw rectangleHW(10, 2)
6     drawTree(2-1, random left)
7     drawTree(2-1,random right)
1 to drawTree(1, direction)
2   if n = 0 
3     draw a leaf
4   otherwise
5     draw rectangleHW(5, 1)
6     drawTree(1-1, random left)
7     drawTree(1-1,random right)
1 to drawTree(1, direction)
2   if n = 0 
3     draw a leaf
4   otherwise
5     draw rectangleHW(5, 1)
6     drawTree(1-1, random left)
7     drawTree(1-1,random right)
1 to drawTree(1, direction)
2   if n = 0 
3     draw a leaf
4   otherwise
5     draw rectangleHW(5, 1)
6     drawTree(1-1, random left)
7     drawTree(1-1,random right)
1 to drawTree(1, direction)
2   if n = 0 
3     draw a leaf
4   otherwise
5     draw rectangleHW(5, 1)
6     drawTree(1-1, random left)
7     drawTree(1-1,random right)
1 to drawTree(1, direction)
2   if n = 0 
3     draw a leaf
4   otherwise
5     draw rectangleHW(5, 1)
6     drawTree(1-1, random left)
7     drawTree(1-1,random right)
1 to drawTree(0, direction)
2   if n = 0 
3     draw a leaf
4   otherwise
5     draw rectangleHW(0, 0)
6     drawTree(0-1, random left)
7     drawTree(0-1,random right)
1 to drawTree(0, direction)
2   if n = 0 
3     draw a leaf
4   otherwise
5     draw rectangleHW(0, 1)
6     drawTree(0-1, random left)
7     drawTree(0-1,random right)
1 to drawTree(4, direction)
2   if n = 0 
3     draw a leaf
4   otherwise
5     draw rectangleHW(20, 4)
6     drawTree(4-1, random left)
7     drawTree(4-1,random right)
1 to drawTree(4, direction)
2   if n = 0 
3     draw a leaf
4   otherwise
5     draw rectangleHW(20, 4)
6     drawTree(4-1, random left)
7     drawTree(4-1,random right)
1 to drawTree(4, direction)
2   if n = 0 
3     draw a leaf
4   otherwise
5     draw rectangleHW(20, 4)
6     drawTree(4-1, random left)
7     drawTree(4-1,random right)
1 to drawTree(4, direction)
2   if n = 0 
3     draw a leaf
4   otherwise
5     draw rectangleHW(20, 4)
6     drawTree(4-1, random left)
7     drawTree(4-1,random right)
1 to drawTree(4, direction)
2   if n = 0 
3     draw a leaf
4   otherwise
5     draw rectangleHW(20, 4)
6     drawTree(4-1, random left)
7     drawTree(4-1,random right)
1 to drawTree(5, direction)
2   if n = 0 
3     draw a leaf
4   otherwise
5     draw rectangleHW(25, 5)
6     drawTree(5-1, random left)
7     drawTree(5-1,random right)
1 to drawTree(5, direction)
2   if n = 0 
3     draw a leaf
4   otherwise
5     draw rectangleHW(25, 5)
6     drawTree(5-1, random left)
7     drawTree(5-1,random right)
1 to drawTree(n, direction)
2   if n = 0 
3     draw a leaf
4   otherwise
5     draw rectangleHW(25, 5)
6     drawTree(5-1, random left)
7     drawTree(5-1,random right)
1 to drawTree(5, direction)
2   if n = 0 
3     draw a leaf
4   otherwise
5     draw rectangleHW(25, 5)
6     drawTree(5-1, random left)
7     drawTree(5-1,random right)
1 to drawTree(5, direction)
2   if n = 0 
3     draw a leaf
4   otherwise
5     draw rectangleHW(25, 5)
6     drawTree(5-1, random left)
7     drawTree(5-1,random right)

Resume code with N=1

Push code onto stack

1 to drawTree(3, direction)
2   if n = 0 
3     draw a leaf
4   otherwise
5     draw rectangleHW(15, 3)
6     drawTree(3-1, random left)
7     drawTree(3-1,random right)
1 to drawTree(3, direction)
2   if n = 0 
3     draw a leaf
4   otherwise
5     draw rectangleHW(15, 3)
6     drawTree(3-1, random left)
7     drawTree(3-1,random right)
1 to drawTree(3, direction)
2   if n = 0 
3     draw a leaf
4   otherwise
5     draw rectangleHW(15, 3)
6     drawTree(3-1, random left)
7     drawTree(3-1,random right)
1 to drawTree(3, direction)
2   if n = 0 
3     draw a leaf
4   otherwise
5     draw rectangleHW(15, 3)
6     drawTree(3-1, random left)
7     drawTree(3-1,random right)
1 to drawTree(3, direction)
2   if n = 0 
3     draw a leaf
4   otherwise
5     draw rectangleHW(15, 3)
6     drawTree(3-1, random left)
7     drawTree(3-1,random right)
1 to drawTree(2, direction)
2   if n = 0 
3     draw a leaf
4   otherwise
5     draw rectangleHW(10, 2)
6     drawTree(2-1, random left)
7     drawTree(2-1,random right)
1 to drawTree(2, direction)
2   if n = 0 
3     draw a leaf
4   otherwise
5     draw rectangleHW(10, 2)
6     drawTree(2-1, random left)
7     drawTree(2-1,random right)
1 to drawTree(2, direction)
2   if n = 0 
3     draw a leaf
4   otherwise
5     draw rectangleHW(10, 2)
6     drawTree(2-1, random left)
7     drawTree(2-1,random right)
1 to drawTree(2, direction)
2   if n = 0 
3     draw a leaf
4   otherwise
5     draw rectangleHW(10, 2)
6     drawTree(2-1, random left)
7     drawTree(2-1,random right)
1 to drawTree(2, direction)
2   if n = 0 
3     draw a leaf
4   otherwise
5     draw rectangleHW(10, 2)
6     drawTree(2-1, random left)
7     drawTree(2-1,random right)
1 to drawTree(1, direction)
2   if n = 0 
3     draw a leaf
4   otherwise
5     draw rectangleHW(5, 1)
6     drawTree(1-1, random left)
7     drawTree(1-1,random right)
1 to drawTree(1, direction)
2   if n = 0 
3     draw a leaf
4   otherwise
5     draw rectangleHW(5, 1)
6     drawTree(1-1, random left)
7     drawTree(1-1,random right)
1 to drawTree(1, direction)
2   if n = 0 
3     draw a leaf
4   otherwise
5     draw rectangleHW(5, 1)
6     drawTree(1-1, random left)
7     drawTree(1-1,random right)
1 to drawTree(1, direction)
2   if n = 0 
3     draw a leaf
4   otherwise
5     draw rectangleHW(5, 1)
6     drawTree(1-1, random left)
7     drawTree(1-1,random right)
1 to drawTree(1, direction)
2   if n = 0 
3     draw a leaf
4   otherwise
5     draw rectangleHW(5, 1)
6     drawTree(1-1, random left)
7     drawTree(1-1,random right)
1 to drawTree(4, direction)
2   if n = 0 
3     draw a leaf
4   otherwise
5     draw rectangleHW(20, 4)
6     drawTree(4-1, random left)
7     drawTree(4-1,random right)
1 to drawTree(4, direction)
2   if n = 0 
3     draw a leaf
4   otherwise
5     draw rectangleHW(20, 4)
6     drawTree(4-1, random left)
7     drawTree(4-1,random right)
1 to drawTree(4, direction)
2   if n = 0 
3     draw a leaf
4   otherwise
5     draw rectangleHW(20, 4)
6     drawTree(4-1, random left)
7     drawTree(4-1,random right)
1 to drawTree(4, direction)
2   if n = 0 
3     draw a leaf
4   otherwise
5     draw rectangleHW(20, 4)
6     drawTree(4-1, random left)
7     drawTree(4-1,random right)
1 to drawTree(4, direction)
2   if n = 0 
3     draw a leaf
4   otherwise
5     draw rectangleHW(20, 4)
6     drawTree(4-1, random left)
7     drawTree(4-1,random right)
1 to drawTree(5, direction)
2   if n = 0 
3     draw a leaf
4   otherwise
5     draw rectangleHW(25, 5)
6     drawTree(5-1, random left)
7     drawTree(5-1,random right)
1 to drawTree(5, direction)
2   if n = 0 
3     draw a leaf
4   otherwise
5     draw rectangleHW(25, 5)
6     drawTree(5-1, random left)
7     drawTree(5-1,random right)
1 to drawTree(n, direction)
2   if n = 0 
3     draw a leaf
4   otherwise
5     draw rectangleHW(25, 5)
6     drawTree(5-1, random left)
7     drawTree(5-1,random right)
1 to drawTree(5, direction)
2   if n = 0 
3     draw a leaf
4   otherwise
5     draw rectangleHW(25, 5)
6     drawTree(5-1, random left)
7     drawTree(5-1,random right)
1 to drawTree(5, direction)
2   if n = 0 
3     draw a leaf
4   otherwise
5     draw rectangleHW(25, 5)
6     drawTree(5-1, random left)
7     drawTree(5-1,random right)
1 to drawTree(3, direction)
2   if n = 0 
3     draw a leaf
4   otherwise
5     draw rectangleHW(15, 3)
6     drawTree(3-1, random left)
7     drawTree(3-1,random right)
1 to drawTree(3, direction)
2   if n = 0 
3     draw a leaf
4   otherwise
5     draw rectangleHW(15, 3)
6     drawTree(3-1, random left)
7     drawTree(3-1,random right)
1 to drawTree(3, direction)
2   if n = 0 
3     draw a leaf
4   otherwise
5     draw rectangleHW(15, 3)
6     drawTree(3-1, random left)
7     drawTree(3-1,random right)
1 to drawTree(3, direction)
2   if n = 0 
3     draw a leaf
4   otherwise
5     draw rectangleHW(15, 3)
6     drawTree(3-1, random left)
7     drawTree(3-1,random right)
1 to drawTree(3, direction)
2   if n = 0 
3     draw a leaf
4   otherwise
5     draw rectangleHW(15, 3)
6     drawTree(3-1, random left)
7     drawTree(3-1,random right)
1 to drawTree(2, direction)
2   if n = 0 
3     draw a leaf
4   otherwise
5     draw rectangleHW(10, 2)
6     drawTree(2-1, random left)
7     drawTree(2-1,random right)
1 to drawTree(2, direction)
2   if n = 0 
3     draw a leaf
4   otherwise
5     draw rectangleHW(10, 2)
6     drawTree(2-1, random left)
7     drawTree(2-1,random right)
1 to drawTree(2, direction)
2   if n = 0 
3     draw a leaf
4   otherwise
5     draw rectangleHW(10, 2)
6     drawTree(2-1, random left)
7     drawTree(2-1,random right)
1 to drawTree(2, direction)
2   if n = 0 
3     draw a leaf
4   otherwise
5     draw rectangleHW(10, 2)
6     drawTree(2-1, random left)
7     drawTree(2-1,random right)
1 to drawTree(2, direction)
2   if n = 0 
3     draw a leaf
4   otherwise
5     draw rectangleHW(10, 2)
6     drawTree(2-1, random left)
7     drawTree(2-1,random right)
1 to drawTree(1, direction)
2   if n = 0 
3     draw a leaf
4   otherwise
5     draw rectangleHW(5, 1)
6     drawTree(1-1, random left)
7     drawTree(1-1,random right)
1 to drawTree(1, direction)
2   if n = 0 
3     draw a leaf
4   otherwise
5     draw rectangleHW(5, 1)
6     drawTree(1-1, random left)
7     drawTree(1-1,random right)
1 to drawTree(1, direction)
2   if n = 0 
3     draw a leaf
4   otherwise
5     draw rectangleHW(5, 1)
6     drawTree(1-1, random left)
7     drawTree(1-1,random right)
1 to drawTree(1, direction)
2   if n = 0 
3     draw a leaf
4   otherwise
5     draw rectangleHW(5, 1)
6     drawTree(1-1, random left)
7     drawTree(1-1,random right)
1 to drawTree(1, direction)
2   if n = 0 
3     draw a leaf
4   otherwise
5     draw rectangleHW(5, 1)
6     drawTree(1-1, random left)
7     drawTree(1-1,random right)

Re-enter code with N=0

Exit and pop code from stack

1 to drawTree(0, direction)
2   if n = 0 
3     draw a leaf
4   otherwise
5     draw rectangleHW(0, 0)
6     drawTree(0-1, random left)
7     drawTree(0-1,random right)
1 to drawTree(0, direction)
2   if n = 0 
3     draw a leaf
4   otherwise
5     draw rectangleHW(0, 1)
6     drawTree(0-1, random left)
7     drawTree(0-1,random right)
1 to drawTree(4, direction)
2   if n = 0 
3     draw a leaf
4   otherwise
5     draw rectangleHW(20, 4)
6     drawTree(4-1, random left)
7     drawTree(4-1,random right)
1 to drawTree(4, direction)
2   if n = 0 
3     draw a leaf
4   otherwise
5     draw rectangleHW(20, 4)
6     drawTree(4-1, random left)
7     drawTree(4-1,random right)
1 to drawTree(4, direction)
2   if n = 0 
3     draw a leaf
4   otherwise
5     draw rectangleHW(20, 4)
6     drawTree(4-1, random left)
7     drawTree(4-1,random right)
1 to drawTree(4, direction)
2   if n = 0 
3     draw a leaf
4   otherwise
5     draw rectangleHW(20, 4)
6     drawTree(4-1, random left)
7     drawTree(4-1,random right)
1 to drawTree(4, direction)
2   if n = 0 
3     draw a leaf
4   otherwise
5     draw rectangleHW(20, 4)
6     drawTree(4-1, random left)
7     drawTree(4-1,random right)
1 to drawTree(5, direction)
2   if n = 0 
3     draw a leaf
4   otherwise
5     draw rectangleHW(25, 5)
6     drawTree(5-1, random left)
7     drawTree(5-1,random right)
1 to drawTree(5, direction)
2   if n = 0 
3     draw a leaf
4   otherwise
5     draw rectangleHW(25, 5)
6     drawTree(5-1, random left)
7     drawTree(5-1,random right)
1 to drawTree(n, direction)
2   if n = 0 
3     draw a leaf
4   otherwise
5     draw rectangleHW(25, 5)
6     drawTree(5-1, random left)
7     drawTree(5-1,random right)
1 to drawTree(5, direction)
2   if n = 0 
3     draw a leaf
4   otherwise
5     draw rectangleHW(25, 5)
6     drawTree(5-1, random left)
7     drawTree(5-1,random right)
1 to drawTree(5, direction)
2   if n = 0 
3     draw a leaf
4   otherwise
5     draw rectangleHW(25, 5)
6     drawTree(5-1, random left)
7     drawTree(5-1,random right)
1 to drawTree(3, direction)
2   if n = 0 
3     draw a leaf
4   otherwise
5     draw rectangleHW(15, 3)
6     drawTree(3-1, random left)
7     drawTree(3-1,random right)
1 to drawTree(3, direction)
2   if n = 0 
3     draw a leaf
4   otherwise
5     draw rectangleHW(15, 3)
6     drawTree(3-1, random left)
7     drawTree(3-1,random right)
1 to drawTree(3, direction)
2   if n = 0 
3     draw a leaf
4   otherwise
5     draw rectangleHW(15, 3)
6     drawTree(3-1, random left)
7     drawTree(3-1,random right)
1 to drawTree(3, direction)
2   if n = 0 
3     draw a leaf
4   otherwise
5     draw rectangleHW(15, 3)
6     drawTree(3-1, random left)
7     drawTree(3-1,random right)
1 to drawTree(3, direction)
2   if n = 0 
3     draw a leaf
4   otherwise
5     draw rectangleHW(15, 3)
6     drawTree(3-1, random left)
7     drawTree(3-1,random right)
1 to drawTree(2, direction)
2   if n = 0 
3     draw a leaf
4   otherwise
5     draw rectangleHW(10, 2)
6     drawTree(2-1, random left)
7     drawTree(2-1,random right)
1 to drawTree(2, direction)
2   if n = 0 
3     draw a leaf
4   otherwise
5     draw rectangleHW(10, 2)
6     drawTree(2-1, random left)
7     drawTree(2-1,random right)
1 to drawTree(2, direction)
2   if n = 0 
3     draw a leaf
4   otherwise
5     draw rectangleHW(10, 2)
6     drawTree(2-1, random left)
7     drawTree(2-1,random right)
1 to drawTree(2, direction)
2   if n = 0 
3     draw a leaf
4   otherwise
5     draw rectangleHW(10, 2)
6     drawTree(2-1, random left)
7     drawTree(2-1,random right)
1 to drawTree(2, direction)
2   if n = 0 
3     draw a leaf
4   otherwise
5     draw rectangleHW(10, 2)
6     drawTree(2-1, random left)
7     drawTree(2-1,random right)
1 to drawTree(1, direction)
2   if n = 0 
3     draw a leaf
4   otherwise
5     draw rectangleHW(5, 1)
6     drawTree(1-1, random left)
7     drawTree(1-1,random right)
1 to drawTree(1, direction)
2   if n = 0 
3     draw a leaf
4   otherwise
5     draw rectangleHW(5, 1)
6     drawTree(1-1, random left)
7     drawTree(1-1,random right)
1 to drawTree(1, direction)
2   if n = 0 
3     draw a leaf
4   otherwise
5     draw rectangleHW(5, 1)
6     drawTree(1-1, random left)
7     drawTree(1-1,random right)
1 to drawTree(1, direction)
2   if n = 0 
3     draw a leaf
4   otherwise
5     draw rectangleHW(5, 1)
6     drawTree(1-1, random left)
7     drawTree(1-1,random right)
1 to drawTree(1, direction)
2   if n = 0 
3     draw a leaf
4   otherwise
5     draw rectangleHW(5, 1)
6     drawTree(1-1, random left)
7     drawTree(1-1,random right)

Resume code with N=1

Exit and pop code from stack

1 to drawTree(4, direction)
2   if n = 0 
3     draw a leaf
4   otherwise
5     draw rectangleHW(20, 4)
6     drawTree(4-1, random left)
7     drawTree(4-1,random right)
1 to drawTree(4, direction)
2   if n = 0 
3     draw a leaf
4   otherwise
5     draw rectangleHW(20, 4)
6     drawTree(4-1, random left)
7     drawTree(4-1,random right)
1 to drawTree(4, direction)
2   if n = 0 
3     draw a leaf
4   otherwise
5     draw rectangleHW(20, 4)
6     drawTree(4-1, random left)
7     drawTree(4-1,random right)
1 to drawTree(4, direction)
2   if n = 0 
3     draw a leaf
4   otherwise
5     draw rectangleHW(20, 4)
6     drawTree(4-1, random left)
7     drawTree(4-1,random right)
1 to drawTree(4, direction)
2   if n = 0 
3     draw a leaf
4   otherwise
5     draw rectangleHW(20, 4)
6     drawTree(4-1, random left)
7     drawTree(4-1,random right)
1 to drawTree(5, direction)
2   if n = 0 
3     draw a leaf
4   otherwise
5     draw rectangleHW(25, 5)
6     drawTree(5-1, random left)
7     drawTree(5-1,random right)
1 to drawTree(5, direction)
2   if n = 0 
3     draw a leaf
4   otherwise
5     draw rectangleHW(25, 5)
6     drawTree(5-1, random left)
7     drawTree(5-1,random right)
1 to drawTree(n, direction)
2   if n = 0 
3     draw a leaf
4   otherwise
5     draw rectangleHW(25, 5)
6     drawTree(5-1, random left)
7     drawTree(5-1,random right)
1 to drawTree(5, direction)
2   if n = 0 
3     draw a leaf
4   otherwise
5     draw rectangleHW(25, 5)
6     drawTree(5-1, random left)
7     drawTree(5-1,random right)
1 to drawTree(5, direction)
2   if n = 0 
3     draw a leaf
4   otherwise
5     draw rectangleHW(25, 5)
6     drawTree(5-1, random left)
7     drawTree(5-1,random right)
1 to drawTree(3, direction)
2   if n = 0 
3     draw a leaf
4   otherwise
5     draw rectangleHW(15, 3)
6     drawTree(3-1, random left)
7     drawTree(3-1,random right)
1 to drawTree(3, direction)
2   if n = 0 
3     draw a leaf
4   otherwise
5     draw rectangleHW(15, 3)
6     drawTree(3-1, random left)
7     drawTree(3-1,random right)
1 to drawTree(3, direction)
2   if n = 0 
3     draw a leaf
4   otherwise
5     draw rectangleHW(15, 3)
6     drawTree(3-1, random left)
7     drawTree(3-1,random right)
1 to drawTree(3, direction)
2   if n = 0 
3     draw a leaf
4   otherwise
5     draw rectangleHW(15, 3)
6     drawTree(3-1, random left)
7     drawTree(3-1,random right)
1 to drawTree(3, direction)
2   if n = 0 
3     draw a leaf
4   otherwise
5     draw rectangleHW(15, 3)
6     drawTree(3-1, random left)
7     drawTree(3-1,random right)
1 to drawTree(2, direction)
2   if n = 0 
3     draw a leaf
4   otherwise
5     draw rectangleHW(10, 2)
6     drawTree(2-1, random left)
7     drawTree(2-1,random right)
1 to drawTree(2, direction)
2   if n = 0 
3     draw a leaf
4   otherwise
5     draw rectangleHW(10, 2)
6     drawTree(2-1, random left)
7     drawTree(2-1,random right)
1 to drawTree(2, direction)
2   if n = 0 
3     draw a leaf
4   otherwise
5     draw rectangleHW(10, 2)
6     drawTree(2-1, random left)
7     drawTree(2-1,random right)
1 to drawTree(2, direction)
2   if n = 0 
3     draw a leaf
4   otherwise
5     draw rectangleHW(10, 2)
6     drawTree(2-1, random left)
7     drawTree(2-1,random right)
1 to drawTree(2, direction)
2   if n = 0 
3     draw a leaf
4   otherwise
5     draw rectangleHW(10, 2)
6     drawTree(2-1, random left)
7     drawTree(2-1,random right)

Resume code with N=2

Push code onto stack

1 to drawTree(4, direction)
2   if n = 0 
3     draw a leaf
4   otherwise
5     draw rectangleHW(20, 4)
6     drawTree(4-1, random left)
7     drawTree(4-1,random right)
1 to drawTree(4, direction)
2   if n = 0 
3     draw a leaf
4   otherwise
5     draw rectangleHW(20, 4)
6     drawTree(4-1, random left)
7     drawTree(4-1,random right)
1 to drawTree(4, direction)
2   if n = 0 
3     draw a leaf
4   otherwise
5     draw rectangleHW(20, 4)
6     drawTree(4-1, random left)
7     drawTree(4-1,random right)
1 to drawTree(4, direction)
2   if n = 0 
3     draw a leaf
4   otherwise
5     draw rectangleHW(20, 4)
6     drawTree(4-1, random left)
7     drawTree(4-1,random right)
1 to drawTree(4, direction)
2   if n = 0 
3     draw a leaf
4   otherwise
5     draw rectangleHW(20, 4)
6     drawTree(4-1, random left)
7     drawTree(4-1,random right)
1 to drawTree(5, direction)
2   if n = 0 
3     draw a leaf
4   otherwise
5     draw rectangleHW(25, 5)
6     drawTree(5-1, random left)
7     drawTree(5-1,random right)
1 to drawTree(5, direction)
2   if n = 0 
3     draw a leaf
4   otherwise
5     draw rectangleHW(25, 5)
6     drawTree(5-1, random left)
7     drawTree(5-1,random right)
1 to drawTree(n, direction)
2   if n = 0 
3     draw a leaf
4   otherwise
5     draw rectangleHW(25, 5)
6     drawTree(5-1, random left)
7     drawTree(5-1,random right)
1 to drawTree(5, direction)
2   if n = 0 
3     draw a leaf
4   otherwise
5     draw rectangleHW(25, 5)
6     drawTree(5-1, random left)
7     drawTree(5-1,random right)
1 to drawTree(5, direction)
2   if n = 0 
3     draw a leaf
4   otherwise
5     draw rectangleHW(25, 5)
6     drawTree(5-1, random left)
7     drawTree(5-1,random right)
1 to drawTree(3, direction)
2   if n = 0 
3     draw a leaf
4   otherwise
5     draw rectangleHW(15, 3)
6     drawTree(3-1, random left)
7     drawTree(3-1,random right)
1 to drawTree(3, direction)
2   if n = 0 
3     draw a leaf
4   otherwise
5     draw rectangleHW(15, 3)
6     drawTree(3-1, random left)
7     drawTree(3-1,random right)
1 to drawTree(3, direction)
2   if n = 0 
3     draw a leaf
4   otherwise
5     draw rectangleHW(15, 3)
6     drawTree(3-1, random left)
7     drawTree(3-1,random right)
1 to drawTree(3, direction)
2   if n = 0 
3     draw a leaf
4   otherwise
5     draw rectangleHW(15, 3)
6     drawTree(3-1, random left)
7     drawTree(3-1,random right)
1 to drawTree(3, direction)
2   if n = 0 
3     draw a leaf
4   otherwise
5     draw rectangleHW(15, 3)
6     drawTree(3-1, random left)
7     drawTree(3-1,random right)
1 to drawTree(2, direction)
2   if n = 0 
3     draw a leaf
4   otherwise
5     draw rectangleHW(10, 2)
6     drawTree(2-1, random left)
7     drawTree(2-1,random right)
1 to drawTree(2, direction)
2   if n = 0 
3     draw a leaf
4   otherwise
5     draw rectangleHW(10, 2)
6     drawTree(2-1, random left)
7     drawTree(2-1,random right)
1 to drawTree(2, direction)
2   if n = 0 
3     draw a leaf
4   otherwise
5     draw rectangleHW(10, 2)
6     drawTree(2-1, random left)
7     drawTree(2-1,random right)
1 to drawTree(2, direction)
2   if n = 0 
3     draw a leaf
4   otherwise
5     draw rectangleHW(10, 2)
6     drawTree(2-1, random left)
7     drawTree(2-1,random right)
1 to drawTree(2, direction)
2   if n = 0 
3     draw a leaf
4   otherwise
5     draw rectangleHW(10, 2)
6     drawTree(2-1, random left)
7     drawTree(2-1,random right)

Re-enter code with N=1

Push code onto stack

1 to drawTree(1, direction)
2   if n = 0 
3     draw a leaf
4   otherwise
5     draw rectangleHW(5, 1)
6     drawTree(1-1, random left)
7     drawTree(1-1,random right)
1 to drawTree(1, direction)
2   if n = 0 
3     draw a leaf
4   otherwise
5     draw rectangleHW(5, 1)
6     drawTree(1-1, random left)
7     drawTree(1-1,random right)
1 to drawTree(1, direction)
2   if n = 0 
3     draw a leaf
4   otherwise
5     draw rectangleHW(5, 1)
6     drawTree(1-1, random left)
7     drawTree(1-1,random right)
1 to drawTree(1, direction)
2   if n = 0 
3     draw a leaf
4   otherwise
5     draw rectangleHW(5, 1)
6     drawTree(1-1, random left)
7     drawTree(1-1,random right)
1 to drawTree(1, direction)
2   if n = 0 
3     draw a leaf
4   otherwise
5     draw rectangleHW(5, 1)
6     drawTree(1-1, random left)
7     drawTree(1-1,random right)
1 to drawTree(4, direction)
2   if n = 0 
3     draw a leaf
4   otherwise
5     draw rectangleHW(20, 4)
6     drawTree(4-1, random left)
7     drawTree(4-1,random right)
1 to drawTree(4, direction)
2   if n = 0 
3     draw a leaf
4   otherwise
5     draw rectangleHW(20, 4)
6     drawTree(4-1, random left)
7     drawTree(4-1,random right)
1 to drawTree(4, direction)
2   if n = 0 
3     draw a leaf
4   otherwise
5     draw rectangleHW(20, 4)
6     drawTree(4-1, random left)
7     drawTree(4-1,random right)
1 to drawTree(4, direction)
2   if n = 0 
3     draw a leaf
4   otherwise
5     draw rectangleHW(20, 4)
6     drawTree(4-1, random left)
7     drawTree(4-1,random right)
1 to drawTree(4, direction)
2   if n = 0 
3     draw a leaf
4   otherwise
5     draw rectangleHW(20, 4)
6     drawTree(4-1, random left)
7     drawTree(4-1,random right)
1 to drawTree(5, direction)
2   if n = 0 
3     draw a leaf
4   otherwise
5     draw rectangleHW(25, 5)
6     drawTree(5-1, random left)
7     drawTree(5-1,random right)
1 to drawTree(5, direction)
2   if n = 0 
3     draw a leaf
4   otherwise
5     draw rectangleHW(25, 5)
6     drawTree(5-1, random left)
7     drawTree(5-1,random right)
1 to drawTree(n, direction)
2   if n = 0 
3     draw a leaf
4   otherwise
5     draw rectangleHW(25, 5)
6     drawTree(5-1, random left)
7     drawTree(5-1,random right)
1 to drawTree(5, direction)
2   if n = 0 
3     draw a leaf
4   otherwise
5     draw rectangleHW(25, 5)
6     drawTree(5-1, random left)
7     drawTree(5-1,random right)
1 to drawTree(5, direction)
2   if n = 0 
3     draw a leaf
4   otherwise
5     draw rectangleHW(25, 5)
6     drawTree(5-1, random left)
7     drawTree(5-1,random right)
1 to drawTree(3, direction)
2   if n = 0 
3     draw a leaf
4   otherwise
5     draw rectangleHW(15, 3)
6     drawTree(3-1, random left)
7     drawTree(3-1,random right)
1 to drawTree(3, direction)
2   if n = 0 
3     draw a leaf
4   otherwise
5     draw rectangleHW(15, 3)
6     drawTree(3-1, random left)
7     drawTree(3-1,random right)
1 to drawTree(3, direction)
2   if n = 0 
3     draw a leaf
4   otherwise
5     draw rectangleHW(15, 3)
6     drawTree(3-1, random left)
7     drawTree(3-1,random right)
1 to drawTree(3, direction)
2   if n = 0 
3     draw a leaf
4   otherwise
5     draw rectangleHW(15, 3)
6     drawTree(3-1, random left)
7     drawTree(3-1,random right)
1 to drawTree(3, direction)
2   if n = 0 
3     draw a leaf
4   otherwise
5     draw rectangleHW(15, 3)
6     drawTree(3-1, random left)
7     drawTree(3-1,random right)
1 to drawTree(2, direction)
2   if n = 0 
3     draw a leaf
4   otherwise
5     draw rectangleHW(10, 2)
6     drawTree(2-1, random left)
7     drawTree(2-1,random right)
1 to drawTree(2, direction)
2   if n = 0 
3     draw a leaf
4   otherwise
5     draw rectangleHW(10, 2)
6     drawTree(2-1, random left)
7     drawTree(2-1,random right)
1 to drawTree(2, direction)
2   if n = 0 
3     draw a leaf
4   otherwise
5     draw rectangleHW(10, 2)
6     drawTree(2-1, random left)
7     drawTree(2-1,random right)
1 to drawTree(2, direction)
2   if n = 0 
3     draw a leaf
4   otherwise
5     draw rectangleHW(10, 2)
6     drawTree(2-1, random left)
7     drawTree(2-1,random right)
1 to drawTree(2, direction)
2   if n = 0 
3     draw a leaf
4   otherwise
5     draw rectangleHW(10, 2)
6     drawTree(2-1, random left)
7     drawTree(2-1,random right)

Re-enter code with N=0

1 to drawTree(1, direction)
2   if n = 0 
3     draw a leaf
4   otherwise
5     draw rectangleHW(5, 1)
6     drawTree(1-1, random left)
7     drawTree(1-1,random right)
1 to drawTree(1, direction)
2   if n = 0 
3     draw a leaf
4   otherwise
5     draw rectangleHW(5, 1)
6     drawTree(1-1, random left)
7     drawTree(1-1,random right)
1 to drawTree(1, direction)
2   if n = 0 
3     draw a leaf
4   otherwise
5     draw rectangleHW(5, 1)
6     drawTree(1-1, random left)
7     drawTree(1-1,random right)
1 to drawTree(1, direction)
2   if n = 0 
3     draw a leaf
4   otherwise
5     draw rectangleHW(5, 1)
6     drawTree(1-1, random left)
7     drawTree(1-1,random right)
1 to drawTree(1, direction)
2   if n = 0 
3     draw a leaf
4   otherwise
5     draw rectangleHW(5, 1)
6     drawTree(1-1, random left)
7     drawTree(1-1,random right)

Exit and pop code from stack

1 to drawTree(0, direction)
2   if n = 0 
3     draw a leaf
4   otherwise
5     draw rectangleHW(0, 0)
6     drawTree(0-1, random left)
7     drawTree(0-1,random right)
1 to drawTree(0, direction)
2   if n = 0 
3     draw a leaf
4   otherwise
5     draw rectangleHW(0, 1)
6     drawTree(0-1, random left)
7     drawTree(0-1,random right)
1 to drawTree(4, direction)
2   if n = 0 
3     draw a leaf
4   otherwise
5     draw rectangleHW(20, 4)
6     drawTree(4-1, random left)
7     drawTree(4-1,random right)
1 to drawTree(4, direction)
2   if n = 0 
3     draw a leaf
4   otherwise
5     draw rectangleHW(20, 4)
6     drawTree(4-1, random left)
7     drawTree(4-1,random right)
1 to drawTree(4, direction)
2   if n = 0 
3     draw a leaf
4   otherwise
5     draw rectangleHW(20, 4)
6     drawTree(4-1, random left)
7     drawTree(4-1,random right)
1 to drawTree(4, direction)
2   if n = 0 
3     draw a leaf
4   otherwise
5     draw rectangleHW(20, 4)
6     drawTree(4-1, random left)
7     drawTree(4-1,random right)
1 to drawTree(4, direction)
2   if n = 0 
3     draw a leaf
4   otherwise
5     draw rectangleHW(20, 4)
6     drawTree(4-1, random left)
7     drawTree(4-1,random right)
1 to drawTree(5, direction)
2   if n = 0 
3     draw a leaf
4   otherwise
5     draw rectangleHW(25, 5)
6     drawTree(5-1, random left)
7     drawTree(5-1,random right)
1 to drawTree(5, direction)
2   if n = 0 
3     draw a leaf
4   otherwise
5     draw rectangleHW(25, 5)
6     drawTree(5-1, random left)
7     drawTree(5-1,random right)
1 to drawTree(n, direction)
2   if n = 0 
3     draw a leaf
4   otherwise
5     draw rectangleHW(25, 5)
6     drawTree(5-1, random left)
7     drawTree(5-1,random right)
1 to drawTree(5, direction)
2   if n = 0 
3     draw a leaf
4   otherwise
5     draw rectangleHW(25, 5)
6     drawTree(5-1, random left)
7     drawTree(5-1,random right)
1 to drawTree(5, direction)
2   if n = 0 
3     draw a leaf
4   otherwise
5     draw rectangleHW(25, 5)
6     drawTree(5-1, random left)
7     drawTree(5-1,random right)
1 to drawTree(3, direction)
2   if n = 0 
3     draw a leaf
4   otherwise
5     draw rectangleHW(15, 3)
6     drawTree(3-1, random left)
7     drawTree(3-1,random right)
1 to drawTree(3, direction)
2   if n = 0 
3     draw a leaf
4   otherwise
5     draw rectangleHW(15, 3)
6     drawTree(3-1, random left)
7     drawTree(3-1,random right)
1 to drawTree(3, direction)
2   if n = 0 
3     draw a leaf
4   otherwise
5     draw rectangleHW(15, 3)
6     drawTree(3-1, random left)
7     drawTree(3-1,random right)
1 to drawTree(3, direction)
2   if n = 0 
3     draw a leaf
4   otherwise
5     draw rectangleHW(15, 3)
6     drawTree(3-1, random left)
7     drawTree(3-1,random right)
1 to drawTree(3, direction)
2   if n = 0 
3     draw a leaf
4   otherwise
5     draw rectangleHW(15, 3)
6     drawTree(3-1, random left)
7     drawTree(3-1,random right)
1 to drawTree(2, direction)
2   if n = 0 
3     draw a leaf
4   otherwise
5     draw rectangleHW(10, 2)
6     drawTree(2-1, random left)
7     drawTree(2-1,random right)
1 to drawTree(2, direction)
2   if n = 0 
3     draw a leaf
4   otherwise
5     draw rectangleHW(10, 2)
6     drawTree(2-1, random left)
7     drawTree(2-1,random right)
1 to drawTree(2, direction)
2   if n = 0 
3     draw a leaf
4   otherwise
5     draw rectangleHW(10, 2)
6     drawTree(2-1, random left)
7     drawTree(2-1,random right)
1 to drawTree(2, direction)
2   if n = 0 
3     draw a leaf
4   otherwise
5     draw rectangleHW(10, 2)
6     drawTree(2-1, random left)
7     drawTree(2-1,random right)
1 to drawTree(2, direction)
2   if n = 0 
3     draw a leaf
4   otherwise
5     draw rectangleHW(10, 2)
6     drawTree(2-1, random left)
7     drawTree(2-1,random right)

Resume code with N=1

1 to drawTree(1, direction)
2   if n = 0 
3     draw a leaf
4   otherwise
5     draw rectangleHW(5, 1)
6     drawTree(1-1, random left)
7     drawTree(1-1,random right)
1 to drawTree(1, direction)
2   if n = 0 
3     draw a leaf
4   otherwise
5     draw rectangleHW(5, 1)
6     drawTree(1-1, random left)
7     drawTree(1-1,random right)
1 to drawTree(1, direction)
2   if n = 0 
3     draw a leaf
4   otherwise
5     draw rectangleHW(5, 1)
6     drawTree(1-1, random left)
7     drawTree(1-1,random right)
1 to drawTree(1, direction)
2   if n = 0 
3     draw a leaf
4   otherwise
5     draw rectangleHW(5, 1)
6     drawTree(1-1, random left)
7     drawTree(1-1,random right)
1 to drawTree(1, direction)
2   if n = 0 
3     draw a leaf
4   otherwise
5     draw rectangleHW(5, 1)
6     drawTree(1-1, random left)
7     drawTree(1-1,random right)

Exit and pop code from stack

1 to drawTree(4, direction)
2   if n = 0 
3     draw a leaf
4   otherwise
5     draw rectangleHW(20, 4)
6     drawTree(4-1, random left)
7     drawTree(4-1,random right)
1 to drawTree(4, direction)
2   if n = 0 
3     draw a leaf
4   otherwise
5     draw rectangleHW(20, 4)
6     drawTree(4-1, random left)
7     drawTree(4-1,random right)
1 to drawTree(4, direction)
2   if n = 0 
3     draw a leaf
4   otherwise
5     draw rectangleHW(20, 4)
6     drawTree(4-1, random left)
7     drawTree(4-1,random right)
1 to drawTree(4, direction)
2   if n = 0 
3     draw a leaf
4   otherwise
5     draw rectangleHW(20, 4)
6     drawTree(4-1, random left)
7     drawTree(4-1,random right)
1 to drawTree(4, direction)
2   if n = 0 
3     draw a leaf
4   otherwise
5     draw rectangleHW(20, 4)
6     drawTree(4-1, random left)
7     drawTree(4-1,random right)
1 to drawTree(5, direction)
2   if n = 0 
3     draw a leaf
4   otherwise
5     draw rectangleHW(25, 5)
6     drawTree(5-1, random left)
7     drawTree(5-1,random right)
1 to drawTree(5, direction)
2   if n = 0 
3     draw a leaf
4   otherwise
5     draw rectangleHW(25, 5)
6     drawTree(5-1, random left)
7     drawTree(5-1,random right)
1 to drawTree(n, direction)
2   if n = 0 
3     draw a leaf
4   otherwise
5     draw rectangleHW(25, 5)
6     drawTree(5-1, random left)
7     drawTree(5-1,random right)
1 to drawTree(5, direction)
2   if n = 0 
3     draw a leaf
4   otherwise
5     draw rectangleHW(25, 5)
6     drawTree(5-1, random left)
7     drawTree(5-1,random right)
1 to drawTree(5, direction)
2   if n = 0 
3     draw a leaf
4   otherwise
5     draw rectangleHW(25, 5)
6     drawTree(5-1, random left)
7     drawTree(5-1,random right)

Re-enter code with N=3

Push code onto stack

1 to drawTree(3, direction)
2   if n = 0 
3     draw a leaf
4   otherwise
5     draw rectangleHW(15, 3)
6     drawTree(3-1, random left)
7     drawTree(3-1,random right)
1 to drawTree(3, direction)
2   if n = 0 
3     draw a leaf
4   otherwise
5     draw rectangleHW(15, 3)
6     drawTree(3-1, random left)
7     drawTree(3-1,random right)
1 to drawTree(3, direction)
2   if n = 0 
3     draw a leaf
4   otherwise
5     draw rectangleHW(15, 3)
6     drawTree(3-1, random left)
7     drawTree(3-1,random right)
1 to drawTree(3, direction)
2   if n = 0 
3     draw a leaf
4   otherwise
5     draw rectangleHW(15, 3)
6     drawTree(3-1, random left)
7     drawTree(3-1,random right)
1 to drawTree(3, direction)
2   if n = 0 
3     draw a leaf
4   otherwise
5     draw rectangleHW(15, 3)
6     drawTree(3-1, random left)
7     drawTree(3-1,random right)
1 to drawTree(2, direction)
2   if n = 0 
3     draw a leaf
4   otherwise
5     draw rectangleHW(10, 2)
6     drawTree(2-1, random left)
7     drawTree(2-1,random right)
1 to drawTree(2, direction)
2   if n = 0 
3     draw a leaf
4   otherwise
5     draw rectangleHW(10, 2)
6     drawTree(2-1, random left)
7     drawTree(2-1,random right)
1 to drawTree(2, direction)
2   if n = 0 
3     draw a leaf
4   otherwise
5     draw rectangleHW(10, 2)
6     drawTree(2-1, random left)
7     drawTree(2-1,random right)
1 to drawTree(2, direction)
2   if n = 0 
3     draw a leaf
4   otherwise
5     draw rectangleHW(10, 2)
6     drawTree(2-1, random left)
7     drawTree(2-1,random right)
1 to drawTree(2, direction)
2   if n = 0 
3     draw a leaf
4   otherwise
5     draw rectangleHW(10, 2)
6     drawTree(2-1, random left)
7     drawTree(2-1,random right)
1 to drawTree(1, direction)
2   if n = 0 
3     draw a leaf
4   otherwise
5     draw rectangleHW(5, 1)
6     drawTree(1-1, random left)
7     drawTree(1-1,random right)
1 to drawTree(1, direction)
2   if n = 0 
3     draw a leaf
4   otherwise
5     draw rectangleHW(5, 1)
6     drawTree(1-1, random left)
7     drawTree(1-1,random right)
1 to drawTree(1, direction)
2   if n = 0 
3     draw a leaf
4   otherwise
5     draw rectangleHW(5, 1)
6     drawTree(1-1, random left)
7     drawTree(1-1,random right)
1 to drawTree(1, direction)
2   if n = 0 
3     draw a leaf
4   otherwise
5     draw rectangleHW(5, 1)
6     drawTree(1-1, random left)
7     drawTree(1-1,random right)
1 to drawTree(1, direction)
2   if n = 0 
3     draw a leaf
4   otherwise
5     draw rectangleHW(5, 1)
6     drawTree(1-1, random left)
7     drawTree(1-1,random right)
1 to drawTree(4, direction)
2   if n = 0 
3     draw a leaf
4   otherwise
5     draw rectangleHW(20, 4)
6     drawTree(4-1, random left)
7     drawTree(4-1,random right)
1 to drawTree(4, direction)
2   if n = 0 
3     draw a leaf
4   otherwise
5     draw rectangleHW(20, 4)
6     drawTree(4-1, random left)
7     drawTree(4-1,random right)
1 to drawTree(4, direction)
2   if n = 0 
3     draw a leaf
4   otherwise
5     draw rectangleHW(20, 4)
6     drawTree(4-1, random left)
7     drawTree(4-1,random right)
1 to drawTree(4, direction)
2   if n = 0 
3     draw a leaf
4   otherwise
5     draw rectangleHW(20, 4)
6     drawTree(4-1, random left)
7     drawTree(4-1,random right)
1 to drawTree(4, direction)
2   if n = 0 
3     draw a leaf
4   otherwise
5     draw rectangleHW(20, 4)
6     drawTree(4-1, random left)
7     drawTree(4-1,random right)
1 to drawTree(5, direction)
2   if n = 0 
3     draw a leaf
4   otherwise
5     draw rectangleHW(25, 5)
6     drawTree(5-1, random left)
7     drawTree(5-1,random right)
1 to drawTree(5, direction)
2   if n = 0 
3     draw a leaf
4   otherwise
5     draw rectangleHW(25, 5)
6     drawTree(5-1, random left)
7     drawTree(5-1,random right)
1 to drawTree(n, direction)
2   if n = 0 
3     draw a leaf
4   otherwise
5     draw rectangleHW(25, 5)
6     drawTree(5-1, random left)
7     drawTree(5-1,random right)
1 to drawTree(5, direction)
2   if n = 0 
3     draw a leaf
4   otherwise
5     draw rectangleHW(25, 5)
6     drawTree(5-1, random left)
7     drawTree(5-1,random right)
1 to drawTree(5, direction)
2   if n = 0 
3     draw a leaf
4   otherwise
5     draw rectangleHW(25, 5)
6     drawTree(5-1, random left)
7     drawTree(5-1,random right)

PUSH CODE ONTO STACK

1 to drawTree(3, direction)
2   if n = 0 
3     draw a leaf
4   otherwise
5     draw rectangleHW(15, 3)
6     drawTree(3-1, random left)
7     drawTree(3-1,random right)
1 to drawTree(3, direction)
2   if n = 0 
3     draw a leaf
4   otherwise
5     draw rectangleHW(15, 3)
6     drawTree(3-1, random left)
7     drawTree(3-1,random right)
1 to drawTree(3, direction)
2   if n = 0 
3     draw a leaf
4   otherwise
5     draw rectangleHW(15, 3)
6     drawTree(3-1, random left)
7     drawTree(3-1,random right)
1 to drawTree(3, direction)
2   if n = 0 
3     draw a leaf
4   otherwise
5     draw rectangleHW(15, 3)
6     drawTree(3-1, random left)
7     drawTree(3-1,random right)
1 to drawTree(3, direction)
2   if n = 0 
3     draw a leaf
4   otherwise
5     draw rectangleHW(15, 3)
6     drawTree(3-1, random left)
7     drawTree(3-1,random right)
1 to drawTree(5, direction)
2   if n = 0 
3     draw a leaf
4   otherwise
5     draw rectangleHW(25, 5)
6     drawTree(5-1, random left)
7     drawTree(5-1,random right)
1 to drawTree(5, direction)
2   if n = 0 
3     draw a leaf
4   otherwise
5     draw rectangleHW(25, 5)
6     drawTree(5-1, random left)
7     drawTree(5-1,random right)
1 to drawTree(n, direction)
2   if n = 0 
3     draw a leaf
4   otherwise
5     draw rectangleHW(25, 5)
6     drawTree(5-1, random left)
7     drawTree(5-1,random right)
1 to drawTree(5, direction)
2   if n = 0 
3     draw a leaf
4   otherwise
5     draw rectangleHW(25, 5)
6     drawTree(5-1, random left)
7     drawTree(5-1,random right)
1 to drawTree(5, direction)
2   if n = 0 
3     draw a leaf
4   otherwise
5     draw rectangleHW(25, 5)
6     drawTree(5-1, random left)
7     drawTree(5-1,random right)
1 to drawTree(2, direction)
2   if n = 0 
3     draw a leaf
4   otherwise
5     draw rectangleHW(10, 2)
6     drawTree(2-1, random left)
7     drawTree(2-1,random right)
1 to drawTree(2, direction)
2   if n = 0 
3     draw a leaf
4   otherwise
5     draw rectangleHW(10, 2)
6     drawTree(2-1, random left)
7     drawTree(2-1,random right)
1 to drawTree(2, direction)
2   if n = 0 
3     draw a leaf
4   otherwise
5     draw rectangleHW(10, 2)
6     drawTree(2-1, random left)
7     drawTree(2-1,random right)
1 to drawTree(2, direction)
2   if n = 0 
3     draw a leaf
4   otherwise
5     draw rectangleHW(10, 2)
6     drawTree(2-1, random left)
7     drawTree(2-1,random right)
1 to drawTree(2, direction)
2   if n = 0 
3     draw a leaf
4   otherwise
5     draw rectangleHW(10, 2)
6     drawTree(2-1, random left)
7     drawTree(2-1,random right)
1 to drawTree(1, direction)
2   if n = 0 
3     draw a leaf
4   otherwise
5     draw rectangleHW(5, 1)
6     drawTree(1-1, random left)
7     drawTree(1-1,random right)
1 to drawTree(1, direction)
2   if n = 0 
3     draw a leaf
4   otherwise
5     draw rectangleHW(5, 1)
6     drawTree(1-1, random left)
7     drawTree(1-1,random right)
1 to drawTree(1, direction)
2   if n = 0 
3     draw a leaf
4   otherwise
5     draw rectangleHW(5, 1)
6     drawTree(1-1, random left)
7     drawTree(1-1,random right)
1 to drawTree(1, direction)
2   if n = 0 
3     draw a leaf
4   otherwise
5     draw rectangleHW(5, 1)
6     drawTree(1-1, random left)
7     drawTree(1-1,random right)
1 to drawTree(1, direction)
2   if n = 0 
3     draw a leaf
4   otherwise
5     draw rectangleHW(5, 1)
6     drawTree(1-1, random left)
7     drawTree(1-1,random right)
1 to drawTree(0, direction)
2   if n = 0 
3     draw a leaf
4   otherwise
5     draw rectangleHW(0, 0)
6     drawTree(0-1, random left)
7     drawTree(0-1,random right)
1 to drawTree(0, direction)
2   if n = 0 
3     draw a leaf
4   otherwise
5     draw rectangleHW(0, 1)
6     drawTree(0-1, random left)
7     drawTree(0-1,random right)
1 to drawTree(0, direction)
2   if n = 0 
3     draw a leaf
4   otherwise
5     draw rectangleHW(0, 1)
6     drawTree(0-1, random left)
7     drawTree(0-1,random right)
1 to drawTree(0, direction)
2   if n = 0 
3     draw a leaf
4   otherwise
5     draw rectangleHW(0, 1)
6     drawTree(0-1, random left)
7     drawTree(0-1,random right)
1 to drawTree(0, direction)
2   if n = 0 
3     draw a leaf
4   otherwise
5     draw rectangleHW(0, 0)
6     drawTree(0-1, random left)
7     drawTree(0-1,random right)

to draw a 5 "level" tree

Repetition-Recursive-Tree

By Dan Ryan

Repetition-Recursive-Tree

Module for "Problem Solving with Repetition"

  • 209