Question

This is my code for family tree, but i am confused on how to make a...

This is my code for family tree, but i am confused on how to make a main class that will use these methods.  

public class FamilyTree

{

private class Node

{

String name;

Node next;

Node child;

public Node(String name)

{

this.name = name;

next = null;

child = null;

}

}

  

public Node addSibling(Node node, String name)

{

if(node == null)

return null;

while(node.next != null)

node = node.next;

return(node.next = new Node(name));

}

  

public Node addChild(Node node, String name)

{

if(node == null)

return null;

  

if(node.child != null)

return(addSibling(node.child,name));

else

return(node.child = new Node(name));

}

  

public void traverse(Node root)

{

if(root == null)

return;

while(root != null)

{

System.out.print(root.name + " ");

if(root.child != null)

traverse(root.child);

root = root.next;

}

}

}

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Simpe Code to use the Faimly Tree's methods:

Changes are been in bold text:

public class FamilyTree

{

private class Node

{

String name;

Node next;

Node child;

public Node(String name)

{

this.name = name;

next = null;

child = null;

}

}

public Node addSibling(Node node, String name)

{

if(node == null)

return null;

while(node.next != null)

node = node.next;

return(node.next = new Node(name));

}

  

public Node addChild(Node node, String name)

{

if(node == null)

return null;

if(node.child != null)

return(addSibling(node.child,name));

else

return(node.child = new Node(name));

}

public void traverse(Node root)

{

if(root == null)

return;

while(root != null)

{

System.out.print(root.name + " ");

if(root.child != null)

traverse(root.child);

root = root.next;

}

}

//main method to start of the program

public static void main(String[] args)

{

//Constructing family tree

FamilyTree familyTree = new FamilyTree();

//adding Nodes to Family Tree

Node grandFather = familyTree.new Node("James Vint");

//adding Child

Node father = familyTree.addChild(grandFather, "Alex");

//adding Siblings

Node father_s_Sister = familyTree.addChild(father, "Romine");

Node me = familyTree.addChild(father, "Browne");

Node myBrother = familyTree.addSibling(me, "Kane Wilson");

//calling traverse() method from grandFather

familyTree.traverse(grandFather);

}

}

Output:

James Vint Alex Romine Browne Kane Wilson

Add a comment
Know the answer?
Add Answer to:
This is my code for family tree, but i am confused on how to make a...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Using the following implementation of Tree class Node   {   public int iData;              // data item (key)   public...

    Using the following implementation of Tree class Node   {   public int iData;              // data item (key)   public double dData;           // data item   public Node leftChild;         // this node's left child   public Node rightChild;        // this node's right child   public void displayNode()      // display ourself      {      System.out.print('{');      System.out.print(iData);      System.out.print(", ");      System.out.print(dData);      System.out.print("} ");      }   }  // end class Node //------------------------------------------------------------------ import java.io.IOException; import java.util.Stack; public class Tree { private Node root; // first node of tree // ------------------------------------------------------------- public Tree() // constructor { root = null; }...

  • 1. Write a function in Tree class which returns true if and only if the tree...

    1. Write a function in Tree class which returns true if and only if the tree satisfies the binary search tree property. The function’s header line is public boolean isValidBST() And in the attached code, you just need to finish the function after the comment: “//Instructor hint: please write your code here:” Make sure you execute your code, and the result in the main function after calling your function should be same as the prompt message I write. Clearly you...

  • Please I need help ASAP Java Programing: Binary Search Tree Fully implement the BST class in Listing 25.4 (on page 961 of the 11th Edition of the text). Design and write a (main) driver program to com...

    Please I need help ASAP Java Programing: Binary Search Tree Fully implement the BST class in Listing 25.4 (on page 961 of the 11th Edition of the text). Design and write a (main) driver program to completely test every method in the BST class to ensure the class meets all its requirements. You should read the Listing 25.5: TestBST.java for an idea of what your program should look like. Listing 25.4 BST.java public class BST> extends AbstractTree { protected TreeNode...

  • Need help in the below question. Answer in java Start with the tree.java program (Listing 8.1)...

    Need help in the below question. Answer in java Start with the tree.java program (Listing 8.1) and modify it to create a binary tree from a string of letters (like A, B, and so on) entered by the user. Each letter will be displayed in its own node. Construct the tree so that all the nodes that contain letters are leaves. Parent nodes can contain some non-letter symbol like +. Make sure that every parent node has exactly two children....

  • Implement the following in java. 1. An insertAtBeginning(Node newNode) function, that inserts a node at the...

    Implement the following in java. 1. An insertAtBeginning(Node newNode) function, that inserts a node at the beginning(root) of the linked list. 2. A removeFromBeginning() function, that removes the node from the beginning of the linked list and assigns the next element as the new beginning(root). 3. A traverse function, that iterates the list and prints the elements in the linked list. For the insertAtBeginning(Node newNode) function: 1. Check if the root is null. If it is, just assign the new...

  • I need to do a tree sort method but the treesortMethod is not working /****Binarytree class****\...

    I need to do a tree sort method but the treesortMethod is not working /****Binarytree class****\ package Tree; public class BinaryTree { private TreeNode root; // head of the list    //constructor - create an empty binary tree public BinaryTree() { root = null;    }    //isEmpty() - return true if tree is empty, false otherwise public boolean isEmpty() { return (root == null); }    //deleteTree() - remove all items from tree public void deleteList() { root =...

  • So I am working on an assignment where we make a rudimentary browser history with c#....

    So I am working on an assignment where we make a rudimentary browser history with c#. The requirement is that we need to use a linked list to do this. The issue that I am having is that when I want to go backwards in the history and print it takes from the beginning of the list and not the front. (Example is if I had a list with 1, 2, 3, 4 in it and went back It would...

  • Add a non-recursive inorder() method to class LinkedBinaryTree<E> to traverse binary tree. Test inorder() method. Implementation...

    Add a non-recursive inorder() method to class LinkedBinaryTree<E> to traverse binary tree. Test inorder() method. Implementation in Java #########LinkedBinary Tree class######### public class LinkedBinaryTree<E> extends AbstractBinaryTree<E> { //---------------- nested Node class ---------------- /** Nested static class for a binary tree node. */ protected static class Node<E> implements Position<E> { private E element; // an element stored at this node private Node<E> parent; // a reference to the parent node (if any) private Node<E> left; // a reference to the left...

  • I am trying to make a linked list queue and I am trying to use the...

    I am trying to make a linked list queue and I am trying to use the display method I made just to see if its working but when I run it nothing is displayed please help. Also the newPlane boolean was made just so I can randomly decide if the plane is going to join the queue or not. public class PlaneSimulation { public static void main(String[] args) { int landTime = 2; int takeoffTime = 3; int avgArrivalInterval =...

  • Hi, So I have a finished class for the most part aside of the toFile method...

    Hi, So I have a finished class for the most part aside of the toFile method that takes a file absolute path +file name and writes to that file. I'd like to write everything that is in my run method also the toFile method. (They are the last two methods in the class). When I write to the file this is what I get. Instead of the desired That I get to my counsel. I am having trouble writing my...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT