Java 数据结构 二叉树 遍历 查找 删除

节点:

package com.m.demo10;

public class Node {
	public int id;
	public String name;
	public Node left;
	public Node right;
	
	public Node(int id, String name) {
		
		this.id = id;
		this.name = name;
	}

	@Override
	public String toString() {
		return "Node [id=" + id + ", name=" + name + "]";
	}
//	前序遍历
	public void preList() {
		System.out.print(this.id+" ");
		if(this.left!=null) {
			this.left.preList();
		}
		if(this.right!=null) {
			this.right.preList();
		}
	}
	/中序遍历
	public void infixList() {
		
		if(this.left!=null) {
			this.left.infixList();
		}
		
		System.out.print(this.id+" ");
		if(this.right!=null) {
			this.right.infixList();
		}
	}
	//后续遍历
	public void postList() {
		
		if(this.left!=null) {
			this.left.postList();
		}
		
		if(this.right!=null) {
			this.right.postList();
		}
		System.out.print(this.id+" ");
	}
	/前序查找
public Node preFind(int id) {//前序查找,先找当前的,如果不是就找左节点的,如果不是,就找右节点的,找到了 temp是有值的,没有找的 temp=null
	Node temp = null;
	if(this.id==id) {
		temp=this;
		return temp;
	}
	if(this.left!=null&&temp==null) {
		temp=this.left.preFind(id);
	}
	if(temp!=null) {
		return temp;
	}
	if(this.right!=null&&temp==null) {
		temp=this.right.preFind(id);
	}
	return temp;
}
中序查找
public Node infixFind(int id) {
	Node temp = null;
	
	if(this.left!=null&&temp==null) {
		temp=this.left.preFind(id);
	}
	if(temp!=null) {
		return temp;
	}
	if(this.id==id) {
		temp=this;
		return temp;
	}
	if(this.right!=null&&temp==null) {
		temp=this.right.preFind(id);
	}
	return temp;
}
删除:找到后 直接置空
public boolean delNode(int id) {
	boolean b=false;
	if(this.left!=null&&this.left.id==id) {
		this.left=null;
		
		return true;
	}
	if(this.right!=null&&this.right.id==id) {
		this.right=null;
		return true;
	}
	if(this.left!=null) {
		b = this.left.delNode(id);
	}
	if(b) {
		return true;
	}
	if(this.right!=null) {
		b=this.right.delNode(id);
	}
	return b;
}
删除:找到后 如果是叶子节点 直接删除,如果有左节点 则让左节点代替,如果只有右节点,则让右节点代替
public boolean delNode2(int id) {
	boolean b=false;
	if(this.left!=null&&this.left.id==id) {
		if(this.left.left==null&&this.left.right==null) {
			this.left=null;
			return true;
		}else if(this.left.left!=null) {
			this.left.id=this.left.left.id;
			this.left.name=this.left.left.name;
			this.left.left=null;
			return true;
		}else if(this.left.left==null&&this.left.right!=null) {
			this.left.id=this.left.right.id;
			this.left.name=this.left.right.name;
			this.left.right=null;
			return true;
		}
		
		
		
	}
	if(this.right!=null&&this.right.id==id) {
		if(this.right.left==null&&this.right.right==null) {
			this.right=null;
			return true;
		}else if(this.right.left!=null) {
			this.right.id=this.right.left.id;
			this.right.name=this.right.left.name;
			this.right.left=null;
			return true;
		}else if(this.right.left==null&&this.right.right!=null) {
			this.right.id=this.right.right.id;
			this.right.name=this.right.right.name;
			this.right.right=null;
			return true;
		}
	}
	if(this.left!=null) {
		b = this.left.delNode(id);
	}
	if(b) {
		return true;
	}
	if(this.right!=null) {
		b=this.right.delNode(id);
	}
	return b;
}
}

tree

package com.m.demo10;

public class Tree {
	Node root;

	public Tree(Node root) {
		
		this.root = root;
	}
	/遍历
	public void preShowTree() {
		if(root==null) {
			System.out.println("这是个空树");
		}else {
			root.preList();
		}
	}
	
	public void infixShowTree() {
		if(root==null) {
			System.out.println("这是个空树");
		}else {
			root.infixList();
		}
	}
	
	public void postShowTree() {
		if(root==null) {
			System.out.println("这是个空树");
		}else {
			root.postList();
		}
	}
	///前序查找
	public void preFindTree(int id) {
		if(root==null) {
			System.out.println("这是个空树");
		}else {
			Node node = root.preFind(id);
			if(node==null) {
				System.out.printf("没有找到id=%d的节点",id);
				
			}else {
				System.out.println(node);
			}
		}
	}
	
	/中序查找
	public void infixFindTree(int id) {
		if(root==null) {
			System.out.println("这是个空树");
		}else {
			Node node = root.infixFind(id);
			if(node==null) {
				System.out.printf("没有找到id=%d的节点",id);
				
			}else {
				System.out.println(node);
			}
		}
	}
	
	/删除
	public void delNodeTree(int id) {
		if(root==null) {
			System.out.println("这是个空树");
		}else if(root.id==id) {
			root=null;
		}else {
			root.delNode(id);
		}
	}
	删除2
	public void delNodeTree2(int id) {
		if(root==null) {
			System.out.println("这是个空树");
		}
		else if(root.id==id) {
//			root=null;
			if(root.left!=null) {
				root.id=root.left.id;
				root.name=root.left.name;
				root.left=null;
			}else if(root.left==null&&root.right!=null) {
				root.id=root.right.id;
				root.name=root.right.name;
				root.right=null;
			}
		}
		else {
			root.delNode2(id);
		}
	}
	
}

测试类

package com.m.demo10;

public class Test {

	public static void main(String[] args) {
		Node root=new Node(1,"小A");
		Node n2=new Node(2,"小B");
		Node n3=new Node(3,"小C");
		Node n4=new Node(4,"小D");
		Node n5=new Node(5,"小E");
		///手动搭建树结构
		root.left=n2;
		root.right=n3;
		n3.left=n5;
		n3.right=n4;
		///测试遍历
		Tree tree=new Tree(root);
		System.out.print("前序遍历:");
		tree.preShowTree();
//		System.out.print("\n中序遍历:");
//		tree.infixShowTree();
//		System.out.print("\n后序遍历:");
//		tree.postShowTree();
		测试查找
		System.out.println("\n===========");
//		tree.infixFindTree(9);
		
		
		测试删除
//		tree.delNodeTree(3);
//		System.out.print("前序遍历:");
//		tree.preShowTree();
		
//		测试删除
		tree.delNodeTree2(1);
		System.out.print("前序遍历:");
		tree.preShowTree();
		
		
		

	}

}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值