二叉树的基本原理和实现方法(Java)

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接: https://blog.csdn.net/qq_41291067/article/details/102745966

二叉树的来源:

数组存储方式的分析
优点:通过下标方式访问元素,速度快。对于有序数组,还可使用二分查找提高检索速度。 缺点:如果要检索具体某个值,或者插入值(按一定顺序)会整体移动,效率较低
在这里插入图片描述
链式存储方式的分析
优点:在一定程度上对数组存储方式有优化(比如:插入一个数值节点,只需要将插入节点,链接到链表中即可, 删除效率也很好)。 缺点:在进行检索时,效率仍然较低,比如(检索某个值,需要从头节点开始遍历) 【示意图】

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

树的示意图以及常用术语:

在这里插入图片描述
结点的度(Degree):结点的子树个数;
树的度:树的所有结点中最大的度数;
叶结点(Leaf):度为0的结点;
父结点(Parent):有子树的结点是其子树的根节点的父结点;
子结点/孩子结点(Child):若A结点是B结点的父结点,则称B结点是A结点的子结点;
兄弟结点(Sibling):具有同一个父结点的各结点彼此是兄弟结点;
路径和路径长度:从结点n1到nk的路径为一个结点序列n1,n2,…,nk。ni是ni+1的父结点。路径所包含边的个数为路径的长度;
祖先结点(Ancestor):沿树根到某一结点路径上的所有结点都是这个结点的祖先结点;
子孙结点(Descendant):某一结点的子树中的所有结点是这个结点的子孙;
结点的层次(Level):规定根结点在1层,其他任一结点的层数是其父结点的层数加1;
树的深度(Depth):树中所有结点中的最大层次是这棵树的深度;

二叉树的概念:

①、树有很多种,每个节点最多只能有两个子节点的一种形式称为二叉树。
②、二叉树的子节点分为左节点和右节点。
③、如果该二叉树的所有叶子节点都在最后一层,并且结点总数= 2^n -1 , n 为层数,则我们称为满二叉树。
④、如果该二叉树的所有叶子节点都在最后一层或者倒数第二层,而且最后一层的叶子节点在左边连续,倒数第二层的叶子节点在右边连续,我们称为完全二叉树。

分析二叉树

前序、中序、后续遍历思路:

前序遍历: 先输出父节点,再遍历左子树和右子树
中序遍历: 先遍历左子树,再输出父节点,再遍历右子树
后序遍历: 先遍历左子树,再遍历右子树,最后输出父节点
在这里插入图片描述

前序、中序、后序查找思路:

在这里插入图片描述

删除节点思路分析:

(删除叶子节点)
在这里插入图片描述

代码实现前中后序遍历


package Tree;

/**   
* 项目名称:algorithm   
* 类名称:BinaryTreeDemo   
* 创建人:Golven   
* 创建时间:2019年10月26日 下午3:19:30   
* @version        
*/
public class BinaryTreeDemo {

	@SuppressWarnings("null")
	public 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, "林冲");
		root.setLeft(node2);
		root.setRight(node3);
		node3.setRight(node4);
		binaryTree.setRoot(root);
		System.out.println("前序遍历:");
		binaryTree.preOrder();
		System.out.println("中序遍历:");
		binaryTree.infixOrder();
		System.out.println("后续遍历:");
		binaryTree.postOrder();

		// 前序遍历查找
		System.out.println("前序遍历查找:");
		HeroNode resNode = binaryTree.preOrderSearch(2);

		if (resNode != null) {
			System.out.printf("已找到节点,no=%d,name=%s\n", resNode.getNo(), resNode.getName());
		} else {
			System.out.printf("么有找到no=%d的英雄\n", 2);
		}
		// 中序遍历查找
		System.out.println("中序遍历查找:");
		HeroNode resNode2 = binaryTree.infixOrderSearch(2);

		if (resNode2 != null) {
			System.out.printf("已找到节点,no=%d,name=%s\n", resNode2.getNo(), resNode2.getName());
		} else {
			System.out.printf("么有找到no=%d的英雄", 2);
		}
		// 后序遍历查找
		System.out.println("后序遍历查找:");
		HeroNode resNode3 = binaryTree.postOrderSearch(2);

		if (resNode3 != null) {
			System.out.printf("已找到节点,no=%d,name=%s\n", resNode3.getNo(), resNode3.getName());
		} else {
			System.out.printf("么有找到no=%d的英雄", 2);
		}
		System.out.println("删除前,前序遍历查找:");
		binaryTree.preOrder();
		binaryTree.deleteNode(4);
		System.out.println("删除后,前序遍历查找:");
		binaryTree.preOrder();
	}

}

//创建数

class BinaryTree {
	private HeroNode root;

	public void setRoot(HeroNode root) {
		this.root = root;
	}

	// 前序遍历
	public void preOrder() {
		if (this.root != null) {
			this.root.preOrder();
		} else {
			System.out.println("二叉树为空");
		}
	}

	// 中序遍历
	public void infixOrder() {
		if (this.root != null) {
			this.root.infixOrderr();
		} else {
			System.out.println("二叉树为空");
		}
	}

	// 后序遍历
	public void postOrder() {
		if (this.root != null) {
			this.root.postOrder();
		} else {
			System.out.println("二叉树为空");
		}
	}

	// 前序遍历查找
	public HeroNode preOrderSearch(int no) {
		if (root != null) {
			return root.preOrderSearch(no);
		} else {
			return null;
		}
	}

	// 中序遍历查找
	public HeroNode infixOrderSearch(int no) {
		if (root != null) {
			return root.infixOrderSearch(no);
		} else {
			return null;
		}
	}

	// 后序遍历查找
	public HeroNode postOrderSearch(int no) {
		if (root != null) {
			return root.postOrderSearch(no);
		} else {
			return null;
		}
	}
	//删除节点
	public void deleteNode(int no) {
		//判断root是否为空
		if(root!=null) {
			//判断root是否为要删除的节点,因为之后就不会遍历到root了
			if(root.getNo()==no) {
				root = null;
			}else {
				root.deleteNode(no);
			}
		}else{
			System.out.println("该二叉树为空!!");
		}
	}

}

//创建节点
class HeroNode {
	private int no;
	private String name;
	private HeroNode left;
	private HeroNode right;
	
	public HeroNode(int no, String name) {
		super();
		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;
	}

	@Override
	public String toString() {
		return "HeroNode [no=" + no + ", name=" + name + "]";
	}

	// 前序遍历
	public void preOrder() {
		System.out.println(this);// 先输出父节点
		if (this.left != null) {
			this.left.preOrder();
		}
		if (this.right != null) {
			this.right.preOrder();
		}
	}

	// 中序遍历
	public void infixOrderr() {
		if (this.left != null) {
			this.left.infixOrderr();
		}
		System.out.println(this);// 输出父节点
		if (this.right != null) {
			this.right.infixOrderr();
		}
	}

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

	// 前序遍历的查找
	@SuppressWarnings("unused")
	public HeroNode preOrderSearch(int no) {
		System.out.println("进入前序遍历——");
		if (this.no == no) {
			return this;
		}
		HeroNode resNode = null;
		// 判断该节点的左子节点是否为空,如果不为空,则向左进行递归查找
		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;
	}

	// 中序遍历查找
	@SuppressWarnings("null")
	public HeroNode infixOrderSearch(int no) {

		HeroNode resNode = null;
		if (this.left != null) {// 判断当前节点的左子节点是否为空,不为空,则向左递归
			resNode = this.left.infixOrderSearch(no);
		}
		if (resNode != null) {
			return resNode;
		}
		System.out.println("进入中序查找——");
		// 比较当前节点
		if (this.no == no) {
			return this;
		}
		if (this.right != null) {
			resNode.right.infixOrderr();
		}
		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;
		}
		// 如果左右子数都没右找到,则判断当前节点
		System.out.println("进入后序查找——");
		if (this.no == no) {
			return this;
		}
		return resNode;
	}

	// 递归删除指定节点
	// 思路
	/*
	 * 1. 因为我们的二叉树是单向的,所以我们是判断当前结点的子结点是否需要删除结点,而不能去判断当前这个结点是不是需要删除结点. 
	 * 2.如果当前结点的左子结点不为空,并且左子结点 就是要删除结点,就将this.left = null; 并且就返回(结束递归删除) 
	 * 3.如果当前结点的右子结点不为空,并且右子结点 就是要删除结点,就将this.right= null ;并且就返回(结束递归删除) 
	 * 4.如果第2和第3步没有删除结点,那么我们就需要向左子树进行递归删除 5. 如果第4步也没有删除结点,则应当向右子树进行递归删除.
	 * 
	 */
	public void deleteNode(int no) {
		//如果当前结点的左子结点不为空,并且左子结点 就是要删除结点,就将this.left = null; 并且就返回(结束递归删除) 
		if (this.left != null && this.left.no == no) {
			this.left = null;
			return;
		}
		//如果当前结点的右子结点不为空,并且右子结点 就是要删除结点,就将this.right= null ;并且就返回(结束递归删除) 
		if(this.right!=null&&this.right.no==no) {
			this.right=null;
			return;
		}
		//向左子树进行递归删除
		if(this.left!=null) {
			this.left.deleteNode(no);
		}
		//向右子树进行递归删除
		if(this.right!=null) {
			this.right.deleteNode(no);
		}
	}
}

猜你喜欢

转载自blog.csdn.net/qq_41291067/article/details/102745966
今日推荐