二叉树(BinaryTree)day06

二叉树

为什么需要树这种结构

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

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

3、树存储的方式分析
能提高数据存储,读取的效率,比如利用二叉排序树(Binary Sort Tree),既可以保证数据的检索速度,同时也可以保证数据的插入,删除,修改的速度。[示意图]
在这里插入图片描述

树示意图

在这里插入图片描述

树的常用术语(结合示意图理解):

  1. 节点
  2. 根节点
  3. 父节点
  4. 子节点
  5. 叶子节点(没有子节点的节点)
  6. 节点的权(节点值)
  7. 路径(从root节点找到该节点的路线)
  8. 子树
  9. 树的高度(最大层数)
  10. 森林:多颗子树构成森林

二叉树的概念

1)树有很多种,每个节点最多只能有两个子节点的一种形式称为二叉树。
2)二叉树的子节点分为左节点和右节点
3)示意图
在这里插入图片描述

4)如果该二叉树的所有叶子节点都在最后一层,并且结点总数= 2^n-1,n为层数,则我们称为满二叉树。(完美二叉树)

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

二叉树遍历说明

使用前序,中序和后序对下面的二叉树进行遍历.

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

二叉树遍历实现

在这里插入图片描述

package com.xhl.BinaryTree;

public class Node {
	private int value;
	private Node left;
	private Node right;

	public Node(int value) {
		super();
		this.value = value;
	}

	public int getValue() {
		return value;
	}

	public void setValue(int value) {
		this.value = value;
	}

	public Node getLeft() {
		return left;
	}

	public void setLeft(Node left) {
		this.left = left;
	}

	public Node getRight() {
		return right;
	}

	public void setRight(Node right) {
		this.right = right;
	}

	@Override
	public String toString() {
		// TODO Auto-generated method stub
		return "Node:" + value;
	}

	// 前序遍历
	public void perOrder() {
		System.out.println(this);
		if (this.left != null) {
			this.left.perOrder();
		}
		if (this.right != null) {
			this.right.perOrder();
		}
	}

	// 中序遍历
	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);
	}
}

package com.xhl.BinaryTree;

public class BinaryTree {
	private Node root;

	public void setRoot(Node root) {
		this.root = root;
	}
	
	public void infixOrder() {
		if(this.root!=null) {
			root.infixOrder();
		}else {
			System.out.println("二叉树为空");
		}
	}
	
	public void postOrder() {
		if(this.root!=null) {
			root.postOrder();
		}else {
			System.out.println("二叉树为空");
		}
	}
	
	public void perOrder() {
		if(this.root!=null) {
			root.perOrder();
		}else {
			System.out.println("二叉树为空");
		}
	}
}

package com.xhl.BinaryTree;

public class BinaryTreeDemo {

	public static void main(String[] args) {
		
		BinaryTree bt = new BinaryTree();
		
		Node root = new Node(1);
		Node n2 = new Node(2);
		Node n3 = new Node(3);
		Node n4 = new Node(4);
		Node n5 = new Node(5);
		Node n6 = new Node(6);
		Node n7 = new Node(7);
		
		root.setLeft(n2);
		root.setRight(n5);
		n2.setLeft(n3);
		n2.setRight(n4);
		n5.setLeft(n6);
		n5.setRight(n7);
		bt.setRoot(root);
		
		System.out.println("前序遍历;");
		bt.perOrder();
		
		System.out.println("================================");
		System.out.println("中序遍历;");
		bt.infixOrder();
		
		System.out.println("================================");		
		
		System.out.println("后序遍历;");
		bt.postOrder();
	
	}

}

在这里插入图片描述

二叉树查找指定节点

  1. 请编写前序查找,中序查找和后序查找的方法。
  2. 并分别使用三种查找方式,查找value=5的节点
  3. 并分析各种查找方式,分别比较了多少次
    在这里插入图片描述
package com.xhl.BinaryTree;

public class Node {
	private int value;
	private Node left;
	private Node right;

	public Node(int value) {
		super();
		this.value = value;
	}

	public int getValue() {
		return value;
	}

	public void setValue(int value) {
		this.value = value;
	}

	public Node getLeft() {
		return left;
	}

	public void setLeft(Node left) {
		this.left = left;
	}

	public Node getRight() {
		return right;
	}

	public void setRight(Node right) {
		this.right = right;
	}

	@Override
	public String toString() {
		// TODO Auto-generated method stub
		return "Node:" + value;
	}

	// 前序遍历
	public void perOrder() {
		System.out.println(this);
		if (this.left != null) {
			this.left.perOrder();
		}
		if (this.right != null) {
			this.right.perOrder();
		}
	}

	// 中序遍历
	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 Node perOrderSearch(int value) {
		if(this.value==value) {
			return this;
		}
		
		Node resNode = null;
		if (this.left != null) {
			resNode = this.left.perOrderSearch(value);
		}
		
		if (resNode==null&&this.right != null) {
			resNode = this.right.perOrderSearch(value);
		}
		
		return resNode;
	}
	
	// 中序遍历查找
		public Node infixOrderSearch(int value) {
			Node resNode = null;
			if (this.left != null) {
				resNode = this.left.infixOrderSearch(value);
			}
			if(resNode==null&&this.value==value) {
				resNode=this;
			}
			
			if (resNode==null&&this.right != null) {
				resNode = this.right.infixOrderSearch(value);
			}
			return resNode;
		}

		// 后序遍历查找
		public Node postOrderSearch(int value) {
			Node resNode = null;
			if (this.left != null) {
				resNode = this.left.postOrderSearch(value);
			}

			if (resNode==null&&this.right != null) {
				resNode = this.right.postOrderSearch(value);
			}
			if(resNode==null&&this.value==value) {
				resNode=this;
			}
			return resNode;
		}
}

package com.xhl.BinaryTree;

public class BinaryTree {
	private Node root;

	public void setRoot(Node root) {
		this.root = root;
	}
	
	public void infixOrder() {
		if(this.root!=null) {
			root.infixOrder();
		}else {
			System.out.println("二叉树为空");
		}
	}
	
	public void postOrder() {
		if(this.root!=null) {
			root.postOrder();
		}else {
			System.out.println("二叉树为空");
		}
	}
	
	public void perOrder() {
		if(this.root!=null) {
			root.perOrder();
		}else {
			System.out.println("二叉树为空");
		}
	}
	
	public Node infixOrderSearch(int value) {
		if(this.root!=null) {
			return root.infixOrderSearch(value);
		}else {
			throw new RuntimeException("二叉树为空");
		}
	}
	
	public Node postOrderSearch(int value) {
		if(this.root!=null) {
			return root.postOrderSearch(value);
		}else {
			throw new RuntimeException("二叉树为空");
		}
	}
	
	public Node perOrderSearch(int value) {
		if(this.root!=null) {
			return root.perOrderSearch(value);
		}else {
			throw new RuntimeException("二叉树为空");
		}
	}	
}

二叉树删除节点

  1. 如果删除的节点是叶子节点,则删除该节点
  2. 如果删除的节点是非叶子节点,则删除该子树.
  3. 测试删掉5号子树林,3号子节点
    在这里插入图片描述

//Node类
public void delNode(int value) {
			if(left!=null) {{
				if(left.value==value) {
					left = null;
					return;
				}else {
					left.delNode(value);
				}
			}
				
			}
			if(right!=null) {
				if(right.value==value) {
					right = null;
					return;
				}else {
					right.delNode(value);
				}
			}
		}
//BinaryTree
public void delNode(int value) {
		if(root!=null) {
			if(value==root.getValue()) {
				root = null;
			}else {
				root.delNode(value);
			}
		}else {
			System.out.println("二叉树为空");
		}
	}

顺序存储二叉树

顺序存储二叉树的概念

➢基本说明
从数据存储来看,数组存储方式和树的存储方式可以相互转换,即数组可以转换成树,树也可以转换成数组。
在这里插入图片描述
➢要求:

  1. 上图的二叉树的结点,要求以数组的方式来存放arr:[1,2,3,4,5,6,7]
    2)要求 在遍历数组arr时,仍然可以以前序遍历,中序遍历和后序遍历的方式完成结点的遍历

顺序存储二叉树的特点:

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

顺序存储二叉树遍历

package com.xhl.BinaryTree.arr;
//顺序存储二叉树
public class ArrBinaryTree {
	
	private int[] arr;//存储数据节点的数组
	
	public ArrBinaryTree(int[] arr) {
		this.arr=arr;
	}
	
	
	public void preOrder(int index) {
		//如果数组为空,或者arr.length=0
		if(arr==null||arr.length==0) {
			System.out.println("数组为空");
		}
		System.out.println(arr[index]);
		
		if(index*2+1<arr.length) {
			preOrder(index*2+1);
		}
		
		if(index*2+2<arr.length) {
			preOrder(index*2+2);
		}
	}
	
	public void preOrder() {
		preOrder(0);
	}
}

package com.xhl.BinaryTree.arr;

public class ArrBinaryTreeDemo {

	public static void main(String[] args) {
		int[] arr = {1,2,3,4,5,6,7};
		
		ArrBinaryTree abt = new ArrBinaryTree(arr);
		abt.preOrder();
	}

}

线索化二叉树

问题引出

将数列{1,3,6,8,10,14 } 构建成一颗二叉树.
在这里插入图片描述
➢问题分析:

  1. 当我们对上面的二叉树进行中序遍历时,数列为{8,3,10,1,6,14 }
    2)但是6,8,10,14这几个节点的左右指针,并没有完全的利用上.
    3)如果我们希望充分的利用各个节点的左右指针,让各个节点可以指向自己的前后节点怎么办?
    4)解决方案----线索二叉树

线索二叉树基本介绍

  1. n个结点的二叉链表中含有n+1 **[**公式2n-(n-1)=n+1 ] 个空指针域。利用二叉链表中的空指针域,存放指向
    该结点在某种遍历次序下的前驱和后继结点的指针(这种附加的指针称为"线索" )
  2. 这种加上了线索的二叉链表称为线索链表,相应的二叉树称为线索二叉树(Threaded BinaryTree)。 根据线索性质的不同,线索二叉树可分为前序线索二叉树、中序线索二叉树和后序线索二叉树三种
  3. 一个结点的前一个结点,称为前驱结点
  4. 一个结点的后一个结点,称为后继结点

线索二叉树应用案例

应用案例说明:将下面的二叉树,进行中序线索二叉树。中序遍历的数列为{8, 3, 10,1, 14, 6}
在这里插入图片描述
在这里插入图片描述

➢说明:当线索化二叉树后,Node节点的属性left 和right ,有如下情况:

  1. left 指向的是左子树,也可能是指向的前驱节点.比如1节点left 指向的左子树,而10节点的left 指向的就是前驱节点.
  2. right指向的是右子树,也可能是指向后继节点,比如1节点right 指向的是右子树,而10节点的right 指向的是后继节点.
package com.xhl.BinaryTree.Threaded;

public class Node {
	private int value;
	private Node left;
	private Node right;
	
	//说明
	//1.如果leftType==0 表示指向的是左子树,如果1则表示指向前驱结点
	//2.如果rightType==0表示指向是右子树,如果1表示指向后继结点

	private int leftType;
	private int rightType;
	
	
	public int getLeftType() {
		return leftType;
	}

	public void setLeftType(int leftType) {
		this.leftType = leftType;
	}

	public int getRightType() {
		return rightType;
	}

	public void setRightType(int rightType) {
		this.rightType = rightType;
	}

	public Node(int value) {
		super();
		this.value = value;
	}

	public int getValue() {
		return value;
	}

	public void setValue(int value) {
		this.value = value;
	}

	public Node getLeft() {
		return left;
	}

	public void setLeft(Node left) {
		this.left = left;
	}

	public Node getRight() {
		return right;
	}

	public void setRight(Node right) {
		this.right = right;
	}

	@Override
	public String toString() {
		// TODO Auto-generated method stub
		return "Node:" + value;
	}

	// 前序遍历
	public void perOrder() {
		System.out.println(this);
		if (this.left != null) {
			this.left.perOrder();
		}
		if (this.right != null) {
			this.right.perOrder();
		}
	}

	// 中序遍历
	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 Node perOrderSearch(int value) {
		if(this.value==value) {
			return this;
		}
		
		Node resNode = null;
		if (this.left != null) {
			resNode = this.left.perOrderSearch(value);
		}
		
		if (resNode==null&&this.right != null) {
			resNode = this.right.perOrderSearch(value);
		}
		
		return resNode;
	}
	
	// 中序遍历查找
		public Node infixOrderSearch(int value) {
			Node resNode = null;
			if (this.left != null) {
				resNode = this.left.infixOrderSearch(value);
			}
			if(resNode==null&&this.value==value) {
				resNode=this;
			}
			
			if (resNode==null&&this.right != null) {
				resNode = this.right.infixOrderSearch(value);
			}
			return resNode;
		}

		// 后序遍历查找
		public Node postOrderSearch(int value) {
			Node resNode = null;
			if (this.left != null) {
				resNode = this.left.postOrderSearch(value);
			}

			if (resNode==null&&this.right != null) {
				resNode = this.right.postOrderSearch(value);
			}
			if(resNode==null&&this.value==value) {
				resNode=this;
			}
			return resNode;
		}
		
		public void delNode(int value) {
			if(left!=null) {{
				if(left.value==value) {
					left = null;
					return;
				}else {
					left.delNode(value);
				}
			}
				
			}
			if(right!=null) {
				if(right.value==value) {
					right = null;
					return;
				}else {
					right.delNode(value);
				}
			}
		}
}

package com.xhl.BinaryTree.Threaded;


public class ThreadedBinaryTree {
	private Node root;
	
	//为了实现线索化,需要创建要给指向当前结点的前驱结点的指针
	//在递归进行线索化时,pre总是保留前一个结点
	private Node pre = null;

	public void setRoot(Node root) {
		this.root = root;
	}
	
	
	//遍历线索二叉树的方法
	public void threadeList() {
		//定义一个变量,存储当前遍历的节点,从root开始
		Node node = root;
		while(node != null) {
			//循环找到leftType == 1 的结点,第一个找到的就是8节点
			//后面随着遍历而变化,因为当leftType==1时,说明该节点是按照线索化
			//处理后的有效节点
			while(node.getLeftType()==0) {
				node = node.getLeft();
			}
			
			//打印当前节点
			System.out.println(node);
			//如果当前节点的右指针指向的是后继节点就一直输出
			while(node.getRightType()==1) {
				node = node.getRight();
				System.out.println(node);
			}
			
			//替换这个遍历的节点
			node = node.getRight();
		}
	}
	
	//编写对二叉树进行中序线索化的方法
	public void threadedNode(Node node) {
		//如果node==null,不能线索化
		if(node==null) {
			return;
		}else {
			//1.先线索化左子树
			threadedNode(node.getLeft());
			
			//2.线索化当前结点
			//处理当前结点的前驱节点
			//以8节点来理解
			//8节点的left==null,8节点的leftType=1
			if(node.getLeft()==null) {
				node.setLeft(pre);
				node.setLeftType(1);
			}
			
			//处理后续节点
			if(pre!=null&&pre.getRight()==null) {
				//让前驱节点的右指针指向当前结点
				pre.setRight(node);
				pre.setRightType(1);
			}
			//每处理一个节点后,当前节点是下一个节点的前驱节点
			pre = node;
			
			//3.线索化右子树
			threadedNode(node.getRight());
		}
		
	}
	
	public void threadedNode() {
		this.threadedNode(root);
	}
	
	

}

package com.xhl.BinaryTree.Threaded;

public class ThreadedDemo {
	public static void main(String[] args) {
		Node node1 = new Node(1);
		Node node3 = new Node(3);
		Node node6 = new Node(6);
		
		node3.setLeft(new Node(8));
		node3.setRight(new Node(10));
		
		node6.setLeft(new Node(14));
		
		node1.setLeft(node3);
		node1.setRight(node6);
		
		ThreadedBinaryTree tbTree = new ThreadedBinaryTree();
		tbTree.setRoot(node1);
		tbTree.threadedNode();
		tbTree.threadeList();
	}
}

在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值