二叉树的前,中,后序遍历,查找,删除

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

2)二叉树的子节点分为左节点和右节点。

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

4)果该二叉树的所有叶子节点都在最后一层或者倒数第二层,而且最后一层的叶子节点在左边连续,倒数第二层的叶子节点在右边连续,我们称为完全二叉树。

前序遍历: 先输出父节点,再遍历左子树和右子树

序遍历: 先遍历左子树,再输出父节点,再遍历右子树

序遍历: 先遍历左子树,再遍历右子树,最后输出父节点

小结: 看输出父节点的顺序,就确定是前序,中序还是后序

 

package com.wang;

public class Binary {

	public static void main(String[] args) {
		//先创建一棵二叉树
		Tree binary = new Tree();
		
		//创建需要的节点
		Node root = new Node(1, "宋江");
		Node Node2 = new Node(2, "吴用");
		Node Node3 = new Node(3, "卢俊义");
		Node Node4 = new Node(4, "林冲");
		
		//手动创建二叉树
		root.setLeft(Node2);
		root.setRight(Node3);
		Node3.setRight(Node4);
		binary.setRoot(root);
		
		System.out.println("前序遍历:");
		binary.perOeder();
		
		System.out.println("中序遍历:");
		binary.infixOeder();
		
		System.out.println("后序遍历:");
		binary.afterOeder();
		
		System.out.println("前序查找:");
		Node preSeach = binary.preSeach(2);
		if (preSeach!=null) {
			System.out.println(preSeach);
		}else {
			System.out.println("无");
		}
		
		System.out.println("中序查找:");
		Node infixSeach = binary.infixSeach(2);
		if (infixSeach!=null) {
			System.out.println(preSeach);
		}else {
			System.out.println("无");
		}
		
		System.out.println("后序查找:");
		Node afterSeach = binary.afterSeach(2);
		if (afterSeach!=null) {
			System.out.println(preSeach);
		}else {
			System.out.println("无");
		}
		
		binary.del(4);
		System.out.println("删除后的前序遍历:");
		binary.perOeder();
	}

}


//二叉树
class Tree{
	//根节点
	private Node root;

	public void setRoot(Node root) {
		this.root = root;
	}
	
	//前序遍历
	public void perOeder() {
		if (this.root != null) {
			this.root.perOrder();
		}else {
			System.out.println("当前二叉树为空");
		}
	}
	
	//中序遍历
	public void infixOeder() {
		if (this.root != null) {
			this.root.infixOrder();
		}else {
			System.out.println("当前二叉树为空");
		}
	}
	
	//后序遍历
	public void afterOeder() {
		if (this.root != null) {
			this.root.afterOrder();
		}else {
			System.out.println("当前二叉树为空");
		}
	}
	
	
	//前序查找
	public Node preSeach(int no) {
		if (root != null) {
			return root.perSearch(no);
		}else {
			return null;
		}
	}
	
	//中序查找
	public Node infixSeach(int no) {
		if (root != null) {
			return root.infixSearch(no);
		}else {
			return null;
		}
	}
	
	//后序查找
	public Node afterSeach(int no) {
		if (root != null) {
			return root.afterSearch(no);
		}else {
			return null;
		}
	}
	
	//删除:如果是叶子节点就删除该节点,如果是父节点就删除它生成的树
	public void del(int no) {
		//根节点不为空
		if (root != null) {
			//根节点是否为要删除的节点
			if (root.getNo()==no) {
				this.root=null;
			}else {
				root.del(no);
			}
		}else {
			System.out.println("空树");
		}
	}
}



//节点
class Node{
	private int no;
	private String name;
	private Node left;
	private Node right;
	public Node(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 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() {
		return "Node [no=" + no + ", name=" + name + "]";
	}
	
	//前序遍历
	public void perOrder() {
		//父节点(D)
		System.out.println(this);
		//递归向左遍历(L)
		if (this.left != null) {
			this.left.perOrder();
		}
		//递归向右遍历(R)
		if (this.right != null) {
			this.right.perOrder();
		}
	}
	
	//中序遍历
	public void infixOrder() {
		//递归向左遍历(L)
		if (this.left != null) {
			this.left.perOrder();
		}
		//父节点(D)
		System.out.println(this);
		//递归向右遍历(R)
		if (this.right != null) {
			this.right.perOrder();
		}
	}
	
	//后序遍历
	public void afterOrder() {
		//递归向左遍历(L)
		if (this.left != null) {
			this.left.perOrder();
		}
		//递归向右遍历(R)
		if (this.right != null) {
			this.right.perOrder();
		}
		//父节点(D)
		System.out.println(this);
	}
	
	
	//前序查找
	//根据节点进行查找,找到就返回该节点,找不到返回null
	public Node perSearch(int no) {
		
		System.out.println("进入前序查找");
		
		//父节点是不是
		if (this.no==no) {
			return this;
		}
		//不是则判断左节点是否为空,不为空则递归前序查找
		Node ans=null;
		if (this.left != null) {
			ans = this.left.perSearch(no);
		}
		//找到
		if (ans != null) {
			return ans;
		}
		//没找到则判断右节点是否为空,不为空则递归前序查找
		if (this.right != null) {
			ans = this.right.perSearch(no);
		}
		return ans;
	}
	
	
	    //中序查找
		//根据节点进行查找,找到就返回该节点,找不到返回null
		public Node infixSearch(int no) {
			
			//不是则判断左节点是否为空,不为空则递归前序查找
			Node ans=null;
			if (this.left != null) {
				ans = this.left.infixSearch(no);
			}
			//找到
			if (ans != null) {
				return ans;
			}
			//这一句要写在this的前面,因为前面有判断是否为空,会增加额外的次数
			System.out.println("进入中序查找");
			
			//父节点是不是
			if (this.no==no) {
				return this;
			}
			//没找到则判断右节点是否为空,不为空则递归前序查找
			if (this.right != null) {
				ans = this.right.infixSearch(no);
			}
			return ans;
		}
		
	
	    //后序查找
		//根据节点进行查找,找到就返回该节点,找不到返回null
		public Node afterSearch(int no) {
			
			//不是则判断左节点是否为空,不为空则递归前序查找
			Node ans=null;
			if (this.left != null) {
				ans = this.left.afterSearch(no);
			}
			//找到
			if (ans != null) {
				return ans;
			}
			//没找到则判断右节点是否为空,不为空则递归前序查找
			if (this.right != null) {
				ans = this.right.afterSearch(no);
			}
			//找到
			if (ans != null) {
				return ans;
			}
			
			//这一句要写在this的前面,因为前面有判断是否为空,会增加额外的次数
			System.out.println("进入后序查找");
			
			//父节点是不是
			if (this.no==no) {
				return this;
			}
			return ans;
		}
		
	    //删除节点:因为二叉树是单向的,所以我们判断的是当前节点的   子节点   是否为要删除的节点
		public void del(int no) {
			//1.如果当前节点的左节点不为空并且为要删除的节点,就将this.left=null,并返回(结束递归并删除)
			if (this.left != null&&this.left.no == no) {
				this.left=null;
				return;
			}
			
			//2.如果当前节点的右节点不为空并且为要删除的节点,就将this.right=null,并返回(结束递归并删除)
			if (this.right != null&&this.right.no == no) {
				this.right=null;
				return;
			}
			
			//3.都没有删除就向左子树进行递归
			if (this.left!=null) {
				this.left.del(no);
			}
			
			//4.都没有删除就向右子树进行递归
			if (this.right!=null) {
				this.right.del(no);
			}
		}
}

  • 3
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

1while(true){learn}

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值