Advertisement
crsandu

Untitled

Feb 5th, 2019
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.16 KB | None | 0 0
  1.  
  2. public class TreeSet <Type extends Comparable>{
  3.     private TreeSet rightChild = null; // initialily this is null
  4.     private TreeSet leftChild = null; // initiali this is null
  5.    
  6.     private Type value = null; // no value assigned yet
  7.    
  8.     public TreeSet(Type valoare) {
  9.         this.value = valoare;
  10.     }
  11.    
  12.     public TreeSet getRight() {
  13.         return this.rightChild;
  14.     }
  15.    
  16.     public TreeSet getLeft() {
  17.         return this.leftChild;
  18.     }
  19.    
  20.     public Type getValue() {
  21.         return this.value;
  22.     }
  23.    
  24.     public void addNewNode(TreeSet value) {
  25.         if(this.leftChild == null && this.rightChild == null) {
  26.             // am ajuns la frunza, adaugam valoarea
  27.             // like Potolea (<3)
  28.            
  29.             if(value.getValue().compareTo(this.getValue()) == -1) {
  30.                 // daca e mai mica
  31.                 this.leftChild = value;
  32.             }
  33.             else if(value.getValue().compareTo(this.getValue()) == 1) {
  34.                 // daca e mai mare
  35.                 this.rightChild = value;
  36.             }
  37.         }
  38.         else {
  39.             if(value.getValue().compareTo(this.getValue()) == -1) {
  40.                 // daca e mai mica
  41.                 this.getLeft().addNewNode(value);
  42.             }
  43.             else if(value.getValue().compareTo(this.getValue()) == 1) {
  44.                 // daca e mai mare
  45.                 this.getRight().addNewNode(value);
  46.             }
  47.         }
  48.     }
  49. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement