Due: Friday 5/2 10:00am
Submission name: w18_tree
For this, you will need a TreeNode
class, which you can get here: https://github.com/nextcs-dw/thesource/tree/main/trees/TreeRunner
Create a Tree
class with the following elements:
Instance Variables:
TreeNode root
int numLevels
: The height of the tree
Methods:
- Constructor:
Tree(int x, int y, int levels)
- Set
root
to a new node at (x, y). - Set
numLevels
tolevels
- Assign
root.left
androot.right
to the return value formakeTree
(described below).
- Set
TreeNede makeTree(int x, int y, int levels)
- Create a new node at
(x, y)
with a random character.char(int(random(26)) + 'A')
- Create a full left subtree with
levels-1
levels - Create a full right subtree with
levels-1
levels - When creating a subtree, you want the next level down to be below and to the left/right of (x, y). You can either make these offsets from (x, y) based on constants (i.e. (x-50, y+100)…) or dynamically based on the current level. You may take either approach.
- If
levels
is0
, returnnull
- Create a new node at
void display(TreeNode current)
- Draw the tree rooted at
current
.
- Draw the tree rooted at
Main Driver File. Use the following file:
Tree oak;
void setup() {
size(800, 500);
oak = new Tree(width/2, 50, 4);
oak.display(oak.root);
}//setup