ahmedshayan 0 Newbie Poster

I am faced this Problem that I can't print display parent's content. Please help:

import javax.swing.JOptionPane;
public class BinaryTree
{

  private Node root;

  BinaryTree()
     {
        root = null;
     }
//------------------------------------------------------------------
  void buildTree()
  {
      String n=JOptionPane.showInputDialog("Root Information:\nEnter Name: ");
      int a=Integer.parseInt(JOptionPane.showInputDialog("Root Information:\nEnter Age of Root:"));
      String s=JOptionPane.showInputDialog("Root Information:\nAlive or Dead?");
      String w=JOptionPane.showInputDialog("Root Information:\nMarried or unmarried?");
      root=new Node(n,a,s,w);

  }
//------------------------------------------------------------------
  public boolean Search(String data, int a)
      {
        return(Search(root, data, a));
      }

  private boolean Search(Node node, String data, int a)
    {

          if (node==null)
              {
               return(false);
              }
           if (data.equals(node.name))
              {
                    return(true);
              }
           else
              {
              if(a<=(node.age))
                   return(Search(node.left, data, a));
              else
                    return(Search(node.right, data, a));
              }

  }
 //-----------------------------------------------------------------------
  public void insert(String nvar,String cvar,int k,int avar,String s,String w)
  {
    root = insert(root,nvar,cvar, k, avar, s, w);

  }

  private Node insert(Node node,String nvar,String cvar,int k, int avar,String s,String w)
  {
      Node temp=new Node();
    if (node==null)
    {
      node = new Node(nvar,avar,s,w);
      node.parent=temp;
      System.out.println("\nNew Node Information:\n");
      node.display();
[b]      System.out.println("\nParent Node Information:\n");
      node.parent.display();[/b]
    }
    else
    {
      if (cvar.equals(node.name) && k==node.key && avar<node.age)
      {
        temp=node;
        node.left = insert(node.left,nvar,cvar,k,avar,s,w);
      }
      else
      {
        node.right = insert(node.right,nvar,cvar,k,avar,s,w);
      }
    }

    return(node); // in any case, return the new pointer to the caller
  }
  public void printTree()
   {
     printTree(root);
     System.out.println();
   }
   private void printTree(Node node)
   {
     if (node == null)
         return;

     printTree(node.left);
     System.out.println("Information Of Node"+node.key+"\n\tName:"+node.name + "\n\tLife Status:"+node.status+"\n\tMartial Status "+node.wife);
    // printTree(node.parent);
     printTree(node.right);
   }
}

and here is the Node file:

public class Node
{
    Node parent;
    Node left;
    Node right;
    static int count=0;
    int key;
    String name;
    int age;
    String status;
    String wife;

    Node()
    {
      parent=null;
      left = null;
      right = null;
      name=null;
      age=0;
      status=null;
      wife=null;
    }
    Node(String n, int a, String s, String w)
    {
      key=count;
      parent=null;
      left = null;
      right = null;
      name=n;
      age=a;
      status=s;
      wife=w;
      count++;
    }
    public void display()
    {
       System.out.println("\nKey:"+key+"\nName:"+name+"\nAge:"+age+"\nLife Status:"+status+"\nMaritial Status:"+wife);
    }
    Node(Node n){
        key=count;
      parent=n.parent;
      left = n.left;
      right = n.right;
      name=n.name;
      age=n.age;
      status=n.status;
      wife=n.wife;
      count++;
    }
}

And here is the Driver File:

import java.io.*;
import javax.swing.JOptionPane;
import java.util.Scanner;

public class Driver
{
    public static void main(String[] args)
    {
        // TODO code application logic here
        BinaryTree familyTree=new BinaryTree();

        String n,s,w,p;
        int a,k,c,ch;
        familyTree.buildTree();
       do
            {
        System.out.println("\tMenu.\n\n\t1. Insert new Node.\n\t2. Search a Node.\n\t3. Print Tree.\n\t4. Exit.");
        ch=Integer.parseInt(JOptionPane.showInputDialog("Enter your choice"));


        switch(ch)
        {
            case 1:
            {
            n=JOptionPane.showInputDialog("Node Information:\nEnter Name: ");
            a=Integer.parseInt(JOptionPane.showInputDialog("Node Information:\nEnter Age of Root:"));
            k=Integer.parseInt(JOptionPane.showInputDialog("Node Information:\nEnter Key of Parent:"));
            p=JOptionPane.showInputDialog("Node Information:\nEnter Name Parent:");
            s=JOptionPane.showInputDialog("Root Information:\nAlive or Dead?");
            w=JOptionPane.showInputDialog("Root Information:\nMarried or unmarried?");
            familyTree.insert(n,p,k,a,s,w);
            try{
            BufferedWriter br=new BufferedWriter(new FileWriter("file.txt",true));
            br.append(n);
            br.close();
            }
            catch(IOException e){
                e.printStackTrace();
            }
            break;
            }
            case 2:
            {

            String sr=JOptionPane.showInputDialog("Enter name to be Searched: ");
            int ag=Integer.parseInt(JOptionPane.showInputDialog("Enter Age: "));
            System.out.println("Search:"+familyTree.Search(sr,ag));
            break;
            }
            case 3:
            {

                 familyTree.printTree();
                 break;
            }
            case 4:
            {
                System.exit(0);
            }
            default:
                System.err.println("Wrong Input.");
            }

            c=Integer.parseInt(JOptionPane.showInputDialog("Do you want another go?(1 or 0)"));

            }while(c==1);

}
}
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.