0

Any thoughts why this wouldn't work. when using the commented line instead of the previous, Entire Binary tree is not getting printed but just the root node.

public class BinaryTreePractise {

  Node root;

  public void addNode(int age, String name){
      Node node = new Node(age, name);

      if (root ==null){
          root = node;
          return;
      }
      else {
          Node focusNode = root;
          while (true) {

            Node  parent = focusNode;
          if (age < focusNode.age) {
              focusNode = focusNode.leftChild;
              if (  focusNode == null){
                  parent.leftChild = node; // working code
                  focusNode = node; // any thoughts why this wouldn't work instead of the above
                  return;
              }


          }
          else {
              focusNode = focusNode.rightChild;
              if (  focusNode == null) {
                  parent.rightChild = node; // working code
                  focusNode = node; // any thoughts why this wouldn't work instead of the above
                  return;
              }

          }
          }
      }
  }

  class Node {
      int age;
      String name;

      Node leftChild;
      Node rightChild;
      Node(int age, String name){
          this.age = age;
          this.name = name;

      }

      public String toString(){
          return name + "has the age" + age;
      }
  }

  public void inOrderTraversal(Node focusNode){

      if(focusNode !=null) {

          inOrderTraversal(focusNode.leftChild);
          System.out.println(focusNode);
          inOrderTraversal(focusNode.rightChild);

      }


  }

  public static void main(String[] args){

      BinaryTreePractise binaryTreePractise = new BinaryTreePractise();

      binaryTreePractise.addNode(15, "A");
      binaryTreePractise.addNode(85, "B");
      binaryTreePractise.addNode(40, "C");
      binaryTreePractise.addNode(50, "D");
      binaryTreePractise.addNode(95, "E");
      binaryTreePractise.addNode(25, "F");
      binaryTreePractise.inOrderTraversal(binaryTreePractise.root);
  }
}

Any thoughts why this wouldn't work. when using the commented line instead of the previous, Entire Binary tree is not getting printed but just the root node.

josejuan
  • 9,338
  • 24
  • 31
Chris
  • 11
  • 1
  • 1
    `focusNode` is a local variable, scoped inside the `addNode` method. When you set it and then return, nothing happens. `parent.leftChild = node` modifies an object's state, which exists as long as the object referenced by `parent` does. See [What is the difference between a member variable and a local variable?](https://stackoverflow.com/questions/1177723/what-is-the-difference-between-a-member-variable-and-a-local-variable) – Welbog Aug 20 '21 at 19:01
  • Thanks Weblog, appreciate it!!! – Chris Aug 20 '21 at 19:39
  • Hey @Welbog, still not able to quite understand how parent.leftChild is exactly changing the root. Is there something more than the local variable? Both the variables parent and focusNode are declared within the method and not sure how parent is diff than focusNode variable. – Chris Aug 21 '21 at 16:12
  • `parent` is a variable containing a reference to an object. Let's say that object is `A`, off in some memory address. When you say `parent = B`, you're changing where the `parent` reference points. You're changing neither `A` nor `B`. When you say `parent.leftChild = B`, you're updating `A`'s `leftChild` variable, so that it now references `B`. From now on, whenever you reference `A`, whether by `parent`, `root` or anything else, its `leftChild` will always reference `B`. You need to understand the nuances between variables, references and objects. – Welbog Aug 21 '21 at 20:33
  • This may help: https://stackoverflow.com/questions/9224517/what-are-classes-references-and-objects, and this: https://stackoverflow.com/questions/32010172/what-is-the-difference-between-a-variable-object-and-reference. – Welbog Aug 21 '21 at 20:36

0 Answers0