26、二叉树的应用

  • 思路:

写了树节点类、树类和测试类,并写了前中后序遍历和搜索,并写了删除函数。

  • 相关代码
package tree;

public class BinaryTreeDemo {

	
	public static void main(String[] args) {
		
		//1、**************演示前中后序打印结果***************
		// TODO Auto-generated method stub
		BinaryTreeNode binaryTreeNode1 = new BinaryTreeNode(1, "宋江");
		BinaryTreeNode binaryTreeNode2= new BinaryTreeNode(2, "林冲");
		BinaryTreeNode binaryTreeNode3 = new BinaryTreeNode(3, "卢俊义");
		BinaryTreeNode binaryTreeNode4 = new BinaryTreeNode(4, "武大郎");
		BinaryTreeNode binaryTreeNode5 = new BinaryTreeNode(5, "智多星");
		BinaryTreeNode binaryTreeNode6 = new BinaryTreeNode(6, "及时雨");
		
		binaryTreeNode1.setLeftNode(binaryTreeNode2);
		binaryTreeNode1.setRightNode(binaryTreeNode4);
		binaryTreeNode2.setLeftNode(binaryTreeNode3);
		binaryTreeNode4.setLeftNode(binaryTreeNode5);
		binaryTreeNode4.setRightNode(binaryTreeNode6);
		
		BinaryTree binaryTree = new BinaryTree(binaryTreeNode1);//root
//		binaryTree.preOrder();
//		binaryTree.infixOrder();
//		binaryTree.lastOrder();
		//2、**************演示前中后序查询打印结果***************
//		//前
//		BinaryTreeNode binaryTreeNode  = binaryTree.preSearch(4);
//		if(binaryTreeNode != null) {
//			System.out.println(binaryTreeNode.toString());
//		}else {
//			System.out.println("未找到");
//		}
		//中
//		BinaryTreeNode binaryTreeNode = binaryTree.infixSearch(4);
//		if(binaryTreeNode != null) {
//			System.out.println(binaryTreeNode.toString());
//		}else {
//			System.out.println("未找到");
//		}
		//后
//		BinaryTreeNode binaryTreeNode  = binaryTree.lastSearch(4);
//		if(binaryTreeNode != null) {
//			System.out.println(binaryTreeNode.toString());
//		}else {
//			System.out.println("未找到");
//		}
		
		//**************测试删除*****************
		System.out.println("删除前的前序排列");
		binaryTree.preOrder();
		binaryTree.delete(2);
		System.out.println("删除后的前序排列");
		binaryTree.preOrder();

	}
	
	

}
class BinaryTree{
	BinaryTreeNode root; //root
	
	
	public BinaryTree(BinaryTreeNode root) {
		super();
		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.infixOrder();
		}else {
			System.out.println("二叉树为空,不能遍历");
		}
	}
	//后序遍历
	public void lastOrder() {
		if(this.root != null ) {
			this.root.lastOrder();
		}else {
			System.out.println("二叉树为空,不能遍历");
		}
	}
	
	
	//前序搜索
	public BinaryTreeNode preSearch(int no) {
		if(this.root != null) {
			return this.root.preSearch(no);
		}
		return null;
	}
	//中序搜索
	public BinaryTreeNode infixSearch(int no) {
		if(this.root != null) {
			return this.root.infixSearch(no);
		}
		return null;
	}
	//后序搜索
	public BinaryTreeNode lastSearch(int no) {
		if(this.root != null) {
			return this.root.lastSearch(no);
		}
		return null;
	}
	
	//删除
	public void delete(int no) {
		if(this.root == null) {
			System.out.println("二叉树为空,不能删除!");
		}else if(this.root.getNo() == no) {
			this.root = null;
		}else {
			this.root.delete(no);
		}
	}
	
}



//定义二叉树的节点
class BinaryTreeNode{
	private int no;
	private String name;
	private BinaryTreeNode leftNode;//默认为空
	private BinaryTreeNode rightNode;//默认为空
	
	//构造函数
	public BinaryTreeNode(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 BinaryTreeNode getLeftNode() {
		return leftNode;
	}

	public void setLeftNode(BinaryTreeNode leftNode) {
		this.leftNode = leftNode;
	}

	public BinaryTreeNode getRightNode() {
		return rightNode;
	}

	public void setRightNode(BinaryTreeNode rightNode) {
		this.rightNode = rightNode;
	}

	@Override
	public String toString() {
		return "BinaryTreeNode [no=" + no + ", name=" + name + "]";
	}
	
	//前序遍历
	public void preOrder() {
		System.out.println(this.toString());
		if(this.leftNode != null) {
			this.leftNode.preOrder();
		}
		if(this.rightNode != null) {
			this.rightNode.preOrder();
		}
	}
	
	//中序遍历
	public void infixOrder() {
		if(this.leftNode != null) {
			this.leftNode.infixOrder();
		}
		System.out.println(this.toString());
		if(this.rightNode != null) {
			this.rightNode.infixOrder();
		}
	}
	
	//后序遍历
	public void lastOrder() {
		if(this.leftNode != null) {
			this.leftNode.lastOrder();
		}		
		if(this.rightNode != null) {
			this.rightNode.lastOrder();
		}
		System.out.println(this.toString());
	}
	
	
	//前序搜索
	public BinaryTreeNode preSearch(int no) {
		System.out.println("前序遍历~");
		if(this.no == no) {
			return this;
		}
		BinaryTreeNode resBinaryTreeNode = null;
		if(this.leftNode !=null) {
			resBinaryTreeNode = this.leftNode.preSearch(no);
		}
		if(resBinaryTreeNode != null) {
			return resBinaryTreeNode;
		}
		
		if(this.rightNode !=null) {
			resBinaryTreeNode = this.rightNode.preSearch(no);
		}
		return resBinaryTreeNode;	
	}
	
	
	
	
	//中序搜索
	public BinaryTreeNode infixSearch(int no) {
		System.out.println("中序遍历~");
		BinaryTreeNode resBinaryTreeNode = null;
		if(this.leftNode !=null) {
			resBinaryTreeNode = this.leftNode.infixSearch(no);
		}
		if(resBinaryTreeNode != null) {
			return resBinaryTreeNode;
		}
		if(this.no == no) {
			return this;
		}
		
		if(this.rightNode !=null) {
			resBinaryTreeNode = this.rightNode.infixSearch(no);
		}
		return resBinaryTreeNode;	
	}
		
	
	
	//后序搜索
	public BinaryTreeNode lastSearch(int no) {
		BinaryTreeNode resBinaryTreeNode = null;
		if(this.leftNode !=null) {
			resBinaryTreeNode = this.leftNode.lastSearch(no);
		}
		if(resBinaryTreeNode != null) {
			return resBinaryTreeNode;
		}		
		if(this.rightNode !=null) {
			resBinaryTreeNode = this.rightNode.lastSearch(no);
		}
		if(resBinaryTreeNode != null) {
			return resBinaryTreeNode;
		}
		System.out.println("后序遍历~");
		if(this.no == no) {
			return this;
		}else {
			return resBinaryTreeNode;
		}
		
	}
	
	//删除节点
	public void delete(int no) {
		if(this.leftNode != null && this.leftNode.getNo() == no) {
			this.leftNode = null;
			return;
		}
		if(this.rightNode != null && this.rightNode.getNo() == no) {
			this.rightNode = null;
			return;
		}
		if(this.leftNode != null) {
			this.leftNode.delete(no);
		}
		if(this.rightNode != null) {
			this.rightNode.delete(no);
		}
		
		
	}
	
			
	
	
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值