前言

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

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

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

正文

节点模型:

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. 建立两台linux主机的ssh信任,实现ssh免密登录远程服务器

    1.介绍 假设我们现在有AB两个服务器,要求A能够远程登录到B服务. CentOS版本:CentOS Linux release 7.6.1810 (Core) 2.实操 1.先在A服务上输入以下命令 ...

  2. 探究WPF中文字模糊的问题:TextOptions的用法

    有网友问WPF中一些文字模糊是什么问题.之前我也没有认真思考过这个问题,只是大概知道和WPF的像素对齐(pixel snapping).抗锯齿(anti-aliasing)有关,通过设置附加属性Tex ...

  3. vite 子项目 热部署 通过nginx,和父项目端口号不同,导致热更新的websocket报错的解决方案

    vite 子项目 热部署 通过nginx,和父项目端口号不同,导致热更新的websocket报错的解决方案 我的父项目端口号是8888 子项目端口号是 8013 这里报错的原因就是,热更新的webso ...

  4. 新博客 VuejsDev.com 用于梳理知识点

    新博客 VuejsDev.com 用于梳理知识点 https://www.vuejsdev.com/ 后期没有精力发布了,迁移到博客园了,防止服务器到期~ [VueJsDev] 目录列表 https: ...

  5. Android Studio自带模拟器无法访问网络问题解决

    测试APP的时候,发现Android Studio自带的模拟器访问不了百度等网站,之前一直用的好好的,觉得可能是版本的问题,也有可能是公司网络的问题(因为在家里的电脑的Android Studio的模 ...

  6. Android 开发Day2

    我的是小刺猬版本,算是比较新的版本了,还有火烈鸟和蜻蜓版啥的 新建项目(project)点击加号新建就行了.这时我们会选择一个模板作为开发的辅助起点,看上哪个就选哪个就行了.推荐新手选空项目(Empt ...

  7. 记录--Vue自动生成组件名

    这里给大家分享我在网上总结出来的一些知识,希望对大家有所帮助 unplugin-generate-component-name 一款用于以文件夹名或者setup标签写入名字来自动生成Vue组件名的插件 ...

  8. 记录--说一说css的font-size: 0

    这里给大家分享我在网上总结出来的一些知识,希望对大家有所帮助 平常我们说的font-size:0:就是设置字体大小为0对吧,但是它的用处不仅仅如此哦,它还可以消除子行内元素间额外多余的空白! 问题描述 ...

  9. Informix日志报错:Could not do a physical-order read to fetch netxt row

    jmeter请求接口,1线程不报错,2线程及以上报错"无法执行查询",看后台日志,报错Could not do a physical-order read to fetch net ...

  10. 如何使用 JavaScript 导入和导出 Excel

    前言 在现代的Web应用开发中,与Excel文件的导入和导出成为了一项常见而重要的任务.无论是数据交换.报告生成还是数据分析,与Excel文件的交互都扮演着至关重要的角色.本文小编将为大家介绍如何在熟 ...