前言

你好这里的一个删除,指的是如果删除的叶子节点则直接删除,如果删除的是非叶子节点,则删除的是这颗子树。

这样删除的场景并不多,这种删除方式了解即可。

十七和十六没有放树图,把树图放一下。

正文

节点模型:

public class HeroNode
{
private int no; private string name; private HeroNode left; private HeroNode right; public HeroNode(int no, string name) {
this.no = no;
this.name = name;
} public int getNo() {
return no;
}
public void setNo(int no)
{
this.no = no;
} public String getName()
{
return name;
}
public void setName(String name)
{
this.name = name;
}
public HeroNode getLeft()
{
return left;
}
public void setLeft(HeroNode left)
{
this.left = left;
}
public HeroNode getRight()
{
return right;
}
public void setRight(HeroNode right)
{
this.right = right;
}
public override string ToString()
{
return "姓名:" + name + "编号:" + no;
}
//编写前序遍历的方法 是根、左、右
public void preOrder() {
Console.WriteLine(this); if (this.left != null)
{
this.left.preOrder();
}
if (this.right != null)
{
this.right.preOrder();
}
}
//中序遍历 是左、根、右
public void infixOrder() {
if (this.left != null)
{
this.left.infixOrder();
}
Console.WriteLine(this);
if (this.right != null)
{
this.right.infixOrder();
}
}
// 后续遍历为 左、右、根
public void postOrder()
{
if (this.left != null)
{
this.left.postOrder();
}
if (this.right != null)
{
this.right.postOrder();
}
Console.WriteLine(this);
}
//前序遍历查找
public HeroNode preOrderSearch(int no)
{
HeroNode resNode = null;
record();
if (this.no == no)
{
return this;
}
if (this.left != null)
{
resNode=this.left.preOrderSearch(no);
}
if (resNode != null)
{
return resNode;
}
if (this.right != null)
{
resNode = this.right.preOrderSearch(no);
}
return resNode;
} //中序遍历查找 public HeroNode infixOrderSearch(int no)
{
HeroNode resNode = null;
if (this.left != null)
{
resNode = this.left.infixOrderSearch(no);
}
if (resNode != null)
{
return resNode;
}
record();
if (this.no == no)
{
return this;
}
if (this.right != null)
{
resNode = this.right.infixOrderSearch(no);
}
return resNode;
} //后序遍历查找 public HeroNode postOrderSearch(int no)
{ HeroNode resNode = null; if (this.left != null)
{
resNode = this.left.postOrderSearch(no);
}
if (resNode != null)
{
return resNode;
} if (this.right != null)
{
resNode = this.right.postOrderSearch(no);
}
if (resNode != null)
{
return resNode;
}
record();
if (this.no == no)
{
resNode=this;
}
return resNode;
} public void delNode(int no)
{
if (this.left!=null&&this.left.no==no)
{
this.left = null;
}
if (this.right != null && this.right.no == no)
{
this.right = null;
}
if (this.left != null)
{
this.left.delNode(no);
}
if (this.right != null)
{
this.right.delNode(no);
}
}
public void record()
{
Console.WriteLine("查找步骤为:名字" + this.name + " 编号:" + this.no);
}
}

树模型:

public class BinaryTree
{
private HeroNode root; public void setRoot(HeroNode root)
{
this.root = root;
}
//前序遍历
public void preOrder()
{
if (this.root != null)
{
this.root.preOrder();
}
else
{
Console.WriteLine("二叉树为空,无法遍历");
}
} //中序遍历
public void infixOrder()
{
if (this.root != null)
{
this.root.infixOrder();
}
else
{
Console.WriteLine("二叉树为空,无法遍历");
}
}
//后序遍历
public void postOrder()
{
if (this.root != null)
{
this.root.postOrder();
}
else
{
Console.WriteLine("二叉树为空,无法遍历");
}
}
//前序遍历查找
public HeroNode preOrderSearch(int no)
{
if (root != null)
{
return this.root.preOrderSearch(no);
} else {
return null;
}
}
//中序遍历查找
public HeroNode infixOrderSearch(int no)
{
if (root != null)
{
return this.root.infixOrderSearch(no);
}else
{
return null;
}
}
//后序遍历查找
public HeroNode postOrderSearch(int no)
{
if (root != null)
{
return this.root.postOrderSearch(no);
}else {
return null;
}
} public void delNode(int no)
{
if (root != null)
{
if (root.getNo() == no)
{
root = null;
return;
}
root.delNode(no);
}
}
}

测试:

static void Main(string[] args)
{
//先需要创建一颗二叉树
BinaryTree binaryTree = new BinaryTree();
//创建需要的结点
HeroNode root = new HeroNode(1, "宋江");
HeroNode node2 = new HeroNode(2, "吴用");
HeroNode node3 = new HeroNode(3, "卢俊义");
HeroNode node4 = new HeroNode(4, "林冲");
HeroNode node5 = new HeroNode(5, "关胜");
//设置节点
root.setLeft(node2);
root.setRight(node3);
node3.setRight(node4);
node3.setLeft(node5);
binaryTree.setRoot(root);
//删除4
Console.WriteLine("删除四后遍历");
binaryTree.delNode(4);
binaryTree.preOrder();
//删除3
Console.WriteLine("删除三后遍历");
binaryTree.delNode(3);
binaryTree.preOrder();
//删除1
Console.WriteLine("删除一后遍历");
binaryTree.delNode(1);
binaryTree.preOrder();
Console.ReadKey();
}

结果:

重新整理数据结构与算法(c#)—— 树的节点删除[十八]的更多相关文章

  1. 数据结构与算法——AVL树类的C++实现

    关于AVL树的简单介绍能够參考:数据结构与算法--AVL树简单介绍 关于二叉搜索树(也称为二叉查找树)能够參考:数据结构与算法--二叉查找树类的C++实现 AVL-tree是一个"加上了额外 ...

  2. 数据结构与算法—Trie树

    Trie,又经常叫前缀树,字典树等等.它有很多变种,如后缀树,Radix Tree/Trie,PATRICIA tree,以及bitwise版本的crit-bit tree.当然很多名字的意义其实有交 ...

  3. Android版数据结构与算法(六):树与二叉树

    版权声明:本文出自汪磊的博客,未经作者允许禁止转载. 之前的篇章主要讲解了数据结构中的线性结构,所谓线性结构就是数据与数据之间是一对一的关系,接下来我们就要进入非线性结构的世界了,主要是树与图,好了接 ...

  4. [数据结构与算法] : AVL树

    头文件 typedef int ElementType; #ifndef _AVLTREE_H_ #define _AVLTREE_H_ struct AvlNode; typedef struct ...

  5. 重新整理数据结构与算法(c#)—— 图的深度遍历和广度遍历[十一]

    参考网址:https://www.cnblogs.com/aoximin/p/13162635.html 前言 简介图: 在数据的逻辑结构D=(KR)中,如果K中结点对于关系R的前趋和后继的个数不加限 ...

  6. python数据结构与算法——字典树

    class TrieTree(): def __init__(self): self.root = {} def addNode(self,str): # 树中每个结点(除根节点),包含到该结点的单词 ...

  7. 数据结构与算法(九):AVL树详细讲解

    数据结构与算法(一):基础简介 数据结构与算法(二):基于数组的实现ArrayList源码彻底分析 数据结构与算法(三):基于链表的实现LinkedList源码彻底分析 数据结构与算法(四):基于哈希 ...

  8. python数据结构与算法

    最近忙着准备各种笔试的东西,主要看什么数据结构啊,算法啦,balahbalah啊,以前一直就没看过这些,就挑了本简单的<啊哈算法>入门,不过里面的数据结构和算法都是用C语言写的,而自己对p ...

  9. 数据结构与算法 Big O 备忘录与现实

    不论今天的计算机技术变化,新技术的出现,所有都是来自数据结构与算法基础.我们需要温故而知新.        算法.架构.策略.机器学习之间的关系.在过往和技术人员交流时,很多人对算法和架构之间的关系感 ...

  10. javascript数据结构与算法-- 二叉树

    javascript数据结构与算法-- 二叉树 树是计算机科学中经常用到的一种数据结构.树是一种非线性的数据结构,以分成的方式存储数据,树被用来存储具有层级关系的数据,比如文件系统的文件,树还被用来存 ...

随机推荐

  1. 新增、修改校验逻辑使用-Validation-的group分组校验完成-2022新项目

    一.业务场景 一般在项目开发中少不了新增.修改操作,这两个操作中传递的参数中也仅仅只有一个参数是不一致的,新增操作时没有ID, 修改时有ID,其校验逻辑也只有这一个ID校验的差别.最开始自己在写代码时 ...

  2. linux文件管理(补充)

    linux文件管理 vim编辑器 vi概述 vi 编辑器 他是linux和unix系统上最基本的文本编辑器,类似于windows系统下的记事本编辑器 vim编辑器 vim是vi的加强版,比vi更容易使 ...

  3. day06-SpringMVC底层机制简单实现-02

    SpringMVC底层机制简单实现-02 https://github.com/liyuelian/springmvc-demo.git 4.任务3-从web.xml动态获取容器配置文件 4.1分析 ...

  4. 几个常用的cmd命令

    compmgmt.msc 计算机管理  devmgmt.msc 设备管理器  diskmgmt.msc 磁盘管理工具  dfrg.msc 磁盘碎片整理  eventvwr.msc 事件查看器  fsm ...

  5. struts1之global-forwards

    当你的某个转发要经常用,并且要携带某些数据(request)的时候用全局转发,也就是global-forwards,例如我们在分页的时候,或者得到数据列表的时候.. ForwardAction呢,是为 ...

  6. 编译OpenWRT-for-MT7620A(带8021x验证)

    PS:要转载请注明出处,本人版权所有. PS: 这个只是基于<我自己>的理解, 如果和你的原则及想法相冲突,请谅解,勿喷. 前置说明   本文作为本人csdn blog的主站的备份.(Bl ...

  7. webpack 项目接入Vite的通用方案介绍(下)

    愿景 希望通过此系列文章,能给读者提供一个存/增量项目接入Vite的点子,起抛砖引玉的作用,减少这方面能力的建设成本 在阐述过程中同时也会逐渐完善webpack-vite-serve这个工具 读者可直 ...

  8. 二次元 & 动漫壁纸网站(内容记录)

    前言 天天和电脑.手机以及平板等电子设备打交道,一个好看的桌面壁纸图片当然是必不可少的,也曾经分享过<值得珍藏的高清壁纸网站推荐>,各种类型和分辨率的壁纸都有. 今天再分享些「高清二次元& ...

  9. Java SM2

    pom <?xml version="1.0" encoding="UTF-8"?> <project xmlns="http:// ...

  10. SpringBoot项目 填充Excel模板 导出 下载

    模板 下载后的效果 项目结构 pom <?xml version="1.0" encoding="UTF-8"?> <project xmln ...