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++实现线性表的链式存储结构: 为了解决顺序存储不足:用线性表另外一种结构-链式存储.在顺序存储结构(数组描述)中,元素的地址是由数学公式决定的,而在链式储存结构中,元素的地址是随机分布的,每个元素 ...
随机推荐
- SpringBoot入门最简单的一个项目示例
使用IDEA创建一个SpringBoot项目 1.1 打开IDEA,文件-New-Project 1.2下一步,选择版本8(根据自己安装的JDK版本来选择) 1.3 下一步后点击Web,勾选Sprin ...
- 10 XSRF和XSS
10 XSRF和XSS CSRF(Cross-site request forgery)跨站请求伪造 XSS(Cross Site Scripting)跨站脚本攻击 CSRF重点在请求,XSS重点在脚 ...
- JIRA、Confluence等产品明年2月停售本地化版本,将影响中国近90%的客户!
作为目前应用最为广泛的软件开发管理软件,JIRA.Confluence等产品几乎被所有的科技型公司所应用.我们的每天的任务管理.文档编写等工作几乎都在这些软件的帮助下进行和管理.当然我也不例外,在读书 ...
- .NET操作Excel
一.读取Excel数据,并显示 1.配置文件 <configuration> <system.web> <compilation debug="true&quo ...
- [收藏] 传说中的12306买票插件-chrome专用
12306.cn买票,难死了,登录登录登录... 现在不用了... js插件+chrome浏览器: /* * 12306 Auto Query => A javascript snippet t ...
- Linux下如何创建loop device
在Linux中,有一种特殊的块设备叫loop device,这种loop device设备是通过映射操作系统上的正常的文件而形成的虚拟块设备 因为这种设备的存在,就为我们提供了一种创建一个存在于其他文 ...
- oracle的迁移工作
1.创建新数据库用户 1).创建用户和分配权限 sqlplus / as sysdba create user ENFRC_TEST_GZ_TMP identified by ENFRC_TEST_G ...
- hadoop启动脚本
记录一下一个简单的hadoop启动脚本 就是启动zookeeper集群,hadoop的HDFS和YRAN的脚本 start-cluster.sh 关于关闭的脚本,只需要顺序换一下,然后将start改为 ...
- windows 无法启动 SQL Server (MSSQLSERVER) 服务(位于本地计算机上)。错误 1069由于登入失败而无法启动 。
windows 无法启动 SQL Server (MSSQLSERVER) 服务(位于本地计算机上).错误 1069由于登入失败而无法启动. 今天登录测试服务器突然出现无法登录的情况,经排查发现,SQ ...
- Java中的Socket用法
转发链接:https://www.cnblogs.com/zhanglei93/p/6217384.html (1)Java中的Socket用法 Java中的Socket分为普通的Socket和Nio ...