前言

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

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

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

正文

节点模型:

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. VC-MFC 在磁盘中读取文件

    1 // ReadDlg.cpp : 实现文件 2 // 3 4 #include "stdafx.h" 5 #include "Read.h" 6 #incl ...

  2. 协议I2C

    SCL   SDA   同步,半双工 开漏+弱上拉,谁用这跟线,就下拉成低电平 想输出,去拉杆子或放手,操作杆子变化 想输入,直接放手,看电平高低就行 线与,一个低电平,全部低电平,可以利用这个执行多 ...

  3. 单词本z develop vel = 到上面 从下面到上面的一种过程 抽象是相对从无到有

    单词本z develop vel = 到上面 从下面到上面的一种过程 抽象是相对从无到有 develop 发展 开发 de = down 下面 velop 这里 vel 就是 lev的反写 op = ...

  4. 单词本z custom cu = com 一起 都, st=suet 自己, om 尾缀, 都是自己身上的 = 习惯,习俗

    单词本z custom cu = com 一起 都, st=suet 自己, om 尾缀, 都是自己身上的 = 习惯,习俗 custom 来自拉丁语 consuetus cu = com st = s ...

  5. 摆脱鼠标系列 - vscode - Esc 返回时候 强制显示英文输入法 - ahk 脚本 - autoHotKey

    为什么 摆脱鼠标系列 - vscode - Esc 返回时候 强制显示英文输入法 切换网页的时候,回来还是搜索输入法,就想到按esc,直接强制英文输入法 之前vim插件里面 用了一个 im-selec ...

  6. roadmap - json格式的 思维导图

    roadmap - json格式的 思维导图 前端路线图 http://www.bitcountrys.com/frontend.html https://gitee.com/ironman1987/ ...

  7. AutoTipZen 实时根据文字是否溢出 提示title

    AutoTipZen 实时根据文字是否溢出 提示title <template> <div ref="autoTipRef" @mouseover="o ...

  8. Elasticsearch(es) 查询语句语法详解

    Elasticsearch 查询语句采用基于 RESTful 风格的接口封装成 JSON 格式的对象,称之为 Query DSL.Elasticsearch 查询分类大致分为全文查询.词项查询.复合查 ...

  9. C#事件(event)的理解

    一.多播委托的应用--观察者模式 遇到一个开发的问题? 面试者:以面向对象的思想实现一下的场景: 猫:Miao一声,紧接着引发了一系列的行为~ Miao:引发了一系列的动作: 从代码层面来说:代码这样 ...

  10. 矢量数据库与LLM的集成:实践指南

    矢量数据库与LLM的集成:实践指南 本文将了解到什么是矢量数据库,以及如何与LLMs进行集成.通过LLMs和向量数据库的结合,可以节省微调带来的开销和时间. 通常,LLM会在各种各样的数据上进行训练, ...