Hi, I wanted to understand a java code that i picked from a website RoseIndian.net. It is about Binary Search three implementation in java. Can someone please explain how the retrieval of the node values [node.value] in the method "printInOrder(Node node){}" being done, because the node.values are not being stored in an array, so come are they able to retrieve by calling printInOrder(Node node)

public class BinaryTreeExample {

  public static void main(String[] args)

    {
  new BinaryTreeExample().run();
  }

  static class Node 

   {

  Node left;
  Node right;
  int value;

  public Node(int value) {
  this.value = value;
  }
  }

  public void run() {
  Node rootnode = new Node(25);
  System.out.println("Building tree with rootvalue 
" + rootnode.value);
  System.out.println("=======================
  ==========");
  insert(rootnode, 11);
  insert(rootnode, 15);
  insert(rootnode, 16);
  insert(rootnode, 23);
  insert(rootnode, 79);
  System.out.println("Traversing tree in order");
  System.out.println("========================
  =========");
  printInOrder(rootnode);

  }

  public void insert(Node node, int value) {
  if (value < node.value) {
  if (node.left != null) {
  insert(node.left, value);
  } else {
  System.out.println("  Inserted " + value + 
    " to left of node " + node.value);
  node.left = new Node(value);
  }
  } else if (value > node.value) {
  if (node.right != null) {
  insert(node.right, value);
  } else {
  System.out.println("  Inserted " + value + " 
  to right of node " + node.value);
  node.right = new Node(value);
  }
  }
  }

  public void printInOrder(Node node) {
  if (node != null) {
  printInOrder(node.left);
  System.out.println("  Traversed " + node.value);
  printInOrder(node.right);
  }
  }
}

Thanx,..

Recommended Answers

All 12 Replies

Please edit your code and wrap it in code tags to preserve the formatting.
Unformated code is hard to read

I suspect the codes are connect by their left and right reference pointers.
No array needed.

I did put it in Code-tags. Anyways, i still don't understand how the retrieval is being done. Can you please give some example or something to explain that to me.

Many thanks.

What prints out when the code is executed? It looks like the printouts in it should explain what it is doing.

Hint: It's recursive.

ps Treat RoseIndia (note correct spelling) with some caution, although there's a lot of useful code there, some of its stuff is out of date and some is just poor quality.

I do understand what it is doing, it is first traversing the left node, prints it and then the right node and it prints that node.

But what i want to know is how is it retrieving the 'node.left' and 'node.right', in the printInOrder(Node node) method, since i don't see any array that is saving node.left and node.right.

There is no array. The nodes are connected by the addresses stored in the left and right pointers. For example the following connects a new node to the left chain:
node.left = new Node(value);

class Node {
  Node left;
  Node right;
  int value;
  ...

Every instance of a Node has its own left and right. As Norm says, there is no array, the Nodes are just stored somewhere on the heap when they are created..

So what you are saying is that there is some type of storage as in (heap) that is collecting and storing the nodes.

Don't get antagonistic, i m just a newbie in java, because i was of the opinion that whenever u want to store and retrieve something u do it through some collection.

Thanks.

whenever u want to store and retrieve something

AClass aCls = new AClass(); // create an object that is stored in the heap
You can retrieve its value by using the aCls variable.

A linked list can have references that point to other nodes with references that point to other nodes that have references, etc

Thank you very much for your explanations.

Don't get antagonistic...

I'm really sorry if I gave the impression of being antagonistic. That certainly wasn't my intent.
I'm a crap typist, so I try to get my message across in the minimum number of words, maybe that seems abrupt. The reason I'm here is to help newbies, not antagonise them. So Mea Cupla. Sorry.

Thank you anyways,all.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.