Advertisement
damarijsilva

ejercicio1-b

Oct 27th, 2017
132
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 6.62 KB | None | 0 0
  1. /*-------------------------------------------Clase BTNode-------------------------------------------------------*/
  2. public class BTNode<E>
  3.     {
  4.         private E item;
  5.  
  6.         public virtual E Item
  7.         {
  8.             get { return this.item; }
  9.             set { this.item = value; }
  10.         }
  11.  
  12.         private BTNode<E> left;
  13.  
  14.         public virtual BTNode<E> Left
  15.         {
  16.             get { return this.left; }
  17.             set { this.left = value; }
  18.         }
  19.  
  20.         private BTNode<E> right;
  21.  
  22.         public virtual BTNode<E> Right
  23.         {
  24.             get { return this.right; }
  25.             set { this.right = value; }
  26.         }
  27.  
  28.         public BTNode(E item = default(E), BTNode<E> left = null, BTNode<E> right = null)
  29.         {
  30.             this.Item = item;
  31.             this.Left = left;
  32.             this.Right = right;
  33.         }
  34.  
  35.  
  36.         public virtual void Visit()
  37.         {
  38.             Console.Write("{0} ", this.Item.ToString());
  39.         }
  40.     }
  41.  
  42.  
  43. /*-------------------------------------------Clase BinaryTree-------------------------------------------------------*/
  44.  public class BinaryTree<E>
  45.     {
  46.         private BTNode<E> root;
  47.  
  48.         protected virtual BTNode<E> Root
  49.         {
  50.             get { return this.root; }
  51.             set { this.root = value; }
  52.         }
  53.  
  54.         public virtual Boolean IsEmpty
  55.         {
  56.             get { return this.root == null; }
  57.         }
  58.  
  59.         public BinaryTree(BTNode<E> root = null)
  60.         {
  61.             this.root = root;
  62.         }
  63.  
  64.         public BinaryTree(E item = default(E))
  65.             : this(new BTNode<E>(item))
  66.         {
  67.         }
  68.  
  69.         public BinaryTree(E item, BinaryTree<E> leftTree = null, BinaryTree<E> rightTree = null)
  70.         {
  71.             this.root = new BTNode<E>(item);
  72.             if (leftTree != null)
  73.             {
  74.                 this.root.Left = leftTree.Root;
  75.             }
  76.             if(rightTree != null)
  77.             {
  78.                 this.Root.Right = rightTree.Root;
  79.             }
  80.         }
  81.        
  82.         public override string ToString()
  83.         {
  84.             return ToString(this.root);
  85.         }
  86.  
  87.  
  88.         protected string ToString(BTNode<E> root)
  89.         {
  90.             StringBuilder sb = new StringBuilder();            
  91.             if (root != null)
  92.             {                
  93.                 sb.Append(root.Item.ToString());
  94.                 if (root.Left != null)
  95.                 {                  
  96.                     sb.Append(" (" + ToString(root.Left));
  97.                     if (root.Right != null)
  98.                     {                        
  99.                         sb.Append(", " + ToString(root.Right));
  100.                     }                  
  101.                     sb.Append(")");
  102.                 }
  103.                 else
  104.                 {
  105.                     if (root.Right != null)
  106.                     {                        
  107.                         sb.Append(" (, " + ToString(root.Right) + ")");
  108.                     }
  109.                 }
  110.             }            
  111.             return sb.ToString();
  112.         }
  113.    
  114.          public virtual void PreOrder()
  115.         {
  116.             PreOrder(this.Root);
  117.         }
  118.  
  119.         protected virtual void PreOrder(BTNode<E> root)
  120.         {
  121.             if (root != null)
  122.             {
  123.                 root.Visit();
  124.                 PreOrder(root.Left);
  125.                 PreOrder(root.Right);
  126.             }
  127.         }
  128.  
  129.  
  130.         public virtual void InOrder()
  131.         {
  132.             InOrder(this.Root);
  133.         }
  134.  
  135.  
  136.         protected virtual void InOrder(BTNode<E> root)
  137.         {
  138.             if (root != null)
  139.             {
  140.                 InOrder(root.Left);
  141.                 root.Visit();
  142.                 InOrder(root.Right);
  143.             }
  144.         }
  145.  
  146.         public virtual void PostOrder()
  147.         {
  148.             PostOrder(this.Root);
  149.         }
  150.  
  151.         protected virtual void PostOrder(BTNode<E> root)
  152.         {
  153.             if (root != null)
  154.             {
  155.                 PostOrder(root.Left);
  156.                 PostOrder(root.Right);
  157.                 root.Visit();
  158.             }
  159.         }
  160.  
  161.          public virtual int LeafCount()
  162.         {
  163.             return LeafCount(this.Root);
  164.         }
  165.      
  166.         protected virtual int LeafCount(BTNode<E> root)
  167.         {
  168.             if (root != null)
  169.             {
  170.                 if ((root.Left == null) && (root.Right == null))
  171.                 {
  172.                     return 1;
  173.                 }
  174.                 else
  175.                 {
  176.                     return LeafCount(root.Left) + LeafCount(root.Right);
  177.                 }
  178.             }
  179.             return 0;
  180.         }
  181.  
  182. /*-------------------------------------------Clase Program-------------------------------------------------------*/
  183.  
  184. class Program
  185.     {
  186.         static void Main(string[] args)
  187.         {
  188.  
  189.             Console.WriteLine("Ejemplo 1:");
  190.             Console.WriteLine();            
  191.             BinaryTree<char> a1 = new BinaryTree<char>('B', new BinaryTree<char>('D'), new BinaryTree<char>('E'));
  192.             BinaryTree<char> a2 = new BinaryTree<char>('C', new BinaryTree<char>('F'), new BinaryTree<char>('G'));          
  193.  
  194.             BinaryTree<char> a = new BinaryTree<char>('A', a1,a2);
  195.  
  196.             Console.WriteLine("Arbol -> {0}", a.ToString());
  197.             Console.Write("PreOrden: ");        
  198.             a.PreOrder();
  199.  
  200.             Console.WriteLine("\nCantidad de hojas: {0} ", a.LeafCount());
  201.  
  202.             Console.WriteLine("\r\nEjemplo 2:");
  203.             Console.WriteLine();
  204.            
  205.             BinaryTree<char> b1 = new BinaryTree<char>('G', new BinaryTree<char>('E'), new BinaryTree<char>('H'));
  206.             BinaryTree<char> b2 = new BinaryTree<char>('L',null, new BinaryTree<char>('N'));            
  207.  
  208.             BinaryTree<char> b = new BinaryTree<char>('K', b1, b2);
  209.  
  210.             Console.WriteLine("Arbol -> {0}", b.ToString());
  211.             Console.Write("PreOrden: ");
  212.             b.PreOrder();
  213.  
  214.             Console.WriteLine("\nCantidad de hojas: {0} ", b.LeafCount());
  215.  
  216.             Console.WriteLine("\r\nEjemplo 3:");
  217.             Console.WriteLine();
  218.  
  219.             BinaryTree<int> c1 = new BinaryTree<int>(30, new BinaryTree<int>(4), new BinaryTree<int>(41));
  220.             BinaryTree<int> c2 = new BinaryTree<int>(75, new BinaryTree<int>(60), new BinaryTree<int>(85));
  221.  
  222.             BinaryTree<int> c = new BinaryTree<int>(55, c1, c2);
  223.  
  224.             Console.WriteLine("Arbol -> {0}", c.ToString());
  225.             Console.Write("PreOrden: ");
  226.             c.PreOrder();
  227.  
  228.             Console.WriteLine("\nCantidad de hojas: {0} ", c.LeafCount());
  229.             Console.ReadKey();
  230.  
  231.  
  232.         }
  233.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement