class SubTree {
private SubTree rightTree = null;
private SubTree leftTree = null;
private SubTree parent = null;
private int value = 0;
public SubTree(int value) {
this.value = value;
}
public SubTree(int value, SubTree parent) {
this.value = value;
this.parent = parent;
}
public void insertValue(int newValue) {
if (value >= newValue) {
if (leftTree != null) {
leftTree.insertValue(newValue);
} else {
leftTree = new SubTree(newValue, this);
}
} else {
if (rightTree != null) {
rightTree.insertValue(newValue);
} else {
rightTree = new SubTree(newValue, this);
}
}
}
public String toString () {
String returString = "";
if (leftTree != null) {
returString = leftTree.toString() + " ";
}
returString = returString + value;
if (rightTree != null) {
returString = returString + " " + rightTree.toString();
}
showObject();
return returString;
}
public boolean lookForValue(int søkeVerdi) {
if (søkeVerdi == value) {
return true;
}
if (value > søkeVerdi) {
if (leftTree != null) {
return leftTree.lookForValue(søkeVerdi);
} else {
return false;
}
} else {
if (rightTree != null) {
return rightTree.lookForValue(søkeVerdi);
} else {
return false;
}
}
}
public boolean deleteValue(int value) {
if (this.value == value) {
//If we have found node with right value, we have to find if it has any child nodes
if ((rightTree == null) && (leftTree == null)) {
/**This node has no child, it can be linked off the tree.*/
if(value <= this.parent.getValue()) { //We are on left side, and we will relink there at the parent
this.parent.relink(0, null);
} else { //We are on the right side, and we relink there at the parent
this.parent.relink(1, null);
}
//At the end we relink this objects backreference to parent:
this.parent = null;
return true;
} else if (((rightTree == null) && (leftTree != null)) || ((rightTree != null) && (leftTree == null))) {
/**The node has only one child node. We link over this node to the child */
if(value <= this.parent.getValue()) { //We are on left side, and we link there over this object to this objects rightTree
if ((rightTree != null) && (leftTree == null)) this.parent.relink(0, this.rightTree);
if ((rightTree == null) && (leftTree != null)) this.parent.relink(0, this.leftTree);
this.rightTree = null;
} else { //Or else we are on the right side, and we link there over this object to this objects leftTree
if ((rightTree != null) && (leftTree == null)) this.parent.relink(1, this.rightTree);
if ((rightTree == null) && (leftTree != null)) this.parent.relink(1, this.leftTree);
this.leftTree = null;
}
//At the end we relink this objects reference to the parent:
this.parent = null;
return true;
} else {
/**The noden has two child nodes, these have to moved correctly and placed correctly under the parent node */
if(value <= this.parent.getValue()) { //We are on the left side, we link right child node on left side at the parent
//We then ask the right child node to find new space for the left childnode
this.parent.relink(0, this.rightTree);
this.rightTree.giveNewPlace(this.leftTree);
this.rightTree = null;
this.leftTree = null;
this.parent = null;
} else { //Or else we are on the right side, and we link right child node on the right side at the parent
this.parent.relink(1, this.rightTree);
this.rightTree.giveNewPlace(this.leftTree);
this.rightTree = null;
this.leftTree = null;
this.parent = null;
}
this.parent = null;
return true;
}
} else {
//If we have not foumd the node with the correct value, we will look for the correct child note and look there:
if(this.value < value) {
if (rightTree != null) rightTree.deleteValue(value);
} else {
if (leftTree != null) leftTree.deleteValue(value);
}
}
return false;
}
public int getValue() {
return this.value;
}
public void relink(int i, SubTree t) {
if (i > 0) {
this.rightTree = t;
} else {
this.leftTree = t;
}
t.setNewParent(this);
}
public SubTree getChildNode(int i) {
if (i > 0) {
return this.rightTree;
} else {
return this.leftTree;
}
}
public void giveNewPlace(SubTree t) {
//Takes SubTree t and tries to give it a new place in the tree
//Check if this node has space for a left child node, if not:
//Check if this node has space for a right child node and order the two childnodes correct, if not:
//Ask the next left childNode to give new place with its giveNewPlace().
if (this.leftTree == null) {
this.leftTree = t;
t.setNewParent(this);
} else if (this.rightTree == null) {
if(getValue() < t.getValue()) {
this.rightTree = t;
t.setNewParent(this);
} else {
this.rightTree = this.leftTree;
this.leftTree = t;
t.setNewParent(this);
}
} else {
this.leftTree.giveNewPlace(t);
}
}
public void setNewParent(SubTree t) {
this.parent = t;
}
public void showObject() {
System.out.println("----------------------------------------");
if (parent != null) System.out.println("Parent: " + parent.getValue());
System.out.println("*Value: " + this.value);
if (rightTree != null) System.out.println("Right child: " + rightTree.getValue());
if (leftTree != null) System.out.println("Left child: " + leftTree.getValue());
System.out.println("----------------------------------------");
}
}
class BinarySeekingTree {
private SubTree rot;
public String toString() {
if (rot != null) {
return rot.toString();
} else {
return null;
}
}
public void insertValue(int value) {
if (rot != null) {
rot.insertValue(value);
} else {
rot = new SubTree(value, null);
}
}
public boolean deleteValue(int value) {
//If there is no root in the tree, then there is no value to delete
if (rot == null) {
return false;
} else {
if(rot.deleteValue(value)) {
return true;
} else {
return false;
}
}
}
public boolean lookForValue(int søkeVerdi) {
if (rot == null) {
return false;
}
return rot.lookForValue(søkeVerdi);
}
}
class Seek {
public static void main(String[] args) {
BinarySeekingTree tree = new BinarySeekingTree();
tree.insertValue(15);
tree.insertValue(10);
tree.insertValue(28);
tree.insertValue(3);
tree.insertValue(14);
tree.insertValue(20);
tree.insertValue(29);
tree.insertValue(1);
tree.insertValue(4);
tree.insertValue(12);
tree.insertValue(13);
tree.insertValue(16);
tree.insertValue(21);
tree.insertValue(30);
tree.insertValue(39);
System.out.println("The tree in a sorted display: " + tree.toString()); //Sorting fails after deleting an object
System.out.println("Is 3 in the tree: " + tree.lookForValue(3));
System.out.println("Is 4 in the tree: " + tree.lookForValue(4));
System.out.println("Is 1 in the tree: " + tree.lookForValue(1));
System.out.println("Is 10 in the tree: " + tree.lookForValue(10));
System.out.println("Is 12 in the tree: " + tree.lookForValue(12));
System.out.println("Is 13 in the tree: " + tree.lookForValue(13));
System.out.println("Is 14 in the tree: " + tree.lookForValue(14));
System.out.println("Is 16 in the tree: " + tree.lookForValue(16));
System.out.println("Is 20 in the tree: " + tree.lookForValue(20));
System.out.println("Is 21 in the tree: " + tree.lookForValue(21));
System.out.println("Is 28 in the tree: " + tree.lookForValue(28));
System.out.println("Is 29 in the tree: " + tree.lookForValue(29));
System.out.println("Is 30 in the tree: " + tree.lookForValue(30));
System.out.println("Is 39 in the tree: " + tree.lookForValue(39));
System.out.println("We now try to delete the value 10 from the tree, but not 28.");
tree.deleteValue(10);
//tree.deleteValue(28);
System.out.println("");
System.out.println("The tree in a sorted display: " + tree.toString()); //Sorting fails after deleting an object
System.out.println("Is 3 in the tree: " + tree.lookForValue(3));
System.out.println("Is 4 in the tree: " + tree.lookForValue(4));
System.out.println("Is 1 in the tree: " + tree.lookForValue(1));
System.out.println("Is 10 in the tree: " + tree.lookForValue(10));
System.out.println("Is 12 in the tree: " + tree.lookForValue(12));
System.out.println("Is 13 in the tree: " + tree.lookForValue(13));
System.out.println("Is 14 in the tree: " + tree.lookForValue(14));
System.out.println("Is 16 in the tree: " + tree.lookForValue(16));
System.out.println("Is 20 in the tree: " + tree.lookForValue(20));
System.out.println("Is 21 in the tree: " + tree.lookForValue(21));
System.out.println("Is 28 in the tree: " + tree.lookForValue(28));
System.out.println("Is 29 in the tree: " + tree.lookForValue(29));
System.out.println("Is 30 in the tree: " + tree.lookForValue(30));
System.out.println("Is 39 in the tree: " + tree.lookForValue(39));
}
}
|