java二叉树查找与删除

二叉树的查找思路(以前序为例):先判断是否为空树,然后判断根节点是否为目标节点,如果不是的话,判断左子节点是否为空,如果非空,则向左子树遍历查找,如果没有找到,则向右子树进行相同的步骤。

二叉树的删除思路(此处是以删除非叶子节点就是直接删除这个子树):先判断是否为空树,然后判断根节点是否为目标节点,如果不是的话,判断左子节点是否为空且左子节点是否为删除目标,如果不是,向左子树进行递归,如果没有找到,则向右子树进行相同的步骤。

class binarytrees
{
	node root;
	public void setroot(node root)
	{
		this.root =root;
	}
	public node preordersearch(int no)
	{
		if(root!=null)
		{
			return root.preordersearch(no);
		}
		else
		{
			System.out.println("树为空,无法查找");
			return null;
		}
	}
	public node midordersearch(int no)
	{
		if(root!=null)
		{
			return root.midordersearch(no);
		}
		else
		{
			System.out.println("树为空,无法查找");
			return null;
		}
	}
	public node postordersearch(int no)
	{
		if(root!=null)
		{
			return root.postordersearch(no);
		}
		else
		{
			System.out.println("树为空,无法查找");
			return null;
		}
	}
	public void del(int no)
	{
		if(root!=null)
		{
			if(root.no==no)
			{
				root=null;
			}
			else
			{
				root.del(no);
			}
		}
		else
		{
			System.out.println("树为空");
		}
	}
}
class node
{
	node left;
	node right;
	int no;
	String name;
	public node( int no, String name) {

		this.no = no;
		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;
	}
	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;
	}
	@Override
	public String toString() {
		return "node [no=" + no + ", name=" + name + "]";
	}
	public node preordersearch(int no)
	{
		if(this.no==no)
		{
			return this;
		}
		node 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);
		}
		if(resnode !=null)
		{
			return resnode;
		}
		return resnode;
	}
	public node midordersearch(int no)
	{
		node resnode =null;
		if(this.left!=null)
		{
			resnode =this.left.midordersearch(no);
		}
		if(resnode !=null)
		{
			return resnode;
		}
		if(this.no==no)
		{
			return this;
		}
		if(this.right!=null)
		{
			resnode = this.right.midordersearch(no);
		}
		if(resnode!=null)
		{
			return resnode;
		}
		return resnode;
	}
	public node postordersearch(int no)
	{
		node 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;
	}
	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);
		}
	}
}
public class binarytreesearch {

	public static void main(String[] args) {
		binarytrees tree = new binarytrees();
		node root = new node(1,"tom");
		node node2 = new node(2,"lee");
		node node3 = new node(3,"kitty");
		node node4 = new node(4,"anni");
		root.left=node2;
		root.right=node3;
		node3.right=node4;
		tree.setroot(root);
		tree.del(4);
		node target = tree.preordersearch(4);
		if(target !=null)
		{
			System.out.printf("找到了,no=%d name=%s",target.getNo(),target.getName());
		}
		else
		{
			System.out.println("没找到");
		}

	}

}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值