Advertisement
Guest User

Untitled

a guest
Jan 23rd, 2017
231
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.37 KB | None | 0 0
  1.  
  2. public class Tree1 {
  3.     class Node {
  4.  
  5.         private int elem;
  6.         private Node left;
  7.         private Node right;
  8.  
  9.         public Node(int elem) {
  10.             this.elem = elem;
  11.         }
  12.  
  13.         public void add(int el) {
  14.             if (el < this.elem) {
  15.                 if (this.left == null) {
  16.                     this.left = new Node(el);
  17.                 } else {
  18.                     this.left.add(el);
  19.                 }
  20.  
  21.             } else if (el >= this.elem) {
  22.                 if (this.right == null) {
  23.                     this.right = new Node(el);
  24.                 } else {
  25.                     this.right.add(el);
  26.                 }
  27.             }
  28.  
  29.         }
  30.  
  31.         Node copyTree() {
  32.             // TODO: Implementieren Sie die Methode
  33.             //TODO: Diese Zeile löschen oder entsprechend verändern.
  34.             Node copy = new Node(elem);
  35.             if (this.left != null) {
  36.                 copy.left = this.left.copyTree();
  37.             }
  38.             if (this.right != null) {
  39.                 copy.right = this.right.copyTree();
  40.             }
  41.  
  42.             return copy;
  43.  
  44.         }
  45.  
  46.         public String toString() {
  47.             return (this.left == null ? "" : this.left.toString() + ",")
  48.                     + this.elem + (this.right == null ? "" : "," + this.right.toString());
  49.         }
  50.     }
  51.  
  52.     private Node root;
  53.  
  54.     public void add(int elem) {
  55.         if (this.root == null) {
  56.             this.root = new Node(elem);
  57.         } else {
  58.             this.root.add(elem);
  59.         }
  60.     }
  61.  
  62.     // Diese Methode kopiert den vorhandenen Baum und erstellt eine exakte
  63.     // Kopie. Dafür sollen keine zusätzliche Datenstrukturen verwendet werden.
  64.     // Es wird der Wurzelknoten des kopierten Baumes zurückgegeben.
  65.     public Node copyTree() {
  66.  
  67.         if (this.root == null) {
  68.             return null;
  69.         }
  70.         return this.root.copyTree();
  71.     }
  72.  
  73.     public String toString() {
  74.         return this.root == null ? "()" :   this.root + "";
  75.     }
  76.  
  77.     public static void main(String[] args) {
  78.         Tree1 tree = new Tree1();
  79.         tree.add(10);
  80.         tree.add(12);
  81.         tree.add(11);
  82.         tree.add(5);
  83.         tree.add(1);
  84.         tree.add(6);
  85.         tree.add(14);
  86.  
  87.         System.out.println(tree);
  88.  
  89.         Node copy = tree.copyTree();
  90.         System.out.println(copy);
  91.  
  92.  
  93.  
  94.     }
  95. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement