0

Forgive me if this makes no sense at all, but I am just learning. I have stumbled across a piece of code that traverses a binary tree using a level search. I understand mostly the logic behind it but there are parts that confuse me, mainly 3 lines -

Node left, right; in the Node class. Am I correct that we are just making variables of this class type?

Node root; in the BinaryTree class. Is this an object of type Node? I thought you had to instantiate the class to have the object?

tree_level.root.left.right = new Node(5); I have no clue what is going on here. I understand that tree_level is the object and that through that object it can access the root variable, but then how is root able to access multiple left and right variables from the Node class.

I guess I'm more confused over how the two classes relate without any instantiation and I don't see any static code either.

package com.company;
import com.sun.xml.internal.ws.api.model.wsdl.WSDLOutput;
import org.w3c.dom.ls.LSOutput;
import java.util.Queue;
import java.util.LinkedList;
class Node{
 int data;
 Node left, right;
 public Node(int item){
 data = item;
 left = null;
 right = null;
 }
}
public class BinaryTree {
 Node root;
 public void printLevelOrder(){
 Queue<Node> queue = new LinkedList<Node>();
 queue.add(root);
 while(!queue.isEmpty()){
 System.out.println("While loop starts...");
 for(Node s : queue) {
 System.out.println("Loop: " + s.data);
 }
 Node tempNode = queue.poll();
 System.out.println(tempNode.data + " ");
 if(tempNode.left != null){
 queue.add(tempNode.left);
 }
 if(tempNode.right != null){
 queue.add(tempNode.right);
 }
 }
 }
 public static void main(String[] args) {
 BinaryTree tree_level = new BinaryTree();
 tree_level.root = new Node(1);
 tree_level.root.left = new Node(2);
 tree_level.root.right = new Node(3);
 tree_level.root.left.left = new Node(4);
 tree_level.root.left.right = new Node(5);
 System.out.println("The level order traversal of the binary tree is: ");
 tree_level.printLevelOrder();
 }
}
asked Apr 24, 2020 at 19:04
2
  • 2
    Question 1: Yes, left and right are variables of type Node. Question 2: No, you don't need to instantiate it this is just declaring a variable named root that is type Node. Question 3: This is accessing the components of each Node by direct address. As left and right are Node variables, they each have components date, left, and right. tree_level is a variable of type BinaryTree which has the component, which has a left and a right. tree_level.root.left is the left of the node root of the BinaryTree tree_level. Commented Apr 24, 2020 at 19:09
  • 2
    Look up binary trees. Here, each Node object contains references to its two children on the left and right (kept as instance variables) and it also contains some data, an integer kept in the instance field data. The field root is basically the top of the tree, the root (because binary trees are upside down). In the statement tree_level.root.left.right = new Node(5);, you are setting the right child of the left child of the root Node to a Node containing the data 5. It looks like an pretty complicated statement though Commented Apr 24, 2020 at 19:09

1 Answer 1

2

I'll try to answer your questions.

Node left, right; in the Node class. Am I correct that we are just making variables of this class type?

Yes, that's correct. After that declaration, your class Node has two new attributes Node left and Node right.

enter image description here

Node root; in the BinaryTree class. Is this an object of type Node?

Yes, this is an instantiation of a Node with the value null. From the Java Language Specification, section 4.12.5:

Initial Values of Variables

Every variable in a program must have a value before its value is used:

Each class variable, instance variable, or array component is initialized with a default > value when it is created

[...] For all reference types, the default value is null.

tree_level.root.left.right = new Node(5)

A new object of type Node is being placed inside a BinaryTree. This Node has a value of 5. In this case is something like this:

enter image description here

answered Apr 24, 2020 at 19:29
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.