String based Binary tree and insertion












1















The Binary tree does not use comparison rather, the user inputs the String name of the node they want to add the left or right child, if the node already has a child for either of the two and it will not overwrite it.



I'm having some difficulties, it doesn't stop it from overwriting a pre-existing Node and it does not always remembers it own child.



Please tell me am I missing something simple or if I need to redo everything and if so, how should I do it this time.



Thanks for the help in advanced.



public class Node {

private String name;
private Node leftChild;
private Node rightChild;
private Node parent;

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public Node getLeftChild() {
return leftChild;
}

public void setLeftChild(Node leftChild) {
this.leftChild = leftChild;
}

public Node getRightChild() {
return rightChild;
}

public void setRightChild(Node rightChild) {
this.rightChild = rightChild;
}

public Node getParent() {
return parent;
}

public void setParent(Node parent) {
this.parent = parent;
}

public void displayNode() // display ourselves{
System.out.println(name);
}

}

public class Tree {
private Node root;

public Tree() {
root = null;
}

public void insertRoot(String rootName) {
if (root == null) {
Node newNode = new Node();
newNode.setName(rootName);
root = newNode;
} else {
System.out.println("There is already a root");
}
}

public void insertLeftChild(String parentName, String childName) {
Node temp = new Node();
Node current = parent(parentName, root, temp);

if (current.getName() == null) {
System.out.println("No such parent exists");
} else if (current.getLeftChild() == null) {

Node newNode = new Node();
newNode.setName(childName);

current.displayNode();

newNode.setParent(current);

current.setLeftChild(newNode);
System.out.println("It worked");
} else if (current.getLeftChild() != null) {
System.out.println("They already have a left child");
}
}

public void insertRightChild(String parentName, String childName) {
Node temp = new Node();

Node current = parent(parentName, root, temp);

if (current.getName() == null) {
System.out.println("No such parent exists");
} else if (current.getRightChild() == null) {
Node newNode = new Node();
newNode.setName(childName);

newNode.setParent(current);
current.setRightChild(newNode);
} else if (current.getRightChild() != null) {
System.out.println("They already have a right child");
}
}

public Node parent(String parentName, Node current, Node found) {
if (current != null) {
if (current.getName().equals(parentName)) {
found.setName(parentName);
found.setParent(current.getParent());
found.setLeftChild(current.getLeftChild());
found.setRightChild(current.getRightChild());

return found;
}

parent(parentName, current.getLeftChild(), found);
parent(parentName, current.getRightChild(), found); // right child
}
return found;
}
}


Here is the main method



public class Demo {

public static void main(String args) {

Tree t = new Tree();

t.insertRoot("1");

t.insertLeftChild("1", "2");

t.insertLeftChild("2", "3");

t.insertLeftChild("3", "4");

t.insertLeftChild("1", "3");

t.insertRightChild("7", "8");
}
}


Here is the current results



1
It worked
No such parent exists
No such parent exists
1
It worked



"It worked" is a mark for if the program completes the left insertions
"1" is showing Node value of parent the new insertion is being add to










share|improve this question

























  • Please show main method and show your error code/outpur.

    – Mr.K
    Nov 16 '18 at 5:16













  • One Minute and I will add them

    – Icarus Will
    Nov 16 '18 at 5:21













  • In parent, Node found should be a local variable i.e. should be put inside the function. Tell me if this improves your problem

    – Mr.K
    Nov 16 '18 at 5:25











  • What you want to achieve exactly, explain in question

    – Tarun
    Nov 16 '18 at 5:52











  • Your requirements are not clear. Please explain clearly.

    – efex09
    Nov 16 '18 at 6:05


















1















The Binary tree does not use comparison rather, the user inputs the String name of the node they want to add the left or right child, if the node already has a child for either of the two and it will not overwrite it.



I'm having some difficulties, it doesn't stop it from overwriting a pre-existing Node and it does not always remembers it own child.



Please tell me am I missing something simple or if I need to redo everything and if so, how should I do it this time.



Thanks for the help in advanced.



public class Node {

private String name;
private Node leftChild;
private Node rightChild;
private Node parent;

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public Node getLeftChild() {
return leftChild;
}

public void setLeftChild(Node leftChild) {
this.leftChild = leftChild;
}

public Node getRightChild() {
return rightChild;
}

public void setRightChild(Node rightChild) {
this.rightChild = rightChild;
}

public Node getParent() {
return parent;
}

public void setParent(Node parent) {
this.parent = parent;
}

public void displayNode() // display ourselves{
System.out.println(name);
}

}

public class Tree {
private Node root;

public Tree() {
root = null;
}

public void insertRoot(String rootName) {
if (root == null) {
Node newNode = new Node();
newNode.setName(rootName);
root = newNode;
} else {
System.out.println("There is already a root");
}
}

public void insertLeftChild(String parentName, String childName) {
Node temp = new Node();
Node current = parent(parentName, root, temp);

if (current.getName() == null) {
System.out.println("No such parent exists");
} else if (current.getLeftChild() == null) {

Node newNode = new Node();
newNode.setName(childName);

current.displayNode();

newNode.setParent(current);

current.setLeftChild(newNode);
System.out.println("It worked");
} else if (current.getLeftChild() != null) {
System.out.println("They already have a left child");
}
}

public void insertRightChild(String parentName, String childName) {
Node temp = new Node();

Node current = parent(parentName, root, temp);

if (current.getName() == null) {
System.out.println("No such parent exists");
} else if (current.getRightChild() == null) {
Node newNode = new Node();
newNode.setName(childName);

newNode.setParent(current);
current.setRightChild(newNode);
} else if (current.getRightChild() != null) {
System.out.println("They already have a right child");
}
}

public Node parent(String parentName, Node current, Node found) {
if (current != null) {
if (current.getName().equals(parentName)) {
found.setName(parentName);
found.setParent(current.getParent());
found.setLeftChild(current.getLeftChild());
found.setRightChild(current.getRightChild());

return found;
}

parent(parentName, current.getLeftChild(), found);
parent(parentName, current.getRightChild(), found); // right child
}
return found;
}
}


Here is the main method



public class Demo {

public static void main(String args) {

Tree t = new Tree();

t.insertRoot("1");

t.insertLeftChild("1", "2");

t.insertLeftChild("2", "3");

t.insertLeftChild("3", "4");

t.insertLeftChild("1", "3");

t.insertRightChild("7", "8");
}
}


Here is the current results



1
It worked
No such parent exists
No such parent exists
1
It worked



"It worked" is a mark for if the program completes the left insertions
"1" is showing Node value of parent the new insertion is being add to










share|improve this question

























  • Please show main method and show your error code/outpur.

    – Mr.K
    Nov 16 '18 at 5:16













  • One Minute and I will add them

    – Icarus Will
    Nov 16 '18 at 5:21













  • In parent, Node found should be a local variable i.e. should be put inside the function. Tell me if this improves your problem

    – Mr.K
    Nov 16 '18 at 5:25











  • What you want to achieve exactly, explain in question

    – Tarun
    Nov 16 '18 at 5:52











  • Your requirements are not clear. Please explain clearly.

    – efex09
    Nov 16 '18 at 6:05
















1












1








1








The Binary tree does not use comparison rather, the user inputs the String name of the node they want to add the left or right child, if the node already has a child for either of the two and it will not overwrite it.



I'm having some difficulties, it doesn't stop it from overwriting a pre-existing Node and it does not always remembers it own child.



Please tell me am I missing something simple or if I need to redo everything and if so, how should I do it this time.



Thanks for the help in advanced.



public class Node {

private String name;
private Node leftChild;
private Node rightChild;
private Node parent;

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public Node getLeftChild() {
return leftChild;
}

public void setLeftChild(Node leftChild) {
this.leftChild = leftChild;
}

public Node getRightChild() {
return rightChild;
}

public void setRightChild(Node rightChild) {
this.rightChild = rightChild;
}

public Node getParent() {
return parent;
}

public void setParent(Node parent) {
this.parent = parent;
}

public void displayNode() // display ourselves{
System.out.println(name);
}

}

public class Tree {
private Node root;

public Tree() {
root = null;
}

public void insertRoot(String rootName) {
if (root == null) {
Node newNode = new Node();
newNode.setName(rootName);
root = newNode;
} else {
System.out.println("There is already a root");
}
}

public void insertLeftChild(String parentName, String childName) {
Node temp = new Node();
Node current = parent(parentName, root, temp);

if (current.getName() == null) {
System.out.println("No such parent exists");
} else if (current.getLeftChild() == null) {

Node newNode = new Node();
newNode.setName(childName);

current.displayNode();

newNode.setParent(current);

current.setLeftChild(newNode);
System.out.println("It worked");
} else if (current.getLeftChild() != null) {
System.out.println("They already have a left child");
}
}

public void insertRightChild(String parentName, String childName) {
Node temp = new Node();

Node current = parent(parentName, root, temp);

if (current.getName() == null) {
System.out.println("No such parent exists");
} else if (current.getRightChild() == null) {
Node newNode = new Node();
newNode.setName(childName);

newNode.setParent(current);
current.setRightChild(newNode);
} else if (current.getRightChild() != null) {
System.out.println("They already have a right child");
}
}

public Node parent(String parentName, Node current, Node found) {
if (current != null) {
if (current.getName().equals(parentName)) {
found.setName(parentName);
found.setParent(current.getParent());
found.setLeftChild(current.getLeftChild());
found.setRightChild(current.getRightChild());

return found;
}

parent(parentName, current.getLeftChild(), found);
parent(parentName, current.getRightChild(), found); // right child
}
return found;
}
}


Here is the main method



public class Demo {

public static void main(String args) {

Tree t = new Tree();

t.insertRoot("1");

t.insertLeftChild("1", "2");

t.insertLeftChild("2", "3");

t.insertLeftChild("3", "4");

t.insertLeftChild("1", "3");

t.insertRightChild("7", "8");
}
}


Here is the current results



1
It worked
No such parent exists
No such parent exists
1
It worked



"It worked" is a mark for if the program completes the left insertions
"1" is showing Node value of parent the new insertion is being add to










share|improve this question
















The Binary tree does not use comparison rather, the user inputs the String name of the node they want to add the left or right child, if the node already has a child for either of the two and it will not overwrite it.



I'm having some difficulties, it doesn't stop it from overwriting a pre-existing Node and it does not always remembers it own child.



Please tell me am I missing something simple or if I need to redo everything and if so, how should I do it this time.



Thanks for the help in advanced.



public class Node {

private String name;
private Node leftChild;
private Node rightChild;
private Node parent;

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public Node getLeftChild() {
return leftChild;
}

public void setLeftChild(Node leftChild) {
this.leftChild = leftChild;
}

public Node getRightChild() {
return rightChild;
}

public void setRightChild(Node rightChild) {
this.rightChild = rightChild;
}

public Node getParent() {
return parent;
}

public void setParent(Node parent) {
this.parent = parent;
}

public void displayNode() // display ourselves{
System.out.println(name);
}

}

public class Tree {
private Node root;

public Tree() {
root = null;
}

public void insertRoot(String rootName) {
if (root == null) {
Node newNode = new Node();
newNode.setName(rootName);
root = newNode;
} else {
System.out.println("There is already a root");
}
}

public void insertLeftChild(String parentName, String childName) {
Node temp = new Node();
Node current = parent(parentName, root, temp);

if (current.getName() == null) {
System.out.println("No such parent exists");
} else if (current.getLeftChild() == null) {

Node newNode = new Node();
newNode.setName(childName);

current.displayNode();

newNode.setParent(current);

current.setLeftChild(newNode);
System.out.println("It worked");
} else if (current.getLeftChild() != null) {
System.out.println("They already have a left child");
}
}

public void insertRightChild(String parentName, String childName) {
Node temp = new Node();

Node current = parent(parentName, root, temp);

if (current.getName() == null) {
System.out.println("No such parent exists");
} else if (current.getRightChild() == null) {
Node newNode = new Node();
newNode.setName(childName);

newNode.setParent(current);
current.setRightChild(newNode);
} else if (current.getRightChild() != null) {
System.out.println("They already have a right child");
}
}

public Node parent(String parentName, Node current, Node found) {
if (current != null) {
if (current.getName().equals(parentName)) {
found.setName(parentName);
found.setParent(current.getParent());
found.setLeftChild(current.getLeftChild());
found.setRightChild(current.getRightChild());

return found;
}

parent(parentName, current.getLeftChild(), found);
parent(parentName, current.getRightChild(), found); // right child
}
return found;
}
}


Here is the main method



public class Demo {

public static void main(String args) {

Tree t = new Tree();

t.insertRoot("1");

t.insertLeftChild("1", "2");

t.insertLeftChild("2", "3");

t.insertLeftChild("3", "4");

t.insertLeftChild("1", "3");

t.insertRightChild("7", "8");
}
}


Here is the current results



1
It worked
No such parent exists
No such parent exists
1
It worked



"It worked" is a mark for if the program completes the left insertions
"1" is showing Node value of parent the new insertion is being add to







java data-structures binary-tree






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 16 '18 at 6:32









cricket_007

83.7k1147117




83.7k1147117










asked Nov 16 '18 at 5:04









Icarus WillIcarus Will

104




104













  • Please show main method and show your error code/outpur.

    – Mr.K
    Nov 16 '18 at 5:16













  • One Minute and I will add them

    – Icarus Will
    Nov 16 '18 at 5:21













  • In parent, Node found should be a local variable i.e. should be put inside the function. Tell me if this improves your problem

    – Mr.K
    Nov 16 '18 at 5:25











  • What you want to achieve exactly, explain in question

    – Tarun
    Nov 16 '18 at 5:52











  • Your requirements are not clear. Please explain clearly.

    – efex09
    Nov 16 '18 at 6:05





















  • Please show main method and show your error code/outpur.

    – Mr.K
    Nov 16 '18 at 5:16













  • One Minute and I will add them

    – Icarus Will
    Nov 16 '18 at 5:21













  • In parent, Node found should be a local variable i.e. should be put inside the function. Tell me if this improves your problem

    – Mr.K
    Nov 16 '18 at 5:25











  • What you want to achieve exactly, explain in question

    – Tarun
    Nov 16 '18 at 5:52











  • Your requirements are not clear. Please explain clearly.

    – efex09
    Nov 16 '18 at 6:05



















Please show main method and show your error code/outpur.

– Mr.K
Nov 16 '18 at 5:16







Please show main method and show your error code/outpur.

– Mr.K
Nov 16 '18 at 5:16















One Minute and I will add them

– Icarus Will
Nov 16 '18 at 5:21







One Minute and I will add them

– Icarus Will
Nov 16 '18 at 5:21















In parent, Node found should be a local variable i.e. should be put inside the function. Tell me if this improves your problem

– Mr.K
Nov 16 '18 at 5:25





In parent, Node found should be a local variable i.e. should be put inside the function. Tell me if this improves your problem

– Mr.K
Nov 16 '18 at 5:25













What you want to achieve exactly, explain in question

– Tarun
Nov 16 '18 at 5:52





What you want to achieve exactly, explain in question

– Tarun
Nov 16 '18 at 5:52













Your requirements are not clear. Please explain clearly.

– efex09
Nov 16 '18 at 6:05







Your requirements are not clear. Please explain clearly.

– efex09
Nov 16 '18 at 6:05














1 Answer
1






active

oldest

votes


















0














I have updated the logic to find the parent node. It will return null if it does not exist else it will return the parent node.



public Node parent(String parentName, Node root) {
if (root != null) {
if (root.getName().equals(parentName)) {
return root;
} else {
Node found = parent(parentName, root.getLeftChild());
if (found == null) {
found = parent(parentName, root.getRightChild());
}
return found;
}
} else {
return null;
}
}


Full code..



class Node {

private String name;
private Node leftChild;
private Node rightChild;
private Node parent;

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public Node getLeftChild() {
return leftChild;
}

public void setLeftChild(Node leftChild) {
this.leftChild = leftChild;
}

public Node getRightChild() {
return rightChild;
}

public void setRightChild(Node rightChild) {
this.rightChild = rightChild;
}

public Node getParent() {
return parent;
}

public void setParent(Node parent) {
this.parent = parent;
}

public void displayNode() {

System.out.println(name);
}

}



public class Tree {

private Node root;

public Tree() {
root = null;
}

public void insertRoot(String rootName) {
if (root == null) {
Node newNode = new Node();
newNode.setName(rootName);
root = newNode;
} else {
System.out.println("There is already a root");
}
}

public void insertLeftChild(String parentName, String childName) {
Node parent = parent(parentName, root);

if (parent == null) {
System.out.println("No such parent exists");
} else if (parent.getLeftChild() == null) {

Node newNode = new Node();
newNode.setName(childName);

parent.displayNode();

newNode.setParent(parent);

parent.setLeftChild(newNode);
System.out.println("It worked");
} else if (parent.getLeftChild() != null) {
System.out.println("They already have a left child");
}
}

public void insertRightChild(String parentName, String childName) {

Node parent = parent(parentName, root);

if (parent == null) {
System.out.println("No such parent exists");
} else if (parent.getRightChild() == null) {
Node newNode = new Node();
newNode.setName(childName);

newNode.setParent(parent);
parent.setRightChild(newNode);
} else if (parent.getRightChild() != null) {
System.out.println("They already have a right child");
}
}

public Node parent(String parentName, Node root) {
if (root != null) {
if (root.getName().equals(parentName)) {
return root;
} else {
Node found = parent(parentName, root.getLeftChild());
if (found == null) {
found = parent(parentName, root.getRightChild());
}
return found;
}
} else {
return null;
}
}

public static void main(String args) {

Tree t = new Tree();

t.insertRoot("1");

t.insertLeftChild("1", "2");

t.insertLeftChild("2", "3");

t.insertLeftChild("3", "4");

t.insertLeftChild("1", "3");

t.insertRightChild("7", "8");
}
}


Output..




1
It worked
2
It worked
3
It worked
They already have a left child
No such parent exists





share|improve this answer
























    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53331741%2fstring-based-binary-tree-and-insertion%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    0














    I have updated the logic to find the parent node. It will return null if it does not exist else it will return the parent node.



    public Node parent(String parentName, Node root) {
    if (root != null) {
    if (root.getName().equals(parentName)) {
    return root;
    } else {
    Node found = parent(parentName, root.getLeftChild());
    if (found == null) {
    found = parent(parentName, root.getRightChild());
    }
    return found;
    }
    } else {
    return null;
    }
    }


    Full code..



    class Node {

    private String name;
    private Node leftChild;
    private Node rightChild;
    private Node parent;

    public String getName() {
    return name;
    }

    public void setName(String name) {
    this.name = name;
    }

    public Node getLeftChild() {
    return leftChild;
    }

    public void setLeftChild(Node leftChild) {
    this.leftChild = leftChild;
    }

    public Node getRightChild() {
    return rightChild;
    }

    public void setRightChild(Node rightChild) {
    this.rightChild = rightChild;
    }

    public Node getParent() {
    return parent;
    }

    public void setParent(Node parent) {
    this.parent = parent;
    }

    public void displayNode() {

    System.out.println(name);
    }

    }



    public class Tree {

    private Node root;

    public Tree() {
    root = null;
    }

    public void insertRoot(String rootName) {
    if (root == null) {
    Node newNode = new Node();
    newNode.setName(rootName);
    root = newNode;
    } else {
    System.out.println("There is already a root");
    }
    }

    public void insertLeftChild(String parentName, String childName) {
    Node parent = parent(parentName, root);

    if (parent == null) {
    System.out.println("No such parent exists");
    } else if (parent.getLeftChild() == null) {

    Node newNode = new Node();
    newNode.setName(childName);

    parent.displayNode();

    newNode.setParent(parent);

    parent.setLeftChild(newNode);
    System.out.println("It worked");
    } else if (parent.getLeftChild() != null) {
    System.out.println("They already have a left child");
    }
    }

    public void insertRightChild(String parentName, String childName) {

    Node parent = parent(parentName, root);

    if (parent == null) {
    System.out.println("No such parent exists");
    } else if (parent.getRightChild() == null) {
    Node newNode = new Node();
    newNode.setName(childName);

    newNode.setParent(parent);
    parent.setRightChild(newNode);
    } else if (parent.getRightChild() != null) {
    System.out.println("They already have a right child");
    }
    }

    public Node parent(String parentName, Node root) {
    if (root != null) {
    if (root.getName().equals(parentName)) {
    return root;
    } else {
    Node found = parent(parentName, root.getLeftChild());
    if (found == null) {
    found = parent(parentName, root.getRightChild());
    }
    return found;
    }
    } else {
    return null;
    }
    }

    public static void main(String args) {

    Tree t = new Tree();

    t.insertRoot("1");

    t.insertLeftChild("1", "2");

    t.insertLeftChild("2", "3");

    t.insertLeftChild("3", "4");

    t.insertLeftChild("1", "3");

    t.insertRightChild("7", "8");
    }
    }


    Output..




    1
    It worked
    2
    It worked
    3
    It worked
    They already have a left child
    No such parent exists





    share|improve this answer




























      0














      I have updated the logic to find the parent node. It will return null if it does not exist else it will return the parent node.



      public Node parent(String parentName, Node root) {
      if (root != null) {
      if (root.getName().equals(parentName)) {
      return root;
      } else {
      Node found = parent(parentName, root.getLeftChild());
      if (found == null) {
      found = parent(parentName, root.getRightChild());
      }
      return found;
      }
      } else {
      return null;
      }
      }


      Full code..



      class Node {

      private String name;
      private Node leftChild;
      private Node rightChild;
      private Node parent;

      public String getName() {
      return name;
      }

      public void setName(String name) {
      this.name = name;
      }

      public Node getLeftChild() {
      return leftChild;
      }

      public void setLeftChild(Node leftChild) {
      this.leftChild = leftChild;
      }

      public Node getRightChild() {
      return rightChild;
      }

      public void setRightChild(Node rightChild) {
      this.rightChild = rightChild;
      }

      public Node getParent() {
      return parent;
      }

      public void setParent(Node parent) {
      this.parent = parent;
      }

      public void displayNode() {

      System.out.println(name);
      }

      }



      public class Tree {

      private Node root;

      public Tree() {
      root = null;
      }

      public void insertRoot(String rootName) {
      if (root == null) {
      Node newNode = new Node();
      newNode.setName(rootName);
      root = newNode;
      } else {
      System.out.println("There is already a root");
      }
      }

      public void insertLeftChild(String parentName, String childName) {
      Node parent = parent(parentName, root);

      if (parent == null) {
      System.out.println("No such parent exists");
      } else if (parent.getLeftChild() == null) {

      Node newNode = new Node();
      newNode.setName(childName);

      parent.displayNode();

      newNode.setParent(parent);

      parent.setLeftChild(newNode);
      System.out.println("It worked");
      } else if (parent.getLeftChild() != null) {
      System.out.println("They already have a left child");
      }
      }

      public void insertRightChild(String parentName, String childName) {

      Node parent = parent(parentName, root);

      if (parent == null) {
      System.out.println("No such parent exists");
      } else if (parent.getRightChild() == null) {
      Node newNode = new Node();
      newNode.setName(childName);

      newNode.setParent(parent);
      parent.setRightChild(newNode);
      } else if (parent.getRightChild() != null) {
      System.out.println("They already have a right child");
      }
      }

      public Node parent(String parentName, Node root) {
      if (root != null) {
      if (root.getName().equals(parentName)) {
      return root;
      } else {
      Node found = parent(parentName, root.getLeftChild());
      if (found == null) {
      found = parent(parentName, root.getRightChild());
      }
      return found;
      }
      } else {
      return null;
      }
      }

      public static void main(String args) {

      Tree t = new Tree();

      t.insertRoot("1");

      t.insertLeftChild("1", "2");

      t.insertLeftChild("2", "3");

      t.insertLeftChild("3", "4");

      t.insertLeftChild("1", "3");

      t.insertRightChild("7", "8");
      }
      }


      Output..




      1
      It worked
      2
      It worked
      3
      It worked
      They already have a left child
      No such parent exists





      share|improve this answer


























        0












        0








        0







        I have updated the logic to find the parent node. It will return null if it does not exist else it will return the parent node.



        public Node parent(String parentName, Node root) {
        if (root != null) {
        if (root.getName().equals(parentName)) {
        return root;
        } else {
        Node found = parent(parentName, root.getLeftChild());
        if (found == null) {
        found = parent(parentName, root.getRightChild());
        }
        return found;
        }
        } else {
        return null;
        }
        }


        Full code..



        class Node {

        private String name;
        private Node leftChild;
        private Node rightChild;
        private Node parent;

        public String getName() {
        return name;
        }

        public void setName(String name) {
        this.name = name;
        }

        public Node getLeftChild() {
        return leftChild;
        }

        public void setLeftChild(Node leftChild) {
        this.leftChild = leftChild;
        }

        public Node getRightChild() {
        return rightChild;
        }

        public void setRightChild(Node rightChild) {
        this.rightChild = rightChild;
        }

        public Node getParent() {
        return parent;
        }

        public void setParent(Node parent) {
        this.parent = parent;
        }

        public void displayNode() {

        System.out.println(name);
        }

        }



        public class Tree {

        private Node root;

        public Tree() {
        root = null;
        }

        public void insertRoot(String rootName) {
        if (root == null) {
        Node newNode = new Node();
        newNode.setName(rootName);
        root = newNode;
        } else {
        System.out.println("There is already a root");
        }
        }

        public void insertLeftChild(String parentName, String childName) {
        Node parent = parent(parentName, root);

        if (parent == null) {
        System.out.println("No such parent exists");
        } else if (parent.getLeftChild() == null) {

        Node newNode = new Node();
        newNode.setName(childName);

        parent.displayNode();

        newNode.setParent(parent);

        parent.setLeftChild(newNode);
        System.out.println("It worked");
        } else if (parent.getLeftChild() != null) {
        System.out.println("They already have a left child");
        }
        }

        public void insertRightChild(String parentName, String childName) {

        Node parent = parent(parentName, root);

        if (parent == null) {
        System.out.println("No such parent exists");
        } else if (parent.getRightChild() == null) {
        Node newNode = new Node();
        newNode.setName(childName);

        newNode.setParent(parent);
        parent.setRightChild(newNode);
        } else if (parent.getRightChild() != null) {
        System.out.println("They already have a right child");
        }
        }

        public Node parent(String parentName, Node root) {
        if (root != null) {
        if (root.getName().equals(parentName)) {
        return root;
        } else {
        Node found = parent(parentName, root.getLeftChild());
        if (found == null) {
        found = parent(parentName, root.getRightChild());
        }
        return found;
        }
        } else {
        return null;
        }
        }

        public static void main(String args) {

        Tree t = new Tree();

        t.insertRoot("1");

        t.insertLeftChild("1", "2");

        t.insertLeftChild("2", "3");

        t.insertLeftChild("3", "4");

        t.insertLeftChild("1", "3");

        t.insertRightChild("7", "8");
        }
        }


        Output..




        1
        It worked
        2
        It worked
        3
        It worked
        They already have a left child
        No such parent exists





        share|improve this answer













        I have updated the logic to find the parent node. It will return null if it does not exist else it will return the parent node.



        public Node parent(String parentName, Node root) {
        if (root != null) {
        if (root.getName().equals(parentName)) {
        return root;
        } else {
        Node found = parent(parentName, root.getLeftChild());
        if (found == null) {
        found = parent(parentName, root.getRightChild());
        }
        return found;
        }
        } else {
        return null;
        }
        }


        Full code..



        class Node {

        private String name;
        private Node leftChild;
        private Node rightChild;
        private Node parent;

        public String getName() {
        return name;
        }

        public void setName(String name) {
        this.name = name;
        }

        public Node getLeftChild() {
        return leftChild;
        }

        public void setLeftChild(Node leftChild) {
        this.leftChild = leftChild;
        }

        public Node getRightChild() {
        return rightChild;
        }

        public void setRightChild(Node rightChild) {
        this.rightChild = rightChild;
        }

        public Node getParent() {
        return parent;
        }

        public void setParent(Node parent) {
        this.parent = parent;
        }

        public void displayNode() {

        System.out.println(name);
        }

        }



        public class Tree {

        private Node root;

        public Tree() {
        root = null;
        }

        public void insertRoot(String rootName) {
        if (root == null) {
        Node newNode = new Node();
        newNode.setName(rootName);
        root = newNode;
        } else {
        System.out.println("There is already a root");
        }
        }

        public void insertLeftChild(String parentName, String childName) {
        Node parent = parent(parentName, root);

        if (parent == null) {
        System.out.println("No such parent exists");
        } else if (parent.getLeftChild() == null) {

        Node newNode = new Node();
        newNode.setName(childName);

        parent.displayNode();

        newNode.setParent(parent);

        parent.setLeftChild(newNode);
        System.out.println("It worked");
        } else if (parent.getLeftChild() != null) {
        System.out.println("They already have a left child");
        }
        }

        public void insertRightChild(String parentName, String childName) {

        Node parent = parent(parentName, root);

        if (parent == null) {
        System.out.println("No such parent exists");
        } else if (parent.getRightChild() == null) {
        Node newNode = new Node();
        newNode.setName(childName);

        newNode.setParent(parent);
        parent.setRightChild(newNode);
        } else if (parent.getRightChild() != null) {
        System.out.println("They already have a right child");
        }
        }

        public Node parent(String parentName, Node root) {
        if (root != null) {
        if (root.getName().equals(parentName)) {
        return root;
        } else {
        Node found = parent(parentName, root.getLeftChild());
        if (found == null) {
        found = parent(parentName, root.getRightChild());
        }
        return found;
        }
        } else {
        return null;
        }
        }

        public static void main(String args) {

        Tree t = new Tree();

        t.insertRoot("1");

        t.insertLeftChild("1", "2");

        t.insertLeftChild("2", "3");

        t.insertLeftChild("3", "4");

        t.insertLeftChild("1", "3");

        t.insertRightChild("7", "8");
        }
        }


        Output..




        1
        It worked
        2
        It worked
        3
        It worked
        They already have a left child
        No such parent exists






        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 16 '18 at 6:30









        efex09efex09

        19711




        19711
































            draft saved

            draft discarded




















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53331741%2fstring-based-binary-tree-and-insertion%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            Florida Star v. B. J. F.

            Danny Elfman

            Retrieve a Users Dashboard in Tumblr with R and TumblR. Oauth Issues