Hello,
I have to do some tasks for school according linked lists and trees in Java.
I have a Main Class with a Tree that looks like this (first is the name, second a "phonenumber"):
And in another class called BinTree I have a method for inserting entrys and one for putting the list in order.
Here's the code:
okay, that works! The task I can't finish is the remove method. I have to look for a name in the list (public void remove(String name) {}) and then this name should be removed.
And I also already know the way I want to do it: I want to get the previous ContactNode, where I already have a variable (somewhere) and then put the one next in row from my current ContactNode (which would be the one at the right, so something with getRight()... if I'm understanding it correctly) as the next node.
But I just can't get the freaking syntax right! &"!($("@ :grr:
I know I have to make an if-statement looking somehow like the one above:
I am comparing the name of the current node to the one I got through the remove() method, and if they match something should happen. The currentNode should get removed!
Please, if anybody can help me out here I would be really grateful!
All the best
Jacky
I have to do some tasks for school according linked lists and trees in Java.
I have a Main Class with a Tree that looks like this (first is the name, second a "phonenumber"):
Java:
public class Main {
public static void main(String[] args) {
BinTree bt = new BinTree();
bt.insert("Maier", "123");
bt.insert("Huber", "234");
bt.insert("Zwinger", "345");
bt.insert("Düringer", "456");
bt.insert("Mustermann", "567");
bt.insert("Bert", "678");
bt.insert("Sochi", "789");
bt.insert("Vetter", "890");
bt.inorder();
}
}
And in another class called BinTree I have a method for inserting entrys and one for putting the list in order.
Here's the code:
Java:
public class BinTree {
private ContactNode root;
public void insert (String name, String phone) {
ContactNode n = new ContactNode();
n.setName(name);
n.setPhone(phone);
ContactNode prev = null;
ContactNode p = root;
while(p != null) {
prev = p;
if(n.getName().compareTo(p.getName()) < 0) {
p = p.getLeft();
} else {
p = p.getRight();
}
}
if(prev == null) {
root = n;
} else {
if(n.getName().compareTo(prev.getName()) < 0) {
prev.setLeft(n);
} else {
prev.setRight(n);
}
}
}
public void inorder () {
inorder(root);
}
private void inorder(ContactNode p) {
if(p!=null) {
inorder(p.getLeft());
System.out.println(p.getName() + " (" + p.getPhone() + ")");
inorder(p.getRight());
}
}
}
okay, that works! The task I can't finish is the remove method. I have to look for a name in the list (public void remove(String name) {}) and then this name should be removed.
And I also already know the way I want to do it: I want to get the previous ContactNode, where I already have a variable (somewhere) and then put the one next in row from my current ContactNode (which would be the one at the right, so something with getRight()... if I'm understanding it correctly) as the next node.
But I just can't get the freaking syntax right! &"!($("@ :grr:
I know I have to make an if-statement looking somehow like the one above:
Code:
if(n.getName().compareTo(name()) < 0) {
//do something
}
I am comparing the name of the current node to the one I got through the remove() method, and if they match something should happen. The currentNode should get removed!
Please, if anybody can help me out here I would be really grateful!
All the best
Jacky
Zuletzt bearbeitet von einem Moderator: