How to draw a Binary Tree?

This is an example of “Tree in Java”. Can i modify the Node* class so that to draw the node like so

class Node {
int value;
int x;    // add x-coord
int y;    // add y-coord
Node left;
Node right;

Node(int value, int x, int y) {
    this.value = value;
    this.x = x;
    this.y = y;
    right = null;
    left = null;   }
}

And then how to add x-coord and y-coord into the BinaryTree class?

1 Like

Whoa there partner! Before you go about doing any drawing, make sure you have both a setup() and draw() function!

One will define the sketch’s background() color. The other determines the sketch’s size().

Start by posting some code that does that and then we will get to drawing objects!

yes, i have this setup() and draw() functions.

Alright. So in your setup() function, you want to make sure that your root node is a Node.

In your draw function, you want to call your Node’s draw method.

So you have this:

class Node {
  Node() {
  }
  void display(){
  }
}

Node root;

void setup(){
  size(600,400);
  root = new Node();
}

void draw(){
  background(0);
  root.display();
}

Of course, you’ll want to actually draw something is the Node’s display method. Maybe an ellipse?

ellipse(0,0,50,50);
1 Like

oh, but a Node is just the leaf of the Tree. Where is the Tree itself?
i mean something like this: we have a Node

class Node {
  Node() {  }  
}

we have a Tree

public class BinaryTree {
    Node root;
public void add(int value) {  
if (root == null)   {   // create one-time single node  
root= new Node(value);    }
// add left- and right-nodes recursively
if (value < root.value) {  
    root.left= addRecursive(root, value);  
    } else if (value > root.value) {  
       root.right= addRecursive(root, value);   } 
 }
private Node addRecursive(Node current, int value) {      
    if (current == null) {     
        return new Node(value);
    }    
    if (value < current.value) {
       current.left = addRecursive(current.left, value);
    } else if (value > current.value) {
        current.right = addRecursive(current.right, value); 
    }
    return current;
}

then define the Tree and initialize it in setup() function

BinaryTree bt;
void setup(){
BinaryTree bt = new BinaryTree();    
    bt.add(1);
    bt.add(2);
    bt.add(3);
}
1 Like

For a start do not include fields for the X and Y coordinates in the Node class. The reason being that adding and removing nodes from the tree will affect the position of other nodes and you don’t want to traverse the tree to recalculate their positions.

The solution is to create a recursive function in the node class to draw the tree something like this

void show(int x, int y){
  // code to draw this node at xy
  if(left != null){
    left.show(x - 40, y + 20); // left and down
  }
  if(right != null){
    right.show(x + 40, y + 20); // right and down
  }
}

Also you have not defined what should happen when you add a node with the same value as an existing node, you have assumed all nodes will have a unique value.

3 Likes

Yes, it works, thanks)