Also wir sollen einen Search Tree implementieren. Soweit so gut xD Jedoch sieht meine remove()-Methode ugly as fuck aus xD Habt ihr tipps die cleaner zu schreiben oder zu formatieren
Java:
@Override
public boolean remove(KeyType key) {
if(isEmpty()) {
return false;
}
TreeNode<KeyType,DataType> it = head;
while(it != null && it.getKey().compareTo(key) != 0) {
it = key.compareTo(it.getKey()) < 0 ? it.getLeftNode() : it.getRightNode();
}
if(it == null) {
return false;
}
if(it.getLeftNode() == null && it.getRightNode() == null) {
if(it.getKey().compareTo(it.getParentNode().getKey()) < 0) {
it.getParentNode().setLeftNode(null);;
}else {
it.getParentNode().setRightNode(null);;
}
it.setParentNode(null);
}else if(it.getLeftNode() != null && it.getRightNode() == null) {
if(it.getKey().compareTo(it.getParentNode().getKey()) < 0) {
it.getParentNode().setLeftNode(it.getLeftNode());;
}else {
it.getParentNode().setRightNode(it.getLeftNode());;
}
it.getLeftNode().setParentNode(it.getParentNode());
}else if(it.getLeftNode() == null && it.getRightNode() != null) {
if(it.getKey().compareTo(it.getParentNode().getKey()) < 0) {
it.getParentNode().setLeftNode(it.getRightNode());;
}else {
it.getParentNode().setRightNode(it.getRightNode());;
}
it.getRightNode().setParentNode(it.getParentNode());
}else {
//Still have to thing about this xD
}
size--;
return true;
}
Java:
class TreeNode<KeyType extends Comparable<? super KeyType>, DataType>{
private KeyType key;
private DataType data;
private TreeNode<KeyType,DataType> parent;
private TreeNode<KeyType,DataType> left;
private TreeNode<KeyType,DataType> right;
public TreeNode(KeyType key, DataType data) {
this.key = key;
this.data = data;
left = right = parent = null;
}
public KeyType getKey() {
return key;
}
public void setKey(KeyType key) {
this.key = key;
}
public DataType getData() {
return data;
}
public void setData(DataType data) {
this.data = data;
}
public TreeNode<KeyType,DataType> getParentNode(){
return parent;
}
public void setParentNode(TreeNode<KeyType,DataType> parent) {
this.parent = parent;
}
public TreeNode<KeyType,DataType> getLeftNode(){
return left;
}
public void setLeftNode(TreeNode<KeyType,DataType> left) {
this.left = left;
}
public TreeNode<KeyType,DataType> getRightNode(){
return right;
}
public void setRightNode(TreeNode<KeyType,DataType> right) {
this.right = right;
}
public String toString() {
return key+": " + data + " (left: " + (left == null ? null : left.key) + " right: " + (right == null ? null : right.key)+")";
}
}