TIUI
asked on
Need help inserting objects into binary search tree
I am having trouble inserting more than one object into a binary search tree.
I have attached the code for both classes that I am working with. If someone can please explain
to me what it is I am doing wrong that would be great.
I have attached the code for both classes that I am working with. If someone can please explain
to me what it is I am doing wrong that would be great.
import java.util.*;
public class ClubMember {
String memberName;
int memberId;
public ClubMember() {}
public ClubMember(int id, String name) {
memberId = id;
memberName = name;
}
public int getId() {
return memberId;
}
public String getName() {
return memberName;
}
public String toString() {
return String.valueOf(memberId) + ": " + memberName;
}
public static void main(String[] args) {
new ClubMember().run();
}
public void run() {
ClubMember newMember;
int tempId, counter = 10;
Scanner input = new Scanner(System.in);
String userDecision, tempName;
BinaryTree.Node node = null;
BinaryTree tree = new BinaryTree();
for(int i = 0; i < counter; counter--) {
System.out.println("Enter Club member id: ");
tempId = input.nextInt();
System.out.println("Enter Club member name: ");
tempName = input.next();
newMember = new ClubMember(tempId, tempName);
node = new BinaryTree.Node(newMember);
tree.insert(node, newMember);
tree.printInOrder(node);
}
}
}
public class BinaryTree {
static class Node {
Node left, right;
ClubMember value;
public Node(ClubMember value) {
this.value = value;
}
}
public void insert(Node node, ClubMember value) {
if(value.getId() < node.value.getId()) {
if(node.left != null) {
insert(node.left, value);
}else{
System.out.println("Inserted " + value +
" to left of " + node.value);
node.left = new Node(value);
}
}else if (value.getId() > node.value.getId()) {
if(node.right != null) {
insert(node.right, value);
}else{
System.out.println("Inserted " + value +
" to right of " + node.value);
node.right = new Node(value);
}
}
}
public void printInOrder(Node node) {
if(node != null) {
printInOrder(node.left);
System.out.println(node.value);
printInOrder(node.right);
}
}
}
ASKER
Sorry about that I should have put that in the original question.
The app should allow a user to enter in a person's name and an id and store them based on the id in a binary search tree. It should also be able to print out the names with their ids in order. There is no specified number of people to be stored, so I just made it loop through 10 times.
The app should allow a user to enter in a person's name and an id and store them based on the id in a binary search tree. It should also be able to print out the names with their ids in order. There is no specified number of people to be stored, so I just made it loop through 10 times.
for the firast memeber both conditions are not satisfied:
value.getId() < node.value.getId())
and
value.getId() > node.value.getId())
so it was not inserting anything
value.getId() < node.value.getId())
and
value.getId() > node.value.getId())
so it was not inserting anything
ASKER
And that is where I am having the problem. I am new to trees and I really am not sure how to fix this. Any suggestions?
This code from http://www.daniweb.com/software-development/java/threads/353400
is working (see output).
Study it and you'll udenrstandt how to correct yours
One thing - it seem like root node should be inserted in a different way
form all the others
public class BinaryTreeTest {
public static void main(String[] args) {
new BinaryTreeTest().run();
}
static class Node {
Node left;
Node right;
int value;
public Node(int value) {
this.value = value;
}
}
public void run() {
// build the simple <strong class="highlight">tree</strong> from chapter 11.
Node root = new Node(5);
// System.out.println("<strong class=\"highlight">Binary</strong> <strong class="highlight">Tree</strong> Example");
System.out.println("Building with root value " + root.value);
insert(root, 1);
insert(root, 8);
insert(root, 6);
insert(root, 3);
insert(root, 9);
System.out.println("Traversing on order");
printInOrder(root);
System.out.println("Traversing front-to-back from location 7");
printFrontToBack(root, 7);
}
public void insert(Node node, int value) {
if (value < node.value) {
if (node.left != null) {
insert(node.left, value);
} else {
System.out.println(" Inserted " + value + " to left of "
+ node.value);
node.left = new Node(value);
}
} else if (value > node.value) {
if (node.right != null) {
insert(node.right, value);
} else {
System.out.println(" Inserted " + value + " to right of "
+ node.value);
node.right = new Node(value);
}
}
}
public void printInOrder(Node node) {
if (node != null) {
printInOrder(node.left);
System.out.println(" Traversed " + node.value);
printInOrder(node.right);
}
}
/**
* uses in-order traversal when the origin is less than the node's value
*
* uses reverse-order traversal when the origin is greater than the node's
* order
*/
public void printFrontToBack(Node node, int camera) {
if (node == null)
return;
if (node.value > camera) {
// print <strong class="highlight">in</strong> order
printFrontToBack(node.left, camera);
System.out.println(" Traversed " + node.value);
printFrontToBack(node.right, camera);
} else if (node.value < camera) {
// print reverse order
printFrontToBack(node.right, camera);
System.out.println(" Traversed " + node.value);
printFrontToBack(node.left, camera);
} else {
// order doesn't matter
printFrontToBack(node.left, camera);
printFrontToBack(node.right, camera);
}
}
}
Output:
Building with root value 5
Inserted 1 to left of 5
Inserted 8 to right of 5
Inserted 6 to left of 8
Inserted 3 to right of 1
Inserted 9 to right of 8
Traversing on order
Traversed 1
Traversed 3
Traversed 5
Traversed 6
Traversed 8
Traversed 9
Traversing front-to-back from location 7
Traversed 6
Traversed 8
Traversed 9
Traversed 5
Traversed 3
Traversed 1
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Thank you so much, I was able to make the app. work using both solutions. I really appreciate the help.
It asks id and name and then again id and name, etc.
What should happen then?