mirror of
https://gitlab.com/harald.mueller/aktuelle.kurse.git
synced 2024-11-28 04:31:56 +01:00
58 lines
1.5 KiB
Java
58 lines
1.5 KiB
Java
package tree;
|
|
|
|
import java.util.ArrayList;
|
|
|
|
public class MyBinaryTreeTest {
|
|
|
|
public static void main(String[] args) {
|
|
//MyBinaryTree myBinaryTree = generateBinaryTree();
|
|
|
|
MyBinaryTree myBinaryTree = new MyBinaryTree();
|
|
int[] a = {6, 9, 3, 17, 2, 98, 55, 1, 32, 42, 15, 6, 7, 85, 10, 8};
|
|
for (int i=0; i<a.length; i++) {
|
|
myBinaryTree.insert(a[i]);
|
|
}
|
|
System.out.println("Tree populated.");
|
|
inOrderTraversal(myBinaryTree.getRoot());
|
|
System.out.println();
|
|
|
|
myBinaryTree.remove(1);
|
|
myBinaryTree.remove(3);
|
|
myBinaryTree.remove(17);
|
|
System.out.println("Max: [" + myBinaryTree.max() + "]");
|
|
System.out.println("Min: [" + myBinaryTree.min() + "]");
|
|
|
|
inOrderTraversal(myBinaryTree.getRoot());
|
|
|
|
|
|
}
|
|
|
|
private static void inOrderTraversal(MyBinaryTreeNode node) {
|
|
MyBinaryTreeNode current = node;
|
|
if (current.getLeft() != null) {
|
|
inOrderTraversal(current.getLeft());
|
|
}
|
|
System.out.print(current.getElement() + ", ");
|
|
if (current.getRight() != null) {
|
|
inOrderTraversal(current.getRight());
|
|
}
|
|
|
|
}
|
|
|
|
public static MyBinaryTree generateBinaryTree() {
|
|
MyBinaryTree myBinaryTree = new MyBinaryTree();
|
|
MyBinaryTreeNode root = new MyBinaryTreeNode(0);
|
|
myBinaryTree.setRoot(root);
|
|
|
|
root.setLeft(new MyBinaryTreeNode(1));
|
|
root.getLeft().setLeft(new MyBinaryTreeNode(2));
|
|
root.getLeft().setRight(new MyBinaryTreeNode(3));
|
|
|
|
root.setRight(new MyBinaryTreeNode(4));
|
|
root.getRight().setLeft(new MyBinaryTreeNode(5));
|
|
root.getRight().setRight(new MyBinaryTreeNode(6));
|
|
|
|
return myBinaryTree;
|
|
}
|
|
}
|