Advertisement
andresnogales

BinaryTree.java

Nov 10th, 2021
51
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.99 KB | None | 0 0
  1.  
  2. public class BinaryTree<ELEMENT> {
  3.  
  4.     protected class BTNode<ELEMENT> {
  5.  
  6.         public ELEMENT item;
  7.         public BTNode<ELEMENT> left;
  8.         public BTNode<ELEMENT> right;
  9.  
  10.         public BTNode() {
  11.             this(null, null, null);
  12.         }
  13.         public BTNode(ELEMENT item) {
  14.             this(item, null, null);
  15.         }
  16.         public BTNode(ELEMENT item, BTNode<ELEMENT> left, BTNode<ELEMENT> right) {
  17.             this.item = item;
  18.             this.left = left;
  19.             this.right = right;
  20.         }
  21.  
  22.         @Override
  23.         public String toString() {
  24.             return this.item.toString();
  25.         }
  26.  
  27.         public void Visit() {
  28.             System.out.printf(this.item.toString() + "\n");
  29.         }
  30.        
  31.     }
  32.  
  33.  
  34.     protected BTNode<ELEMENT> root;
  35.  
  36.     //Constructores
  37.    
  38.     public BinaryTree() {
  39.         this.root = null;
  40.     }
  41.    
  42.     public BinaryTree(ELEMENT item) {
  43.         this(item, null, null);
  44.     }
  45.    
  46.     public BinaryTree(ELEMENT item, BinaryTree<ELEMENT> left, BinaryTree<ELEMENT> right) {
  47.         this.root = new BTNode<ELEMENT>(item, null, null);
  48.         if (left != null) {
  49.             this.root.left = left.root;
  50.         }
  51.         if (right != null) {
  52.             this.root.right = right.root;
  53.         }
  54.     }
  55.  
  56.     @Override
  57.     public String toString() {
  58.         StringBuilder sb = new StringBuilder();
  59.         toString(sb, this.root);
  60.         return sb.toString();
  61.     }
  62.    
  63.     protected void toString(StringBuilder sb, BTNode<ELEMENT> root) {
  64.         if (root != null) {
  65.             sb.append(root.item.toString());
  66.             if (root.left != null) {
  67.                 sb.append("(");
  68.                 toString(sb, root.left);
  69.                 if (root.right != null) {
  70.                     sb.append(",");
  71.                     toString(sb, root.right);
  72.                 }
  73.                 sb.append(")");
  74.             } else {
  75.                 if (root.right != null) {
  76.                     sb.append("(,");
  77.                     toString(sb, root.right);
  78.                     sb.append(")");
  79.                 }
  80.             }
  81.         }
  82.     }
  83.  
  84.  
  85.     public void PreOrder() {
  86.         PreOrder(this.root);
  87.     }
  88.    
  89.     protected void PreOrder(BTNode<ELEMENT> root) {
  90.         if (root != null) {
  91.             root.Visit();
  92.             PreOrder(root.left);
  93.             PreOrder(root.right);
  94.         }
  95.     }
  96.  
  97.     public void InOrder() {
  98.         InOrder(this.root);
  99.     }
  100.    
  101.     protected void InOrder(BTNode<ELEMENT> root) {
  102.         if (root != null) {
  103.             InOrder(root.left);
  104.             root.Visit();
  105.             InOrder(root.right);
  106.         }
  107.     }
  108.  
  109.     public void PostOrder() {
  110.         PostOrder(this.root);
  111.     }
  112.    
  113.     protected void PostOrder(BTNode<ELEMENT> root) {
  114.         if (root != null) {
  115.             PostOrder(root.left);
  116.             PostOrder(root.right);
  117.             root.Visit();
  118.         }
  119.     }
  120.  
  121.     public void DescendingOrder() {
  122.         DescendingOrder(this.root);
  123.     }
  124.    
  125.     protected void DescendingOrder(BTNode<ELEMENT> root) {
  126.         if (root != null) {
  127.             DescendingOrder(root.right);
  128.             root.Visit();
  129.             DescendingOrder(root.left);
  130.         }
  131.     }
  132.  
  133.     public int NodeCount() {
  134.         return NodeCount(this.root);
  135.     }
  136.    
  137.     protected int NodeCount(BTNode<ELEMENT> root) {
  138.         if (root != null) {
  139.             return 1 + NodeCount(root.left) + NodeCount(root.right);
  140.         }
  141.         return 0;
  142.     }
  143.  
  144.  
  145.     public int LeafCount() {
  146.         return LeafCount(this.root);
  147.     }
  148.    
  149.     protected int LeafCount(BTNode<ELEMENT> root) {
  150.         if (root != null) {
  151.             if ( (root.left == null) && (root.right == null) ) {
  152.                 return 1;
  153.             } else {
  154.                 return LeafCount(root.left) + LeafCount(root.right);
  155.             }
  156.         }
  157.         return 0;
  158.     }
  159.  
  160.  
  161.     public int InternalCount() {
  162.         return InternalCount(this.root);
  163.     }
  164.    
  165.     protected int InternalCount(BTNode<ELEMENT> root) {
  166.         if (root != null) {
  167.             if ( (root.left == null) && (root.right == null) ) {
  168.                 return 0;
  169.             } else {
  170.                 return 1 + InternalCount(root.left) + InternalCount(root.right);
  171.             }
  172.         }
  173.         return 0;
  174.     }
  175.  
  176.  
  177.     public int MaxLevel() {
  178.         return MaxLevel(this.root);
  179.     }
  180.    
  181.     protected int MaxLevel(BTNode<ELEMENT> root) {
  182.         if (root != null) {
  183.             if ( (root.left != null) || (root.right != null) ) {
  184.                 int leftLevel = MaxLevel(root.left);
  185.                 int rightLevel = MaxLevel(root.right);
  186.                 return 1 + Math.max(leftLevel, rightLevel);
  187.             }
  188.             return 0;
  189.         }
  190.         return -1;
  191.     }
  192.  
  193.     public int Height() {
  194.         return MaxLevel() + 1;
  195.     }
  196.  
  197.     public BTNode<ELEMENT> getRoot() {
  198.         return root;
  199.     }
  200.  
  201.     public void setRoot(BTNode<ELEMENT> root) {
  202.         this.root = root;
  203.     }
  204.    
  205.    
  206. }
  207.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement