Advertisement
Jurado001

TP05ej4_BinarySearchTree

Nov 2nd, 2017
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.77 KB | None | 0 0
  1. public class BinarySearchTree<E> extends Product {
  2.     protected Node<Product> root;
  3.    
  4.     public Node<Product> getRoot() {
  5.         return root;
  6.     }
  7.  
  8.     public void setRoot(Node<Product> root) {
  9.         this.root = root;
  10.     }
  11.    
  12.     public boolean isEmpty() {
  13.         return this.root == null;
  14.     }
  15.    
  16.     public BinarySearchTree() {
  17.         this.root = null;
  18.     }
  19.    
  20.     public BinarySearchTree(Product item) {
  21.         this.root = new Node<Product>(item);
  22.     }
  23.    
  24.     public BinarySearchTree(Product item, BinarySearchTree<E> leftTree, BinarySearchTree<E> rightTree) {
  25.         this.root = new Node<Product>(item);
  26.         if(leftTree != null) {
  27.             this.root.Left = leftTree.root;
  28.         }
  29.         if(rightTree != null) {
  30.             this.root.Right = rightTree.root;
  31.         }
  32.     }
  33.  
  34.    
  35.     public void InOrden() {
  36.         InOrden(this.root);
  37.     }
  38.    
  39.     protected void InOrden(Node<Product> root) {
  40.         if(root != null) {
  41.             InOrden(root.Left);
  42.             System.out.println(root.toString());
  43.             InOrden(root.Right);
  44.         }
  45.     }
  46.    
  47.     public boolean Contains(int codigo) {
  48.         return Contains(this.root, codigo);        
  49.     }
  50.    
  51.     private boolean Contains(Node<Product> root, Integer codigo) {
  52.         if(root != null) {
  53.             Integer codAux = root.item.getCodigo();
  54.             if(codigo.compareTo(codAux) == 0) {
  55.                 return true;
  56.             }
  57.             else {
  58.                 if(codigo.compareTo(codAux) < 0) {
  59.                     return Contains(root.Left, codigo);
  60.                 }
  61.                 else {
  62.                     return Contains(root.Left, codigo);
  63.                 }
  64.             }
  65.         }
  66.         return false;
  67.     }
  68.    
  69.     public void Add(Product item) {
  70.         if(this.isEmpty()) {
  71.             this.setRoot(new Node<Product>(item));
  72.         }
  73.         else {
  74.             Integer cod1 = item.getCodigo();
  75.             Node<Product> temp = this.root, prev = null;
  76.             while(temp != null) {
  77.                 prev = temp;
  78.                 Integer cod2 = temp.item.getCodigo();
  79.                 if (cod1.compareTo(cod2)<0) {
  80.                     temp = temp.Left;
  81.                 }
  82.                 else {
  83.                     temp = temp.Right;
  84.                 }
  85.             }
  86.             temp = new Node<Product>(item);
  87.             Integer cod3 = prev.item.getCodigo();
  88.             if (cod1.compareTo(cod3)<0) {
  89.                 prev.Left = temp;
  90.             }else {
  91.                 prev.Right = temp;
  92.             }
  93.         }
  94.     }
  95.    
  96.  
  97.     public class Node<E> {
  98.         public E item;
  99.         public Node<E> Left;
  100.         public Node<E> Right;
  101.        
  102.         public E getItem() {
  103.             return item;
  104.         }
  105.         public void setItem(E item) {
  106.             this.item = item;
  107.         }
  108.        
  109.         public Node() {
  110.             this.item = null;
  111.             this.Left = this.Right = null;
  112.         }
  113.        
  114.         public Node(E item) {
  115.             this.item = item;
  116.             this.Left = this.Right = null;
  117.         }
  118.        
  119.         public Node(E item, Node Left, Node Right) {
  120.             this.item = item;
  121.             this.Left = Left;
  122.             this.Right = Right;
  123.         }
  124.        
  125.         public String toString() {
  126.             return this.item.toString();
  127.         }
  128.     }
  129. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement