Hi erstmal ! Ich lerne grade ObjektOrientierte Programmierung und habe eine Übung bei der ich nicht weiterkomme. Unzwar soll ich einen binären SuchBaum als Datenstruktur zur Speicherung von Int-Werten implementieren. Die Haupteigenschaft ist, dass alle Elemente des linken Unterbaumes kleiner, alle Elemente des rechten Unterbaumes größer (oder – falls per Definition erlaubt – gleich) als das Objekt im aktuellen Knoten sein müssen. Ich selber verstehe nicht wie ich die Methoden für getparent, ToArray, PostOrder und PreOrder. Könnte mir jemand helfen wie ich diese Methoden schreiben muss ? Im Anhang habe ich Bilder meines bisher vorhandenen Codes. Schon mal vielen Dank im Vorraus
Hier das Beispiel:
public class BinarySearchTree {
/** Inner class for the binary tree node. **/
protected class BinaryTreeNode
{
public BinaryTreeNode left;
public BinaryTreeNode right;
public int data;
public BinaryTreeNode (int elem)
{
data = elem;
left = null;
right = null;
} }
/** Root node of the tree. **/
protected BinaryTreeNode root;
/** Number of elements stored in the tree. */
protected int size;
/** Inserts the given element. Duplicate elements are not allowed. Returns true if insertion was successful, false otherwise. */
public boolean insert (int elem) { ... }
/** Searches for the (first) element with the given key. Returns true if it could be found, false otherwise. */
public boolean find (int key) { ... }
/** Removes the element with the given key. Returns true if the key could be found (and removed), false otherwise. */
public boolean remove (int key) { ... }
/** Returns the number of elements stored in the tree. */
public int size () { ... }
/** Returns the parent element of the given key. Integer.MIN_VALUE if no parent can be found. */
public int getParent(int key) { ... }
/** Returns the elements of the tree in ascending (inorder traversal) or descending (reverse inorder traversal) order. */
public int[] toArray (boolean ascending) { ... }
/** Returns the elements of the tree (postorder traversal). */
public int[] toArrayPostOrder() { ... }
/** Returns the elements of the tree (preorder traversal). */
public int[] toArrayPreOrder() { ... }
/** Returns largest number stored in the tree. Integer.MIN_VALUE if no largest element can be found*/
public int max () { ... }
/** Returns smallest number stored in the tree. Integer.MIN_VALUE if no smallest element can be found */
public int min () { ... }
/** Represents the tree in a human readable form. */
public String toString() { ... }
}
Mit freundlichen Grüßen Marcel
Hier das Beispiel:
public class BinarySearchTree {
/** Inner class for the binary tree node. **/
protected class BinaryTreeNode
{
public BinaryTreeNode left;
public BinaryTreeNode right;
public int data;
public BinaryTreeNode (int elem)
{
data = elem;
left = null;
right = null;
} }
/** Root node of the tree. **/
protected BinaryTreeNode root;
/** Number of elements stored in the tree. */
protected int size;
/** Inserts the given element. Duplicate elements are not allowed. Returns true if insertion was successful, false otherwise. */
public boolean insert (int elem) { ... }
/** Searches for the (first) element with the given key. Returns true if it could be found, false otherwise. */
public boolean find (int key) { ... }
/** Removes the element with the given key. Returns true if the key could be found (and removed), false otherwise. */
public boolean remove (int key) { ... }
/** Returns the number of elements stored in the tree. */
public int size () { ... }
/** Returns the parent element of the given key. Integer.MIN_VALUE if no parent can be found. */
public int getParent(int key) { ... }
/** Returns the elements of the tree in ascending (inorder traversal) or descending (reverse inorder traversal) order. */
public int[] toArray (boolean ascending) { ... }
/** Returns the elements of the tree (postorder traversal). */
public int[] toArrayPostOrder() { ... }
/** Returns the elements of the tree (preorder traversal). */
public int[] toArrayPreOrder() { ... }
/** Returns largest number stored in the tree. Integer.MIN_VALUE if no largest element can be found*/
public int max () { ... }
/** Returns smallest number stored in the tree. Integer.MIN_VALUE if no smallest element can be found */
public int min () { ... }
/** Represents the tree in a human readable form. */
public String toString() { ... }
}
Mit freundlichen Grüßen Marcel