Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- namespace Arbol
- {
- 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} \n", this.Item.ToString());
- }
- }
- }
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- namespace Arbol
- {
- 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, 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 BinaryTree<E> GetLeftSubTree()
- {
- if (IsEmpty)
- {
- throw new Exception("Arbol vacio");
- }
- return new BinaryTree<E>(Root.Left);
- }
- public BinaryTree<E> GetRightSubTree()
- {
- if (IsEmpty)
- {
- throw new Exception("Arbol vacio");
- }
- return new BinaryTree<E>(Root.Right);
- }
- 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 BinaryTree(E item = default(E)) : this(new BTNode<E>(item))
- {
- }
- 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 void InDescendingOrder()
- {
- InDescendingOrder(this.Root);
- }
- protected virtual void InDescendingOrder(BTNode<E> root)
- {
- if (root != null)
- {
- InDescendingOrder(root.Right);
- root.Visit();
- InDescendingOrder(root.Left);
- }
- }
- public virtual int NodeCount()
- {
- return NodeCount(this.Root);
- }
- protected virtual int NodeCount(BTNode<E> root)
- {
- if (root != null)
- {
- return 1 + NodeCount(root.Left) + NodeCount(root.Right);
- }
- return 0;
- }
- 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;
- }
- public virtual int InternalNodeCount()
- {
- return InternalNodeCount(this.Root);
- }
- protected virtual int InternalNodeCount(BTNode<E> root)
- {
- if (root != null)
- {
- if ((root.Left == null) && (root.Right == null))
- {
- return 0;
- }
- else
- {
- return 1 + LeafCount(root.Left) + LeafCount(root.Right);
- }
- }
- return 0;
- }
- public virtual int MaxLevel()
- {
- if (!IsEmpty)
- {
- return MaxLevel(this.Root);
- }
- return -1;
- }
- protected virtual int MaxLevel(BTNode<E> root)
- {
- if (root != null)
- {
- if ((root.Left == null) && (root.Right == null))
- {
- int leftLevel = MaxLevel(root.Left);
- int rightLevel = MaxLevel(root.Right);
- return 1 + Math.Max(leftLevel, rightLevel);
- }
- }
- return 0;
- }
- public virtual int Height()
- {
- return MaxLevel() + 1;
- }
- }
- }using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- namespace Arbol
- {
- class Program
- {
- static void Main(string[] args)
- {
- Random num = new Random();
- BinaryTree<int> a = new BinaryTree<int>(15, new BinaryTree<int>(1), new BinaryTree<int>(20));
- Console.WriteLine("Arbol:{0}", a.ToString());
- Console.WriteLine("En orden descendiente: ");
- a.InDescendingOrder();
- Console.ReadKey();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement