Im working on a "family tree" program in java, and I am having trouble wrapping my head around an algorithm for searching through the nodes.
A node consists of a Name, and link to a partner, sibling, child and an integer identifier.
The algorithms I am trying just hit dead ends and I would be extremely grateful for a nudge in the right direction.
Basically, with each node having a a numerical identifier, I want to be able to have the user enter a number, search each node in the tree and insert a node as a child, sibling or partner of the matching node. Tree structure example:
Note, as it is an assignment I cannot change the structure
Alice[2] <--partner-- John[1]
|
Ted[3] --sibling--> Eric[4] --sibling--> Joanne[5]
|
Joe[6] --sibling--> Bret[7]
FamilyTree Class:
public class FamilyTree {
private class FamilyTreeNode{
private int identifier ;
private String Name ;
private FamilyTreeNode partner;
private FamilyTreeNode sibling;
private FamilyTreeNode child;
}
private FamilyTreeNode ancestor;
private FamilyTreeNode currentNode ;
private int indexNumber = 1;
public FamilyTree(){
this.ancestor = new FamilyTreeNode();
this.ancestor.Name = Input.getString("Enter ancestors Name: ");
this.ancestor.identifier = 0;
}
public FamilyTreeNode addChild(){
//Set up variables and create new node
currentNode = ancestor;
boolean matchFound = false ;
FamilyTreeNode newFamilyNode = new FamilyTreeNode() ;
newFamilyNode.Name = Input.getString("Enter Name");
//
//Checking for existing Name
if(currentNode.child != null){
currentNode = currentNode.child;
if(currentNode.Name.compareToIgnoreCase(newFamilyNode.Name) == 0){
matchFound = true;
}
while(currentNode.sibling != null){
currentNode = currentNode.sibling;
if(currentNode.Name.compareToIgnoreCase(newFamilyNode.Name) == 0){
matchFound = true;
}
}
}
//
//Check for existing siblings, add to end of list
currentNode = ancestor;
if(currentNode.child == null){
newFamilyNode.identifier = indexNumber;
currentNode.child = newFamilyNode ;
}else{
currentNode = currentNode.child;
while (currentNode.sibling != null){
currentNode = currentNode.sibling;}
if(matchFound == false){
indexNumber++;
newFamilyNode.identifier = indexNumber;
currentNode.sibling = newFamilyNode;
}
else{
System.out.println("Name already exists");
}
}
//
return newFamilyNode ;
}
public FamilyTreeNode addPartner(){
currentNode = ancestor ;
FamilyTreeNode newPartnerNode = new FamilyTreeNode() ;
int currentNodeIdentifier;
int partnerIdentifier;
boolean insertPointFound = false ;
display();
partnerIdentifier = Input.getInteger("Input partner ID");
while(insertPointFound == false){
if(partnerIdentifier == currentNode.identifier){
}else{
currentNode
}
}
return newPartnerNode;
}
public void display(){
currentNode = ancestor;
System.out.println(currentNode.Name + " " + currentNode.identifier);
if(currentNode.child != null){
currentNode = currentNode.child;
System.out.println(currentNode.Name + " " + currentNode.identifier);
while(currentNode.sibling != null){
currentNode = currentNode.sibling;
System.out.println(currentNode.Name + " " + currentNode.identifier);
}
}
}
}