二叉树泛型 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
- 二叉树泛型 C#实现
- 排序二叉树C#实现
- C#实现二叉树遍历
- C# 二叉树的实现
- C#实现二叉查找树
- C#实现二叉查找树
- c#实现二叉树+二叉树遍历彻底理解
- 数据结构与算法(C#实现)---二叉树
- 数据结构与算法(C#实现)---二叉树
- 二叉树遍历算法实现(C#2.0)
- 二叉树遍历算法实现(C#2.0)
- C#中实现二叉树的遍历
- C#二叉树遍历算法实现浅析
- C#实现的泛型二叉树
- C# 实现的一个二叉树类
- c#使用数组实现二叉查找树
- 二叉树C#实现----MSDN链接
- C#实现二叉树遍历算法
- 再谈STM32的CAN过滤器-bxCAN的过滤器的4种工作模式以及使用方法总结
- 用户登录时启动
- 图片验证码
- 路由器WAN口和LAN口详解
- Xtrabackup用法
- 二叉树泛型 C#实现
- LeetCode OJ 160. Intersection of Two Linked Lists
- Mysql学习笔记
- Linux shell脚本实现kill进程
- saltstack 执行cp.get_file没有反应,salt-minion-The Salt Master has rejected this minion's
- 第二章 操作系统结构
- 6.scala中的包
- Apache Struts2(S2-046)漏洞利用
- Python爬虫 模拟登录