Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*-------------------------------------------Clase BTNode-------------------------------------------------------*/
- public class BTNode<E>
- {
- private E item;
- public virtual E Item
- {
- get { return this.item; }
- set { this.item = value; }
- }
- private BTNode<E> left;
- public virtual BTNode<E> Left
- {
- get { return this.left; }
- set { this.left = value; }
- }
- private BTNode<E> right;
- public virtual BTNode<E> Right
- {
- get { return this.right; }
- set { this.right = value; }
- }
- public BTNode(E item = default(E), BTNode<E> left = null, BTNode<E> right = null)
- {
- this.Item = item;
- this.Left = left;
- this.Right = right;
- }
- public virtual void Visit()
- {
- Console.Write("{0} ", this.Item.ToString());
- }
- }
- /*-------------------------------------------Clase BinaryTree-------------------------------------------------------*/
- public class BinaryTree<E>
- {
- private BTNode<E> root;
- protected virtual BTNode<E> Root
- {
- get { return this.root; }
- set { this.root = value; }
- }
- public virtual Boolean IsEmpty
- {
- get { return this.root == null; }
- }
- public BinaryTree(BTNode<E> root = null)
- {
- this.root = root;
- }
- public BinaryTree(E item = default(E))
- : this(new BTNode<E>(item))
- {
- }
- public BinaryTree(E item, BinaryTree<E> leftTree = null, BinaryTree<E> rightTree = null)
- {
- this.root = new BTNode<E>(item);
- if (leftTree != null)
- {
- this.root.Left = leftTree.Root;
- }
- if(rightTree != null)
- {
- this.Root.Right = rightTree.Root;
- }
- }
- public override string ToString()
- {
- return ToString(this.root);
- }
- protected string ToString(BTNode<E> root)
- {
- StringBuilder sb = new StringBuilder();
- if (root != null)
- {
- sb.Append(root.Item.ToString());
- if (root.Left != null)
- {
- sb.Append(" (" + ToString(root.Left));
- if (root.Right != null)
- {
- sb.Append(", " + ToString(root.Right));
- }
- sb.Append(")");
- }
- else
- {
- if (root.Right != null)
- {
- sb.Append(" (, " + ToString(root.Right) + ")");
- }
- }
- }
- return sb.ToString();
- }
- public virtual void PreOrder()
- {
- PreOrder(this.Root);
- }
- protected virtual void PreOrder(BTNode<E> root)
- {
- if (root != null)
- {
- root.Visit();
- PreOrder(root.Left);
- PreOrder(root.Right);
- }
- }
- public virtual void InOrder()
- {
- InOrder(this.Root);
- }
- protected virtual void InOrder(BTNode<E> root)
- {
- if (root != null)
- {
- InOrder(root.Left);
- root.Visit();
- InOrder(root.Right);
- }
- }
- public virtual void PostOrder()
- {
- PostOrder(this.Root);
- }
- protected virtual void PostOrder(BTNode<E> root)
- {
- if (root != null)
- {
- PostOrder(root.Left);
- PostOrder(root.Right);
- root.Visit();
- }
- }
- public virtual int LeafCount()
- {
- return LeafCount(this.Root);
- }
- protected virtual int LeafCount(BTNode<E> root)
- {
- if (root != null)
- {
- if ((root.Left == null) && (root.Right == null))
- {
- return 1;
- }
- else
- {
- return LeafCount(root.Left) + LeafCount(root.Right);
- }
- }
- return 0;
- }
- /*-------------------------------------------Clase Program-------------------------------------------------------*/
- class Program
- {
- static void Main(string[] args)
- {
- Console.WriteLine("Ejemplo 1:");
- Console.WriteLine();
- BinaryTree<char> a1 = new BinaryTree<char>('B', new BinaryTree<char>('D'), new BinaryTree<char>('E'));
- BinaryTree<char> a2 = new BinaryTree<char>('C', new BinaryTree<char>('F'), new BinaryTree<char>('G'));
- BinaryTree<char> a = new BinaryTree<char>('A', a1,a2);
- Console.WriteLine("Arbol -> {0}", a.ToString());
- Console.Write("PreOrden: ");
- a.PreOrder();
- Console.WriteLine("\nCantidad de hojas: {0} ", a.LeafCount());
- Console.WriteLine("\r\nEjemplo 2:");
- Console.WriteLine();
- BinaryTree<char> b1 = new BinaryTree<char>('G', new BinaryTree<char>('E'), new BinaryTree<char>('H'));
- BinaryTree<char> b2 = new BinaryTree<char>('L',null, new BinaryTree<char>('N'));
- BinaryTree<char> b = new BinaryTree<char>('K', b1, b2);
- Console.WriteLine("Arbol -> {0}", b.ToString());
- Console.Write("PreOrden: ");
- b.PreOrder();
- Console.WriteLine("\nCantidad de hojas: {0} ", b.LeafCount());
- Console.WriteLine("\r\nEjemplo 3:");
- Console.WriteLine();
- BinaryTree<int> c1 = new BinaryTree<int>(30, new BinaryTree<int>(4), new BinaryTree<int>(41));
- BinaryTree<int> c2 = new BinaryTree<int>(75, new BinaryTree<int>(60), new BinaryTree<int>(85));
- BinaryTree<int> c = new BinaryTree<int>(55, c1, c2);
- Console.WriteLine("Arbol -> {0}", c.ToString());
- Console.Write("PreOrden: ");
- c.PreOrder();
- Console.WriteLine("\nCantidad de hojas: {0} ", c.LeafCount());
- Console.ReadKey();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement