C#数据结构-二叉树-链式存储结构
对比上一篇文章“顺序存储二叉树”,链式存储二叉树的优点是节省空间。
二叉树的性质:
1、在二叉树的第i层上至多有2i-1个节点(i>=1)。
2、深度为k的二叉树至多有2k-1个节点(k>=1)。
3、对任何一棵二叉树T,如果其终结点数为n0,度为2的节点数为n2,则n0=n2+1。
4、具有n个节点的完全二叉树的深度为log2n+1。
5、对于一棵有n个节点的完全二叉树的节点按层序编号,若完全二叉树中的某节点编号为i,则若有左孩子编号为2i,若有右孩子编号为2i+1,母亲节点为i/2。
在此记录下链式二叉树的实现方式 :
/// <summary>
/// 树节点
/// </summary>
/// <typeparam name="T"></typeparam>
public class TreeNode<T>
{
/// <summary>
/// 节点数据
/// </summary>
public T data { get; set; }
/// <summary>
/// 左节点
/// </summary>
public TreeNode<T> leftChild { get; set; }
/// <summary>
/// 右节点
/// </summary>
public TreeNode<T> rightChild { get; set; } public TreeNode()
{
data = default(T);
leftChild = null;
rightChild = null;
} public TreeNode(T item)
{
data = item;
leftChild = null;
rightChild = null;
}
}
/// <summary>
/// 二叉树 链表存储结构
/// </summary>
/// <typeparam name="T"></typeparam>
public class LinkStorageBinaryTree<T>
{
/// <summary>
/// 树根节
/// </summary>
private TreeNode<T> head { get; set; } public LinkStorageBinaryTree()
{
head = null;
} public LinkStorageBinaryTree(T val)
{
head = new TreeNode<T>(val);
}
/// <summary>
/// 获取左节点
/// </summary>
/// <param name="treeNode"></param>
/// <returns></returns>
public TreeNode<T> GetLeftNode(TreeNode<T> treeNode)
{
if (treeNode == null)
return null;
return treeNode.leftChild;
}
/// <summary>
/// 获取右节点
/// </summary>
/// <param name="treeNode"></param>
/// <returns></returns>
public TreeNode<T> GetRightNode(TreeNode<T> treeNode)
{
if (treeNode == null)
return null;
return treeNode.rightChild;
}
/// <summary>
/// 获取根节点
/// </summary>
/// <returns></returns>
public TreeNode<T> GetRoot()
{
return head;
}
/// <summary>
/// 插入左节点
/// </summary>
/// <param name="val"></param>
/// <param name="node"></param>
/// <returns></returns>
public TreeNode<T> AddLeftNode(T val,TreeNode<T> node)
{
if (node == null)
throw new ArgumentNullException("参数错误");
TreeNode<T> treeNode = new TreeNode<T>(val);
TreeNode<T> childNode = node.leftChild;
treeNode.leftChild = childNode;
node.leftChild = treeNode;
return treeNode;
} /// <summary>
/// 插入右节点
/// </summary>
/// <param name="val"></param>
/// <param name="node"></param>
/// <returns></returns>
public TreeNode<T> AddRightNode(T val, TreeNode<T> node)
{
if (node == null)
throw new ArgumentNullException("参数错误");
TreeNode<T> treeNode = new TreeNode<T>(val);
TreeNode<T> childNode = node.rightChild;
treeNode.rightChild = childNode;
node.rightChild = treeNode;
return treeNode;
}
/// <summary>
/// 删除当前节点的 左节点
/// </summary>
/// <param name="node"></param>
/// <returns></returns>
public TreeNode<T> DeleteLeftNode(TreeNode<T> node)
{
if (node == null || node.leftChild == null)
throw new ArgumentNullException("参数错误");
TreeNode<T> leftChild = node.leftChild;
node.leftChild = null;
return leftChild;
} /// <summary>
/// 删除当前节点的 右节点
/// </summary>
/// <param name="node"></param>
/// <returns></returns>
public TreeNode<T> DeleteRightNode(TreeNode<T> node)
{
if (node == null || node.leftChild == null)
throw new ArgumentNullException("参数错误");
TreeNode<T> rightChild = node.rightChild;
node.rightChild = null;
return rightChild;
} /// <summary>
/// 先序遍历
/// </summary>
/// <param name="index"></param>
public void PreorderTraversal(TreeNode<T> node)
{
//递归的终止条件
if (head == null)
{
Console.WriteLine("当前树为空");
return;
}
if (node != null)
{
Console.Write(node.data+ " ");
PreorderTraversal(node.leftChild);
PreorderTraversal(node.rightChild);
}
} /// <summary>
/// 中序遍历
/// </summary>
/// <param name="index"></param>
public void MiddlePrefaceTraversal(TreeNode<T> node)
{
//递归的终止条件
if (head == null)
{
Console.WriteLine("当前树为空");
return;
}
if (node != null)
{
MiddlePrefaceTraversal(node.leftChild); Console.Write(node.data + " "); MiddlePrefaceTraversal(node.rightChild);
}
} /// <summary>
/// 后序遍历
/// </summary>
/// <param name="index"></param>
public void AfterwordTraversal(TreeNode<T> node)
{
//递归的终止条件
if (head == null)
{
Console.WriteLine("当前树为空");
return;
}
if (node != null)
{
AfterwordTraversal(node.leftChild);
AfterwordTraversal(node.rightChild);
Console.Write(node.data + " ");
}
} public void LevelTraversal()
{
if (head == null)
return;
//使用队列先入先出
Queue<TreeNode<T>> queue = new Queue<TreeNode<T>>();
queue.Enqueue(head); while (queue.Any())
{
TreeNode<T> item = queue.Dequeue();
Console.Write(item.data +" ");
if (item.leftChild != null)
queue.Enqueue(item.leftChild);
if (item.rightChild != null)
queue.Enqueue(item.rightChild);
}
}
/// <summary>
/// 校验节点是否是叶子节点
/// </summary>
/// <param name="node"></param>
/// <returns></returns>
public bool ValidLeafNode(TreeNode<T> node)
{
if (node == null)
throw new ArgumentNullException("参数错误");
if (node.leftChild != null && node.rightChild != null)
{
Console.WriteLine($"节点 {node.data} 不是叶子节点");
return false;
}
Console.WriteLine($"节点 {node.data} 是叶子节点");
return true;
}
}
遍历方式在顺序存储一文中已经用图表示过,在此不做重复说明。
现在测试下:
LinkStorageBinaryTree<string> linkStorageBinary = new LinkStorageBinaryTree<string>("A");
TreeNode<string> tree1 = linkStorageBinary.AddLeftNode("B", linkStorageBinary.GetRoot());
TreeNode<string> tree2 = linkStorageBinary.AddRightNode("C", linkStorageBinary.GetRoot());
TreeNode<string> tree3 =linkStorageBinary.AddLeftNode("D", tree1);
linkStorageBinary.AddRightNode("E",tree1);
linkStorageBinary.AddLeftNode("F", tree2);
linkStorageBinary.AddRightNode("G", tree2);
//先序遍历
Console.Write("先序遍历:");
linkStorageBinary.PreorderTraversal(linkStorageBinary.GetRoot());
Console.WriteLine();
//中序遍历
Console.Write("中序遍历:");
linkStorageBinary.MiddlePrefaceTraversal(linkStorageBinary.GetRoot());
Console.WriteLine();
//中序遍历
Console.Write("后序遍历:");
linkStorageBinary.AfterwordTraversal(linkStorageBinary.GetRoot());
Console.WriteLine();
//层次遍历
Console.Write("层次遍历:");
linkStorageBinary.LevelTraversal();
linkStorageBinary.ValidLeafNode(tree1);
linkStorageBinary.ValidLeafNode(tree3);
Console.ReadKey();
输出:
先序遍历:A B D E C F G
中序遍历:D B E A F C G
后序遍历:D E B F G C A
层次遍历:A B C D E F G 节点 B 不是叶子节点
节点 D 是叶子节点

C#数据结构-二叉树-链式存储结构的更多相关文章
- javascript实现数据结构:线性表--线性链表(链式存储结构)
上一节中, 线性表的顺序存储结构的特点是逻辑关系上相邻的两个元素在物理位置上也相邻,因此可以随机存取表中任一元素,它的存储位置可用一个简单,直观的公式来表示.然后,另一方面来看,这个特点也造成这种存储 ...
- [置顶] ※数据结构※→☆线性表结构(queue)☆============优先队列 链式存储结构(queue priority list)(十二)
优先队列(priority queue) 普通的队列是一种先进先出的数据结构,元素在队列尾追加,而从队列头删除.在优先队列中,元素被赋予优先级.当访问元素时,具有最高优先级的元素最先删除.优先队列具有 ...
- c数据结构 -- 线性表之 复杂的链式存储结构
复杂的链式存储结构 循环链表 定义:是一种头尾相接的链表(即表中最后一个结点的指针域指向头结点,整个链表形成一个环) 优点:从表中任一节点出发均可找到表中其他结点 注意:涉及遍历操作时,终止条件是判断 ...
- c数据结构 -- 线性表之 顺序存储结构 于 链式存储结构 (单链表)
线性表 定义:线性表是具有相同特性的数据元素的一个有限序列 类型: 1:顺序存储结构 定义:把逻辑上相邻的数据元素存储在物理上相邻的存储单元中的存储结构 算法: #include <stdio. ...
- C++编程练习(6)----“实现简单的队列的链式存储结构“
队列的链式存储结构,其实就是线性表的单链表,只不过它只能尾进头出.简称链队列. 实现代码如下: /* LinkQueue.h 头文件 */ #include<iostream> #defi ...
- C++编程练习(4)----“实现简单的栈的链式存储结构“
如果栈的使用过程中元素数目变化不可预测,有时很小,有时很大,则最好使用链栈:反之,如果它的变化在可控范围内,使用顺序栈会好一些. 简单的栈的链式存储结构代码如下: /*LinkStack.h*/ #i ...
- C++编程练习(2)----“实现简单的线性表的链式存储结构“
单链表采用链式存储结构,用一组任意的存储单元存放线性表的元素. 对于查找操作,单链表的时间复杂度为O(n). 对于插入和删除操作,单链表在确定位置后,插入和删除时间仅为O(1). 单链表不需要分配存储 ...
- java资料——顺序存储结构和链式存储结构(转)
顺序存储结构 主要优点 节省存储空间,随机存取表中元素 缺 点 插入和删除操作需要移动元素 在计算机中用一组地址连续的存储单元依次存储线性表的各个数据元素,称作线性表的顺序存储结构. 顺序存储结 ...
- C++线性表的链式存储结构
C++实现线性表的链式存储结构: 为了解决顺序存储不足:用线性表另外一种结构-链式存储.在顺序存储结构(数组描述)中,元素的地址是由数学公式决定的,而在链式储存结构中,元素的地址是随机分布的,每个元素 ...
随机推荐
- 凯撒密码(Java)
事实上就是把每个字母偏移一下而已,并且字符不限于a-zA-z,可以是别的,那就很显而易见了,代码如下:定义一个Caesar密码类,成员变量只有密钥,也就是偏移量key 代码如下: public cla ...
- 蒲公英 · JELLY技术周刊 Vol.29: 前端智能化在阿里的那些事
蒲公英 · JELLY技术周刊 Vol.29 前端智能化是指借助于 AI 和机器学习的能力拓展前端,使其拥有一些超出现阶段前端能力的特性,这将是未来前端方向中一场重要的变革.目前各家互联网厂商都有自己 ...
- 经典c程序100例==91--100
[程序91] 题目:时间函数举例1 1.程序分析: 2.程序源代码: #include "stdio.h" #include "time.h" void mai ...
- CSS3之线性渐变(linear gradients)
线性渐变(linear gradients)沿着一根轴线改变颜色,从起点到终点颜色进行顺序渐变. 语法: background:linear-gradient(direction,color-stop ...
- 04 . Vue组件注册,数据交互,调试工具及组件插槽介绍及使用
vue组件 组件(Component)是 Vue.js 最强大的功能之一. 组件可以扩展 HTML 元素,封装可重用的代码. 组件系统让我们可以用独立可复用的小组件来构建大型应用,几乎任意类型的应用的 ...
- 可变参数以及stdcall
void event_warnx(const char *fmt, ...) EV_CHECK_FMT(1,2); #define EV_CHECK_FMT(a,b) __attribute__((f ...
- Linux踩坑之云服务器 ssh 连接不上
前奏:今天没事处理一下之前远程不了Linux桌面的问题时,找到一个解决方法(开始入坑): systemctl set-default graphical.tar ...
- CSS 常用列表样式
CSS 常用列表样式 CSS没学扎实,复习记录一下.下面是一些常用的属性 list-style-image 指定一个图片作为列表项的标记 默认值none,可设置为图片的url list-style-i ...
- javascript九宫格碰撞检测
JS九宫格碰撞检测这个东西 以前学过 这次主要是做面试项目web版的win10 桌面图片需要用碰撞检测 再写的时候竟然完全忘记了碰撞检测原理 和怎么写 综合来说还是写的太少 今天再学了一下 理 ...
- Mac如何像Windows一样在文件系统中查看文件?
许多Windows系统用户刚转向Mac的时候,肯定非常不适应Mac系统独特的文件系统,就像安卓转iOS系统一样,那么Mac电脑如何像Windows系统一样,能够查看电脑上的各个盘,如C盘D盘等等,然后 ...