#implementation of 2d binary tree in java

7 messages · Page 1 of 1 (latest)

fresh crater
#

I want to implement a 2d binary tree which means that each node of the main tree contains an int value and a reference to another tree. How do i write this reference to another tree in java?
The whole concept of pointers in java is still really confusing to me so I'd appreciate any kind of help.

Here's my code so far:

     {
    
       //class Node fields:
       public int key;
       public Node leftChild;        
       public Node rightChild;
       public Node root;


       public Node(int key) //class Node constructor
             {
                this.key = key;  
                //root = null;    
             }
     } 


public class TwoDimTree 
{    
    // class TwoDimTree fields:
    private Node root;
    private int key; 

    public TwoDimTree() 
    { 
        root = null;  
    }

  
    public void insert(int x, int y) 
    {      
        Node node = new Node(x);
        node = new Node(y); /// is that how you create the reference to the other tree???
    } 
}```
runic pineBOT
#

This post has been reserved for your question.

Hey @fresh crater! Please use /close or the Close Post button above when you're finished. Please remember to follow the help guidelines. This post will be automatically closed after 300 minutes of inactivity.

TIP: Narrow down your issue to simple and precise questions to maximize the chance that others will reply in here.

vernal fiber
#

basically all the objects you create are put in a table somewhere else, not related to variables
variables then point to where the object you created is in that table
when you create an object with new, that object is added to the table, and its position in that table is given to pass or to put in a variable, for example

#

there you're just reassigning node so x is lost

#

but that is how you create a reference to another tree yeah

#

you just don't have the right logic around it