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

  • Start with the tree.java program (Listing 8.1) and modify it to create a binary tree from...

    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. Don’t worry if the tree is unbalanced. Note that...

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

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

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

  • Professionally and thoroughly comment on this code. //BinarySearchTree.java package Project.bst; //imports required import java.io.BufferedReader; import java.io.FileNotFoundException;...

    Professionally and thoroughly comment on this code. //BinarySearchTree.java package Project.bst; //imports required import java.io.BufferedReader; import java.io.FileNotFoundException; import java.io.FileReader; import java.io.IOException; import java.util.ArrayList; import java.util.List; import java.util.Scanner; class BSTTreeNode{    BSTTreeNode left, right;    String data; public BSTTreeNode(){    left = null;    right = null;    data = null; } public BSTTreeNode(String n){    left = null;    right = null;    data = n; } public void setLeft(BSTTreeNode n){    left = n; } public void setRight(BSTTreeNode n){   ...

  • Java binary search tree Add the following print method to the binary search tree class created...

    Java binary search tree Add the following print method to the binary search tree class created in class (on D2L). This method should print all the nodes in the tree in level order (root first, then all children of root, then all children of those). Ensure your method runs in O(N), include comments to show how it conforms to this rule. Method header: public void printInLevelOrder() public class BinarySearchTree<E extends Comparable<? super E>> { private Node root; public BinarySearchTree() {...

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

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