Due: Monday 4/8 10:00am

Submission name: w16_tree

For this, you will need a TreeNode class, which you can get here: https://github.com/nextcs-dw/dwsource/tree/main/NodeStuff

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)
    • Create a node at (x, y) for the root.
    • Set numLevels to levels
    • Assign root.left and root.right to the return value for makeTree (described below).
  • 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 numLevels is 0, return null
  • void display(TreeNode current)
    • Draw the tree rooted at current.

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

a16-tree