树学习记录

树学习记录


前言

为什么需要树这种数据结构

1) 数组存储方式的分析

优点:通过下标方式访问元素,速度快。对于有序数组,还可使用二分查找提高检索速度。缺点:如果要检索具体某个值,或者插入值(按一定顺序)会整体移动,效率较低
[示意图] 画出操作示意图:
在这里插入图片描述

2) 链式存储方式的分析

优点:在一定程度上对数组存储方式有优化(比如:插入一个数值节点,只需要将插入节点,链接到链表中即可, 删除效率也很好)。
缺点:在进行检索时,效率仍然较低,比如(检索某个值,需要从头节点开始遍历)
【示意图】操作示意图:
在这里插入图片描述

3) 树存储方式的分析

能提高数据存储,读取的效率, 比如利用 二叉排序树(Binary Sort Tree),既可以保证数据的检索速度,同时也可以保证数据的插入,删除,修改的速度。【示意图,后面详讲】
案例: [7, 3, 10, 1, 5, 9, 12]
在这里插入图片描述

树的示意图

在这里插入图片描述

树的常用术语

在这里插入图片描述

一、二叉树的概念

  1. 树有很多种,每个节点最多只能有两个子节点的一种形式称为二叉树。

  2. 二叉树的子节点分为左节点和右节点
    在这里插入图片描述

  3. 如果该二叉树的所有叶子节点都在最后一层,并且结点总数= 2^n -1 , n 为层数,则我们称为满二叉树
    在这里插入图片描述

  4. 如果该二叉树的所有叶子节点都在最后一层或者倒数第二层,而且最后一层的叶子节点在左边连续,倒数第二层的叶子节点在右边连续,我们称为完全二叉树
    在这里插入图片描述

二、二叉树的遍历

  1. 前序遍历: 先输出父节点,再遍历左子树和右子树
  2. 中序遍历: 先遍历左子树,再输出父节点,再遍历右子树
  3. 后序遍历: 先遍历左子树,再遍历右子树,最后输出父节点
  4. 小结: 看输出父节点的顺序,就确定是前序,中序还是后序

1.二叉树遍历应用实例(前序,中序,后序)

应用实例的说明和思路

在这里插入图片描述

2.二叉树遍历查找指定结点(前序,中序,后序)

思路

在这里插入图片描述

3.二叉树删除结点

思路

在这里插入图片描述

遍历、查找、删除的代码实现:


public class BinaryTreeDemo {

	public static void main(String[] args) {
		// 创建结点
		HeroNode root = new HeroNode(1, "设计师");
		HeroNode node2 = new HeroNode(2, "剑豪-亚索");
		HeroNode node3 = new HeroNode(3, "剑魔-亚托克斯");
		HeroNode node4 = new HeroNode(4, "剑圣-易");
		// 添加一个第五结点 放在node3的左结点
		HeroNode node5 = new HeroNode(5, "剑姬-菲奥娜");
		// 建立结点关系
		root.setLeft(node2);
		root.setRight(node3);
		node3.setRight(node4);
		node3.setLeft(node5);
		
		// 创建二叉树
		BinaryTr rTr = new BinaryTr();
		rTr.setRoot(root);// 设置根节点
		
		// 测试 
		System.out.println("前序遍历");
		rTr.preOrder();// 1 2 3 5 4
		
		// 测试
		System.out.println("中序遍历");
		rTr.infixOrder();// 2 1 5 3 4
		
		// 测试
		System.out.println("后序遍历");
		rTr.postOrder();// 2 5 4 3 1
		
		int no = 5;// 要查找的英雄编号
//		// 测试前序遍历查找
//		HeroNode resHeroNode = rTr.preOrderFind(no);
//		if (resHeroNode != null) {
		// 4
//			System.out.printf("编号%d的英雄名称为:%s",no,resHeroNode.getNameString());
//		}else {
//			System.out.printf("编号%d的英雄不存在",no);
//		}
//		// 测试中序遍历查找
//		HeroNode resHeroNode = rTr.infixOrderFind(no);
//		if (resHeroNode != null) {
//			// 3
//			System.out.printf("编号%d的英雄名称为:%s",no,resHeroNode.getNameString());
//		}else {
//			System.out.printf("编号%d的英雄不存在",no);
//		}
//		// 测试后序遍历查找
//		HeroNode resHeroNode = rTr.postOrderFind(no);
//		if (resHeroNode != null) {
//			// 2
//			System.out.printf("编号%d的英雄名称为:%s",no,resHeroNode.getNameString());
//		}else {
//			System.out.printf("编号%d的英雄不存在",no);
//		}
		// 测试删除
		// 测试 
		System.out.println("删除前……前序遍历");
		rTr.preOrder();// 1 2 3 5 4
		rTr.del(5);
		System.out.println("删除后……前序遍历");
		rTr.preOrder();// 1 2 3  4
	}

}
/*
 * 	二叉树
 */
class BinaryTr{
	private HeroNode root;

	public HeroNode getRoot() {
		return root;
	}

	public void setRoot(HeroNode root) {
		this.root = root;
	}
	
	// 前序遍历
	public void preOrder() {
		if (root != null) {
			this.root.preOrder();
		}else {
			System.out.println("二叉树为空 遍历个der!");
		}
	}
	// 中序遍历
	public void infixOrder() {
		if (root != null) {
			this.root.infixOrder();
		}else {
			System.out.println("二叉树为空 遍历个der!");
		}
	}
	// 后序遍历
	public void postOrder() {
		if (root != null) {
			this.root.postOrder();
		}else {
			System.out.println("二叉树为空 遍历个der!");
		}
	}
	// 前序遍历查找
	public HeroNode preOrderFind(int no) {
		if (root != null) {
			return this.root.preOrderFind(no);
		}else {
			return null;
		}
	}
	
	// 中序遍历查找
	public HeroNode infixOrderFind(int no) {
		if (root != null) {
			return this.root.infixOrderFind(no);
		}else {
			return null;
		}
	}
	
	// 后序遍历查找
	public HeroNode postOrderFind(int no) {
		if (root != null) {
			return this.root.postOrderFind(no);
		}else {
			return null;
		}
	}
	// 删除结点
	public void del(int no) {
		if (root != null) {
			if (root.getNo() == no) {
				// 说明根节点就是要删除的 移除树
				root = null;
				return;
			}else {
				root.del(no);
			}
		}else {
			System.out.println("树为空 压根删不了");
		}
	}
}
/*
 * 	英雄结点
 */
class HeroNode{
	private int no;
	private String nameString;
	private HeroNode left;// 默认为null
	private HeroNode right;// 默认为null
	public HeroNode(int no, String nameString) {
		super();
		this.no = no;
		this.nameString = nameString;
	}
	public int getNo() {
		return no;
	}
	public void setNo(int no) {
		this.no = no;
	}
	public String getNameString() {
		return nameString;
	}
	public void setNameString(String nameString) {
		this.nameString = nameString;
	}
	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;
	}
	@Override
	public String toString() {
		return "HeroNode [no=" + no + ", nameString=" + nameString + "]";
	}
	// 前序遍历
	public void preOrder() {
		// 输出父结点
		System.out.println(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();
		}
		// 输出父结点
		System.out.println(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();
		}
		// 输出父结点
		System.out.println(this);
	}
	
	// 前序遍历查找
	public HeroNode preOrderFind(int no) {
		System.out.println("前序遍历查找~");
		// 判断当前结点是否为要查找的结点 是就直接返回
		if (this.no == no) {
			return this;
		}
		// 当前结点不是要找的结点 开始向左子结点递归查找
		// 定义一个临时变量 标识是否找到
		HeroNode resNode = null;
		if (this.left != null) {
			resNode = this.left.preOrderFind(no);
		}
		// 如果临时结点不为null说明在左子结点找到了直接返回resNode
		if (resNode != null) {
			return resNode;
		}
		// 左子结点递归查找没找到 继续向右子结点递归查找
		if (this.right != null) {
			resNode = this.right.preOrderFind(no);
		}
		return resNode;
	}
	
	// 中序遍历查找
	public HeroNode infixOrderFind(int no) {
		// 开始向左子结点递归查找
		// 定义一个临时变量 标识是否找到
		HeroNode resNode = null;
		if (this.left != null) {
			resNode = this.left.infixOrderFind(no);
		}
		// 如果临时结点不为null说明在左子结点找到了直接返回resNode
		if (resNode != null) {
			return resNode;
		}
		System.out.println("中序遍历查找~");
		// 左子结点没找到 判断当前结点是否为要查找的结点 是就直接返回
		if (this.no == no) {
			return this;
		}
		// 当前结点查找没找到 继续向右子结点递归查找
		if (this.right != null) {
			resNode = this.right.infixOrderFind(no);
		}
		return resNode;
	}
	
	// 后序遍历查找
	public HeroNode postOrderFind(int no) {
		// 开始向左子结点递归查找
		// 定义一个临时变量 标识是否找到
		HeroNode resNode = null;
		if (this.left != null) {
			resNode = this.left.postOrderFind(no);
		}
		// 如果临时结点不为null说明在左子结点找到了直接返回resNode
		if (resNode != null) {
			return resNode;
		}
		// 左子结点查找没找到 继续向右子结点递归查找
		if (this.right != null) {
			resNode = this.right.postOrderFind(no);
		}
		// 如果临时结点不为null说明在右子结点找到了直接返回resNode
		if (resNode != null) {
			return resNode;
		}
		System.out.println("后序遍历查找~");
		// 左子结点没找到 判断当前结点是否为要查找的结点 是就直接返回
		if (this.no == no) {
			return this;
		}
		return resNode;
	}
	
	// 删除叶子结点
	public void del(int no) {
		if (this.left != null && this.left.no == no) {
			this.left = null;
			return;
		}
		if (this.right != null && this.right.no == no) {
			this.right = null;
			return;
		}
		if (this.left != null) {
			this.left.del(no);
		}
		if (this.right != null) {
			this.right.del(no);
		}
	}
}

三、顺序存储二叉树

3.1 基本说明

从数据存储来看,数组存储方式和树的存储方式可以相互转换,即数组可以转换成树,树也可以转换成数组, 看下面的示意图。
在这里插入图片描述

3.2 顺序存储二叉树的特点

  1. 顺序二叉树通常只考虑完全二叉树
  2. 第 n 个元素的左子节点为 2 * n + 1
  3. 第 n 个元素的右子节点为 2 * n + 2
  4. 第 n 个元素的父节点为 (n-1) / 2
  5. n : 表示二叉树中的第几个元素(按 0 开始编号如图所示)

3.3 顺序存储二叉树遍历

代码实现

// 顺序存储二叉树
public class ArrayTreeDemo {

	public static void main(String[] args) {
		int[] arr = {1,2,3,4,5,6,7};
		ArrayTree arrayTree = new ArrayTree(arr);
		// 测试前序遍历
		arrayTree.preOrder();// 1245367
		// 测试中序遍历
		arrayTree.infixOrder();// 4251637
		// 测试后序遍历
		arrayTree.postOrder();// 4526731
	}

}
class ArrayTree{
	private int[] arr;

	public int[] getArr() {
		return arr;
	}
	
	public ArrayTree(int[] arr) {
		super();
		this.arr = arr;
	}

	public void setArr(int[] arr) {
		this.arr = arr;
	}
	// 重载前序遍历
	public void preOrder() {
		this.preOrder(0);
		System.out.println("前序遍历结束~");
	}
	// 前序遍历
	public void preOrder(int index) {
		if (arr == null || arr.length == 0) {
			System.out.println("这二叉树数组都没元素 你遍历个der");
			return;
		}
		// 打印当前元素
		System.out.printf("当前元素:%d\t",arr[index]);
		// 左子树递归
		if ((index * 2 + 1) < arr.length) {
			preOrder(index * 2 + 1);
		}
		// 右子树递归
		if ((index * 2 + 2) < arr.length) {
			preOrder(index * 2 + 2);
		}
	}
	// 重载中序遍历
	public void infixOrder() {
		this.infixOrder(0);
		System.out.println("中序遍历结束~");
	}
	// 中序遍历
	public void infixOrder(int index) {
		if (arr == null || arr.length == 0) {
			System.out.println("这二叉树数组都没元素 你遍历个der");
			return;
		}
		// 左子树递归
		if ((index * 2 + 1) < arr.length) {
			infixOrder(index * 2 + 1);
		}
		// 打印当前元素
		System.out.printf("当前元素:%d\t",arr[index]);
		// 右子树递归
		if ((index * 2 + 2) < arr.length) {
			infixOrder(index * 2 + 2);
		}
	}
	// 重载后序遍历
	public void postOrder() {
		this.postOrder(0);
		System.out.println("后序遍历结束~");
	}
	// 后序遍历
	public void postOrder(int index) {
		if (arr == null || arr.length == 0) {
			System.out.println("这二叉树数组都没元素 你遍历个der");
			return;
		}
		// 左子树递归
		if ((index * 2 + 1) < arr.length) {
			postOrder(index * 2 + 1);
		}
		// 右子树递归
		if ((index * 2 + 2) < arr.length) {
			postOrder(index * 2 + 2);
		}
		// 打印当前元素
		System.out.printf("当前元素:%d\t",arr[index]);
	}
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值