C#实现二叉排序树代码实例
作者:Czhenya 发布时间:2021-10-10 06:26:12
标签:c#,排序,二叉排序树,查找
二叉排序树,又称为二叉查找树。它或者是一颗空树,或者是具有下列性质的二叉树:
若它的左子树不为空。则左子树上所有的结点的值均小于跟的结点值
若它的右子树部位空,则右子树的所有结点值均大于它的根结点的值
它的左右子树也分别是二叉排序树
1,排序方便
2,查找方便
3,便于插入和删除
C#链式存储二叉排序树,实现简单的排序,以及查找,具体代码如下:
namespace _2_1_3二叉排序树
{
/// <summary>
/// 结点类
/// </summary>
class BSNode
{
//结点
public BSNode LeftChild { get; set; }
public BSNode RightChild { get; set; }
public BSNode Parent { get; set; }
public int Data { get; set; }
// 构造方法
public BSNode(){}
public BSNode(int item)
{
this.Data = item;
}
}
}
using System;
namespace _2_1_3二叉排序树
{
/// <summary>
/// 二叉排序树
/// </summary>
class BSTree
{
BSNode root = null;
/// <summary>
/// 添加数据
/// </summary>
public void Add(int item)
{
//创建新结点
BSNode newNode = new BSNode(item);
if (root == null) //若为空,则创建为根结点
{
root = newNode;
}
else
{
BSNode temp = root;
while (true)
{
if (item >= temp.Data) //放在temp结点的右边
{
if (temp.RightChild == null)
{
temp.RightChild = newNode;
newNode.Parent = temp;
break;
}
else
{
temp = temp.RightChild;
}
}
else //放在temp结点的左边
{
if (temp.LeftChild == null)
{
temp.LeftChild = newNode;
newNode.Parent = temp;
break;
}
else
{
temp = temp.LeftChild;
}
}
}
}
}
/// <summary>
/// 中序遍历二叉树
/// </summary>
public void MiddleBianli()
{
MiddleBianli(root);
}
//递归方式中序遍历树
private void MiddleBianli(BSNode node)
{
if (node == null) return;
MiddleBianli(node.LeftChild);
Console.Write(node.Data + " ");
MiddleBianli(node.RightChild);
}
/// <summary>
///查找方法-1
/// </summary>
public bool Find1(int item)
{
return Find(item, root);
}
private bool Find(int item, BSNode node)
{
if (node == null) { return false; }
if (node.Data == item)
{
return true;
}
else
{
//利用二叉排序树的便利
if (item > node.Data)
{
return Find(item, node.RightChild);
}
else
{
return Find(item, node.LeftChild);
}
}
}
/// <summary>
/// 查找方法-2
/// </summary>
/// <param name="item"></param>
/// <returns></returns>
public bool Find2(int item)
{
BSNode temp = root;
while (true)
{
if (temp == null) return false;
if (temp.Data == item) return true;
if (item > temp.Data)
{
temp = temp.RightChild;
}
else
{
temp = temp.LeftChild;
}
}
}
public bool Delete(int item)
{
BSNode temp = root;
while (true)
{
if (temp == null) return false;
if (temp.Data == item)
{
Delete(temp);
return true;
}
if (item > temp.Data)
{
temp = temp.RightChild;
}
else
{
temp = temp.LeftChild;
}
}
}
public void Delete(BSNode node)
{
//叶子结点,即无子树情况
if (node.LeftChild == null && node.RightChild == null)
{
if (node.Parent == null)
{
root = null;
}
else if (node.Parent.LeftChild == node)
{
node.Parent.LeftChild = null;
}
else if (node.Parent.RightChild == node)
{
node.Parent.RightChild = null;
}
return;
}
//只有右子树的情况
if (node.LeftChild == null && node.RightChild != null)
{
node.Data = node.RightChild.Data;
node.RightChild = null;
return;
}
//只有左子树的情况
if (node.LeftChild != null && node.RightChild == null)
{
node.Data = node.LeftChild.Data;
node.LeftChild = null;
return;
}
//删除的结点有左,右子树
BSNode temp = node.RightChild;
while (true)
{
if (temp.LeftChild != null)
{
temp = temp.LeftChild;
}
else
{
break;
}
}
node.Data = temp.Data;
Delete(temp);
}
}
}
using System;
namespace _2_1_3二叉排序树
{
/// <summary>
/// 测试类
/// </summary>
class Program
{
static void Main(string[] args)
{
BSTree tree = new BSTree();
int[] data = {62,58,28,47,73,99,35,51,93,37 };
foreach (int item in data)
{
tree.Add(item);
}
Console.Write("中序遍历的结果:");
tree.MiddleBianli();
Console.WriteLine();
Console.WriteLine("Find-1方法查找62是否存在:" + tree.Find1(62));
Console.WriteLine("Find-2方法查找62是否存在:" + tree.Find2(62));
Console.WriteLine("Find-1方法查找63是否存在:" + tree.Find1(63));
Console.WriteLine("Find-2方法查找63是否存在:" + tree.Find2(63));
Console.WriteLine("删除根结点后的结果:");
tree.Delete(62);
tree.MiddleBianli();
Console.ReadKey();
}
}
}
来源:https://blog.csdn.net/Czhenya/article/details/78887679
0
投稿
猜你喜欢
- IDEA时跳转到.class的解决项目背景:jdk1.8软件环境:IDEA问题:两个不同的项目,在A项目中写了一个实体类。B项目中引用。我想
- 本文实例为大家分享了Java实现斗地主的具体代码,供大家参考,具体内容如下import java.util.ArrayList;import
- import java.util.Calendar;import java.util.Date;public class Matrix {&
- 目录1、先创建对应相关操作的注解1.1 bTable 标识表1.2 DbPrimaryKey 标识主键1.3 DbFil
- 本文实例讲述了Android编程之消息机制。分享给大家供大家参考,具体如下:一、角色描述1.Looper: 一个线程可以产生一个Looper
- springboot 异常与重定向在spring中,有两个重定向类型:301,永久性跳转302,暂时性跳转默认调用302。1.下面先通过一个
- 本人一直使用的是Eclipse作为开发工具的,不过现在IDEA非常的受推崇,所以决定上手试一试。网上有很多旗舰版的文章,我没有仔细看,我这次
- 因项目集成了Redis缓存部分数据,需要在程序启动时将数据加载到Redis中,即初始化数据到Redis。在SpringBoot项目下,即在容
- 利用AsyncQueryHandler能异步任务获取手机联系人,增加用户体验,使用起来也很方便。不多说,上干货。布局文件main.xml&l
- 一、题目描述题目:同步锁出现的目的就是为了解决多线程安全问题。同步锁的几种方式synchronized1、同步代码块2、同步方法jdk1.5
- Android 动态改变布局 &n
- 迭代器模式,一直没用过,也不会用。恰巧MyBatis框架中也使用到了迭代器模式,而且看起来还比较简单,在以后的工作中,若有需要咱们可模仿它的
- Java Set集合的遍历及实现类的比较Java中Set集合是一个不包含重复元素的Collection,首先我们先看看遍历方法package
- Android EditText限制输入字符的方法总结最近项目要求限制密码输入的字符类型, 例如不能输入中文。 &nb
- 一:问题描述 在已经root过的android设备下,app执行一个linux命令,app需要获取su权限,在某些a
- 在hibernate5中,有了一些新的变动: 新引导 APISpatial/GIS 支持Java 8 支持扩展 AUTO
- 1. 什么是AOPAOP (Aspect Oriented Programming)意为:面向切面编程,通过预编译方式和运行期 * 实现在
- 需求:校验收货地址是否超出配送范围重要:做该需求的思路就是通过卖家和卖家具体的地址信息,来获取到二者的经纬度, 此时可以使用百度的 &quo
- 前言序列化想必大家都很熟悉了,对象在进行网络传输过程中,需要序列化之后才能传输到客户端,或者客户端的数据序列化之后送达到服务端序列化的标准解
- 首先介绍一下Java解释器的概念,Java解释器:解释器是Java虚拟机非常重要的一部分,它的工作就是把字节码转化为机器码并在特定的平台进行