对比上一篇文章“顺序存储二叉树”,链式存储二叉树的优点是节省空间。

二叉树的性质:

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#数据结构-二叉树-链式存储结构的更多相关文章

  1. javascript实现数据结构:线性表--线性链表(链式存储结构)

    上一节中, 线性表的顺序存储结构的特点是逻辑关系上相邻的两个元素在物理位置上也相邻,因此可以随机存取表中任一元素,它的存储位置可用一个简单,直观的公式来表示.然后,另一方面来看,这个特点也造成这种存储 ...

  2. [置顶] ※数据结构※→☆线性表结构(queue)☆============优先队列 链式存储结构(queue priority list)(十二)

    优先队列(priority queue) 普通的队列是一种先进先出的数据结构,元素在队列尾追加,而从队列头删除.在优先队列中,元素被赋予优先级.当访问元素时,具有最高优先级的元素最先删除.优先队列具有 ...

  3. c数据结构 -- 线性表之 复杂的链式存储结构

    复杂的链式存储结构 循环链表 定义:是一种头尾相接的链表(即表中最后一个结点的指针域指向头结点,整个链表形成一个环) 优点:从表中任一节点出发均可找到表中其他结点 注意:涉及遍历操作时,终止条件是判断 ...

  4. c数据结构 -- 线性表之 顺序存储结构 于 链式存储结构 (单链表)

    线性表 定义:线性表是具有相同特性的数据元素的一个有限序列 类型: 1:顺序存储结构 定义:把逻辑上相邻的数据元素存储在物理上相邻的存储单元中的存储结构 算法: #include <stdio. ...

  5. C++编程练习(6)----“实现简单的队列的链式存储结构“

    队列的链式存储结构,其实就是线性表的单链表,只不过它只能尾进头出.简称链队列. 实现代码如下: /* LinkQueue.h 头文件 */ #include<iostream> #defi ...

  6. C++编程练习(4)----“实现简单的栈的链式存储结构“

    如果栈的使用过程中元素数目变化不可预测,有时很小,有时很大,则最好使用链栈:反之,如果它的变化在可控范围内,使用顺序栈会好一些. 简单的栈的链式存储结构代码如下: /*LinkStack.h*/ #i ...

  7. C++编程练习(2)----“实现简单的线性表的链式存储结构“

    单链表采用链式存储结构,用一组任意的存储单元存放线性表的元素. 对于查找操作,单链表的时间复杂度为O(n). 对于插入和删除操作,单链表在确定位置后,插入和删除时间仅为O(1). 单链表不需要分配存储 ...

  8. java资料——顺序存储结构和链式存储结构(转)

    顺序存储结构 主要优点 节省存储空间,随机存取表中元素 缺    点 插入和删除操作需要移动元素 在计算机中用一组地址连续的存储单元依次存储线性表的各个数据元素,称作线性表的顺序存储结构. 顺序存储结 ...

  9. C++线性表的链式存储结构

    C++实现线性表的链式存储结构: 为了解决顺序存储不足:用线性表另外一种结构-链式存储.在顺序存储结构(数组描述)中,元素的地址是由数学公式决定的,而在链式储存结构中,元素的地址是随机分布的,每个元素 ...

随机推荐

  1. 80386学习(一) 80386CPU介绍

    一.80386CPU介绍 Inter80386CPU是Inter公司于1985年推出的第一款32位80x86系列的微处理器.80386的数据总线是32位的,其地址总线也是32位,因而最大可寻址4GB的 ...

  2. 【Java】线程的创建方式

    1.继承Thread类方式 这种方式适用于执行特定任务,并且需要获取处理后的数据的场景. 举例:一个用于累加数组内数据的和的线程. public class AdditionThread extend ...

  3. LeetCode-680-验证回文字符串 Ⅱ

    给定一个非空字符串 s,最多删除一个字符.判断是否能成为回文字符串. image.png 解题思路: 判断是否回文字符串:isPalindrome = lambda x: x==x[::-1],即将字 ...

  4. flex-shrink值的计算

    flex-shrink为弹性盒模型中,当弹性项不断行,并且所有弹性项的宽度只和大于弹性盒模型的可分配空间时,弹性项的收缩程度. 找到英文资料对flex-shrink的定义描述: flex-shrink ...

  5. select模型(一 改进客户端)

    一.改程序使用select来改进客户端对标准输入和套接字输入的处理,否则关闭服务器之后循环中的内容都要被gets阻塞.原程序中https://www.cnblogs.com/wsw-seu/p/841 ...

  6. ip rule 策略路由

    1. 工具安装 yum install iproute 查看工具是否安装 ip -V 2. ip rule 和 ip route ip命令中和策略路由相关的OBJECT有 rule 和 route. ...

  7. 解决自动安装Freebsd系统盘符无法确定问题

    最近因为需要用到Freebsd,所以研究了打包的一些方法,这个没什么太大问题,通过网上的一些资料可以解决,但是由于确实不太熟悉这套系统,还是碰上了一些比较麻烦的地方,目前也没看到有人写如何处理,那就自 ...

  8. 6、Sping Boot消息

    1.消息概述 可通过消息服务中间件来提升系统异步通信.扩展解耦能力 消息服务中两个重要概念:消息代理(message broker)和目的地(destination)当消息发送者发送消息以后,将由消息 ...

  9. 在 macOS 中使用 Podman

    原文链接:https://fuckcloudnative.io/posts/use-podman-in-macos/ Podman 是一个无守护程序与 Docker 命令兼容的下一代 Linux 容器 ...

  10. windbg 分析cpu异常

    1.   !threadpool  查看当前CPU状况 线程数等等 2.   !runaway 查看那几个线程使用的高 建议多抓几个dump 然后确定到底是哪个线程 3.   ~线程IDs 跳转到那个 ...