Java版二叉树遍历,查找,顺序化存储代码实现

前中后序遍历,查找

package Tree;
/**
 * 二叉树前 中 后 遍历,查找
 * @author bai
 *
 */
public class BinaryTreeDemo {
	public static void main(String[] args) {
		//先需要创建一颗二叉树
		BinaryTree binaryTree = new BinaryTree();
		//创建需要的节点
		HeroNode root = new HeroNode(1, "钢铁侠");
		HeroNode node1 = new HeroNode(2, "无敌浩克");
		HeroNode node2 = new HeroNode(3, "美国队长");
		HeroNode node3 = new HeroNode(4, "战争机器");
		HeroNode node4 = new HeroNode(5, "蜘蛛侠");
		
		//说明:先手动创建二叉树,后面学习用递归的方法调用
		root.setLeft(node1);
		root.setRight(node2);
		node2.setRight(node3);
		node2.setLeft(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(5);
		if(resNode!=null){
			System.out.println(resNode);
		}else{
			System.out.println("null");
		}
		
		//中序查找测试
		System.out.println("中序查找");
		resNode = binaryTree.infixOrderSearch(3);
		if(resNode!=null){
			System.out.println(resNode);
		}else{
			System.out.println("null");
		}
		
		//后序查找遍历
		System.out.println("后序查找");
		resNode = binaryTree.postOrderSearch(2);
		if(resNode!=null){
			System.out.println(resNode);
		}else{
			System.out.println("null");
		}
		
		//删除测试
		System.out.println("删除前,前序遍历");
		binaryTree.preOrder();
		binaryTree.delNode(3);
		System.out.println("删除后,前序遍历");
		binaryTree.preOrder();
	}
}
//定义一个BinaryTree 二叉数
class BinaryTree{
	private HeroNode root;
	public void setRoot(HeroNode root){
		this.root = root;
	}
	//删除结点
	public void delNode(int no){
		if(root!=null){
			if(root.getNo()==no){
				root = null;
			}else{
				root.delNode(no);
			}
		}else{
			System.out.println("空树,不能删除");
		}
	}
	
	//前序遍历
	public void preOrder(){
		if(this.root!=null){
			this.root.preOrder();
		}else{
			System.out.println("二叉树为空,无法遍历");
		}
	}
	//中序遍历
	public void infixOrder(){
		if(this.root!=null){
			this.root.infixOrder();
		}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.preOrderseach(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;
		}
	}
}
//先创建HeroNode结点
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 + "]";
	}
	//递归删除节点
	//1 如果删除的节点是叶子节点,则删除该节点
	//2 如果删除的节点是非叶子节点,则删除该子树
	public void delNode(int no){
		//思路
		/*
		 * 1 因为二叉树是单向的,所以我们是判断当前结点是否需要删除结点
		 * 2如果当前节点的左子结点不为空,并且左子结点,就是要删除结点,就将this.left=null,并且就返回
		 * 3如果当前结点的右子节点不为空,并且右子结点,就是要删除结点,就将this.right = null,并且返回
		 * 4如果第2 3 步没有删除结点,那么我们就需要向左子树进行递归删除
		 * 5 如果第4 步也没有删除结点,则应当向右子树进行递归删除
		 */
		//如果当前结点的左子节点不为空,并且左子结点就是要删除结点,就将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.delNode(no);
		}
		//右子树递归删除
		if(this.right!=null){
			this.right.delNode(no);
		}
	}
	
	
	
	
	//编写前序遍历的方法
	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);
	}
	//前序遍历查找
	/**
	 * @param no 查找no
	 * @return 如果查找到就返回node 如果没有找到就返回null
	 */
	public HeroNode preOrderseach(int no){
		
		//比较当前节点
		if(this.no == no){
			return this;
		}
		//1 判断当前节点的左子节点是否为空,如果不为空,则递归前序查找
		//2 如果左递归前序查找,找到节点,则返回
		HeroNode resNode  = null;
		if(this.left!=null){
			resNode = this.left.preOrderseach(no);
		}
		if(resNode!=null){
			return resNode;
		}
		//1 左递归前序查找,找到节点,则返回,否继续判断
		//2 当前节点的右子节点是否为空,如果不空,则继续向右递归前序查找
		if(this.right !=null){
			resNode = this.right.preOrderseach(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;
		}
		//如果找到,则返回,如果没有找到,就跟当前节点比较,如果是则是返回当前节点
		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;
		}
		//判断当前节点
		if(this.no == no){
			return this;
		}
		return resNode;
	}
}

顺序存储

package Tree;
/*
 * 顺序化存储
 */
public class ArrBinaryTreeDemo {
	public static void main(String[] args) {
		int[] arr ={1,2,3,4,5,6,7};
		ArrBinaryTree arrBinaryTree = new ArrBinaryTree(arr);
		arrBinaryTree.preOrder();
	}
}
class ArrBinaryTree{
	private int[] arr;//存储数据结点的数组

	public ArrBinaryTree(int[] arr) {
		super();
		this.arr = arr;
	}
	
	//重载preOrder
	public void preOrder(){
		this.preOrder(0);
	}
	//编写一个方法,完成顺序存储二叉树的前序遍历
	public void preOrder(int index){
		if(arr==null||arr.length==0){
			System.out.println("数组为空");
		}
		//输出当前这个元素
		System.out.println(arr[index]);
		if((index*2+1)<arr.length){
			preOrder(2*index+1);
		}
		if((index*2+2)<arr.length){
			preOrder(index*2+2);
		}
	}
	//编写一个方法,完成顺序存储二叉树的中序遍历
	public void infixOrder(int index){
		if(arr==null&&arr.length==0){
			System.out.println("数组为空");
		}
		if((index*2+1)<arr.length){
			infixOrder(2*index+1);
		}
		System.out.println(arr[index]);
		if((index*2+2)<arr.length){
			infixOrder(index*2+2);
		}
	}
	//顺序存储二叉树的后序遍历
	public void postOrder(int index){
		if(arr==null&&arr.length==0){
			System.out.println("数组为空");
		}
		if((index*2+1)<arr.length){
			infixOrder(2*index+1);
		}
		if((index*2+2)<arr.length){
			infixOrder(index*2+2);
		}
		System.out.println(arr[index]);
	}
	
}
发布了70 篇原创文章 · 获赞 8 · 访问量 3261

猜你喜欢

转载自blog.csdn.net/gldbys/article/details/105185148