二叉树泛型 C#实现

来源:互联网 发布:美拍怎么加淘宝链接 编辑:程序博客网 时间:2024/04/28 21:29
/// <summary>    /// 二叉树类    /// </summary>    /// <typeparam name="ElementType"></typeparam>    class BinaryTree<ElementType> where ElementType: IComparable<ElementType>    {        public ElementType element;        public BinaryTree<ElementType> left;        public BinaryTree<ElementType> right;        /// <summary>        /// 清空树        /// </summary>        /// <param name="T"></param>        /// <returns></returns>        public BinaryTree<ElementType> MakeEmpty( BinaryTree<ElementType> T )        {            T.left = null;            T.right = null;            return T;        }        /// <summary>        /// 查找特定元素,返回BinaryTree类        /// </summary>        /// <param name="X"></param>        /// <param name="T"></param>        /// <returns></returns>        public BinaryTree<ElementType> Find (ElementType X, BinaryTree<ElementType> T)        {            if ( T == null )                return null;            if (X.CompareTo(T.element) < 0)                return Find(X, T.left);            else if (X.CompareTo(T.element) > 0)                return Find(X, T.right);            else return T;        }        /// <summary>        /// 查找最大元素        /// </summary>        /// <param name="T"></param>        /// <returns></returns>        public BinaryTree<ElementType> FindMax(BinaryTree<ElementType> T)        {            if (T == null)                return null;            else if (T.right == null)                return T;            else                return FindMax(T.right);        }        /// <summary>        /// 查找最小元素        /// </summary>        /// <param name="T"></param>        /// <returns></returns>        public BinaryTree<ElementType> FindMin(BinaryTree<ElementType> T)        {            if (T == null)                return null;            else if (T.left == null)                return T;            else                return FindMin(T.left);        }        /// <summary>        /// 插入元素        /// </summary>        /// <param name="X"></param>        /// <param name="T"></param>        /// <returns></returns>        public BinaryTree<ElementType> Insert(ElementType X, BinaryTree<ElementType> T)        {            if (T == null)            {                T = new BinaryTree<ElementType>();                if (T == null)                    return null;                T.element = X;            }            if (X.CompareTo(T.element) < 0)                T.left = Insert(X, T.left);            else if (X.CompareTo(T.element) > 0)                T.right = Insert(X, T.right);            return T;        }        /// <summary>        /// 删除特定元素        /// </summary>        /// <param name="X"></param>        /// <param name="T"></param>        /// <returns></returns>        public BinaryTree<ElementType> Delete(ElementType X, BinaryTree<ElementType> T)        {            BinaryTree<ElementType> Tmp;            if (T == null)            {                return null;            }            else if (X.CompareTo(T.element) < 0)                T.left = Delete(X, T.left);            else if (X.CompareTo(T.element) > 0)                T.right = Delete(X, T.right);            else if (T.left != null && T.right != null)            {                Tmp = FindMin(T.right);                T.element = Tmp.element;                T.right = Delete(Tmp.element, T.right);            }            else            {                Tmp = T;                if (T.left == null)                {                    T = T.right;                }                else                    T = T.left;            }            return T;        }        public int Max(int a, int b)        {            return a > b ? a : b;        }        public int Height(BinaryTree<ElementType> T)        {            if (T == null)                return -1;            return Max(Height(T.left), Height(T.right)) + 1;        }        /// <summary>        /// 打印单个结点的元素,根据树高        /// </summary>        /// <param name="T"></param>        public void PrintElement(BinaryTree<ElementType> T)        {            int height = Height(T);            for (int i = 0; i < height; i++)            {                Console.Write("\t");            }            Console.Write(T.element + "\n");        }        public void PrintTree(BinaryTree<ElementType> T)        {            if (T != null)            {                PrintTree(T.left);                PrintElement(T);                PrintTree(T.right);            }        }    }

0 0
原创粉丝点击