Advertisement
Guest User

ejercicio 1e

a guest
Oct 19th, 2017
127
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 7.42 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5.  
  6. namespace Arbol
  7. {
  8.     class BTNode<E>
  9.     {
  10.         private E item;
  11.         public virtual E Item
  12.         {
  13.             get { return this.item; }
  14.             set { this.item = value; }
  15.         }
  16.         private BTNode<E> left;
  17.         public virtual BTNode<E> Left
  18.         {
  19.             get { return this.left; }
  20.             set { this.left = value; }
  21.            
  22.         }
  23.         private BTNode<E> right;
  24.         public virtual BTNode<E> Right
  25.         {
  26.             get { return this.right; }
  27.             set { this.right = value; }
  28.  
  29.         }
  30.         public BTNode(E item = default(E), BTNode<E> left = null, BTNode<E> right = null)
  31.         {
  32.             this.Item = item;
  33.             this.Left = left;
  34.             this.Right = right;
  35.         }
  36.         public virtual void Visit()
  37.         {
  38.             Console.Write("{0} \n", this.Item.ToString());
  39.         }
  40.        
  41.     }
  42. }
  43. using System;
  44. using System.Collections.Generic;
  45. using System.Linq;
  46. using System.Text;
  47.  
  48. namespace Arbol
  49. {
  50.     class BinaryTree<E>
  51.     {
  52.         private BTNode<E> root;
  53.         protected virtual BTNode<E> Root
  54.         {
  55.             get { return this.root; }
  56.             set { this.root = value; }
  57.         }
  58.         public virtual Boolean IsEmpty
  59.         {
  60.             get { return this.Root == null; }
  61.         }
  62.         public BinaryTree(BTNode<E> root = null)
  63.         {
  64.             this.Root = root;
  65.         }
  66.         public BinaryTree(E item, BinaryTree<E> leftTree = null, BinaryTree<E> rightTree = null)
  67.         {
  68.             this.Root = new BTNode<E>(item);
  69.             if (leftTree != null)
  70.             {
  71.                 this.Root.Left = leftTree.Root;
  72.             }
  73.             if (rightTree != null)
  74.             {
  75.                 this.Root.Right = rightTree.Root;
  76.             }
  77.         }
  78.         public BinaryTree<E> GetLeftSubTree()
  79.         {
  80.             if (IsEmpty)
  81.             {
  82.                 throw new Exception("Arbol vacio");
  83.             }
  84.             return new BinaryTree<E>(Root.Left);
  85.         }
  86.         public BinaryTree<E> GetRightSubTree()
  87.         {
  88.             if (IsEmpty)
  89.             {
  90.                 throw new Exception("Arbol vacio");
  91.             }
  92.             return new BinaryTree<E>(Root.Right);
  93.         }
  94.         public override string ToString()
  95.         {
  96.             return ToString(this.Root);
  97.         }
  98.         protected string ToString(BTNode<E> root)
  99.         {
  100.             StringBuilder sb = new StringBuilder();
  101.             if (root != null)
  102.             {
  103.                 sb.Append(root.Item.ToString());
  104.                 if (root.Left != null)
  105.                 {
  106.                     sb.Append( ","+ToString(root.Left));
  107.                     if (root.Right != null)
  108.                     {
  109.                         sb.Append("," + ToString(root.Right));
  110.                     }
  111.                     sb.Append(" ");
  112.                 }
  113.                 else
  114.                 {
  115.                     if (root.Right != null)
  116.                     {
  117.                         sb.Append("" + ToString(root.Right)+")");
  118.                     }
  119.                 }
  120.             }
  121.             return sb.ToString();
  122.         }
  123.         public BinaryTree(E item = default(E)) : this(new BTNode<E>(item))
  124.         {
  125.         }
  126.         public virtual void PreOrder()
  127.         {
  128.             PreOrder(this.Root);
  129.         }
  130.         protected virtual void PreOrder(BTNode<E> root)
  131.         {
  132.             if (root != null)
  133.             {
  134.                 root.Visit();
  135.                 PreOrder(root.Left);
  136.                 PreOrder(root.Right);
  137.             }
  138.         }
  139.         public virtual void InOrder()
  140.         {
  141.             InOrder(this.Root);
  142.         }
  143.         protected virtual void InOrder(BTNode<E> root)
  144.         {
  145.             if (root != null)
  146.             {
  147.                
  148.                 InOrder(root.Left);
  149.                 root.Visit();
  150.                 InOrder(root.Right);
  151.             }
  152.         }
  153.         public virtual void PostOrder()
  154.         {
  155.             PostOrder(this.Root);
  156.         }
  157.         protected virtual void PostOrder(BTNode<E> root)
  158.         {
  159.             if (root != null)
  160.             {
  161.  
  162.                 PostOrder(root.Left);
  163.                 PostOrder(root.Right);
  164.                 root.Visit();
  165.             }
  166.         }
  167.         public virtual void InDescendingOrder()
  168.         {
  169.             InDescendingOrder(this.Root);
  170.         }
  171.         protected virtual void InDescendingOrder(BTNode<E> root)
  172.         {
  173.             if (root != null)
  174.             {
  175.                 InDescendingOrder(root.Right);
  176.                 root.Visit();
  177.                 InDescendingOrder(root.Left);
  178.             }
  179.         }
  180.         public virtual int NodeCount()
  181.         {
  182.             return NodeCount(this.Root);
  183.         }
  184.         protected virtual int NodeCount(BTNode<E> root)
  185.         {
  186.             if (root != null)
  187.             {
  188.                 return 1 + NodeCount(root.Left) + NodeCount(root.Right);
  189.             }
  190.             return 0;
  191.         }
  192.         public virtual int LeafCount()
  193.         {
  194.             return LeafCount(this.Root);
  195.         }
  196.         protected virtual int LeafCount(BTNode<E> root)
  197.         {
  198.             if (root != null)
  199.             {
  200.                 if ((root.Left == null) && (root.Right == null))
  201.                 {
  202.                     return 1;
  203.                 }
  204.                 else
  205.                 {
  206.                     return LeafCount(root.Left) + LeafCount(root.Right);
  207.                 }
  208.             }
  209.             return 0;
  210.         }
  211.         public virtual int InternalNodeCount()
  212.         {
  213.             return InternalNodeCount(this.Root);
  214.         }
  215.         protected virtual int InternalNodeCount(BTNode<E> root)
  216.         {
  217.             if (root != null)
  218.             {
  219.                 if ((root.Left == null) && (root.Right == null))
  220.                 {
  221.                     return 0;
  222.                 }
  223.                 else
  224.                 {
  225.                     return 1 + LeafCount(root.Left) + LeafCount(root.Right);
  226.                 }
  227.             }
  228.             return 0;
  229.         }
  230.         public virtual int MaxLevel()
  231.         {
  232.             if (!IsEmpty)
  233.             {
  234.                 return MaxLevel(this.Root);
  235.             }
  236.             return -1;
  237.         }
  238.         protected virtual int MaxLevel(BTNode<E> root)
  239.         {
  240.             if (root != null)
  241.             {
  242.                 if ((root.Left == null) && (root.Right == null))
  243.                 {
  244.                     int leftLevel = MaxLevel(root.Left);
  245.                     int rightLevel = MaxLevel(root.Right);
  246.                     return 1 + Math.Max(leftLevel, rightLevel);
  247.                 }
  248.             }
  249.             return 0;
  250.         }
  251.         public virtual int Height()
  252.         {
  253.             return MaxLevel() + 1;
  254.         }
  255.        
  256.     }
  257. }using System;
  258. using System.Collections.Generic;
  259. using System.Linq;
  260. using System.Text;
  261.  
  262. namespace Arbol
  263. {
  264.     class Program
  265.     {
  266.         static void Main(string[] args)
  267.         {
  268.             Random num = new Random();
  269.             BinaryTree<int> a = new BinaryTree<int>(15, new BinaryTree<int>(1), new BinaryTree<int>(20));
  270.             Console.WriteLine("Arbol:{0}", a.ToString());
  271.             Console.WriteLine("En orden descendiente: ");
  272.             a.InDescendingOrder();
  273.             Console.ReadKey();
  274.  
  275.         }
  276.     }
  277. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement