ICode9

精准搜索请尝试: 精确搜索
首页 > 编程语言> 文章详细

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

2020-07-06 15:08:05  阅读:209  来源: 互联网

标签:null no c# HeroNode 重新整理 right 数据结构 root public


前言

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

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

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

正文

节点模型:

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();
}

结果:

标签:null,no,c#,HeroNode,重新整理,right,数据结构,root,public
来源: https://www.cnblogs.com/aoximin/p/13253019.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有