java 二叉树创建、遍历、查找和删除

树的节点类:

public class TreeNode {
	int value;  //树的权值
	TreeNode leftNode; //左孩子
	TreeNode rightNode; //右孩子

	public TreeNode(int value){
		this.value = value;
	}
		
	public void setLeftNode(TreeNode leftNode) {
		this.leftNode = leftNode;
	}
	
	public TreeNode getLeftNode() {
		return leftNode;
	}
	
	public void setRightNode(TreeNode rightNode) {
		this.rightNode = rightNode;
	}
	
	public TreeNode getRightNode() {
		return rightNode;
	}
	
	//前序遍历  (将遍历写在节点类中是为了方便特定的子树,不必每次遍历整棵树)
	public void frontShow(){
		//先遍历当前节点的内容
		System.out.print(value + " ");
		//遍历左节点
		if(leftNode!=null){
			leftNode.frontShow();
		}
		//遍历右节点
		if(rightNode!=null){
			rightNode.frontShow();
		}
	}
	
	//中序遍历
	public void midShow(){
		//遍历左节点
		if(leftNode != null){
			leftNode.midShow();
		}
		System.out.print(value + " ");
		//遍历右节点
		if(rightNode != null){
			rightNode.midShow();
		}
	}

	//后序遍历
	public void afterShow() {
		//遍历左节点
		if(leftNode != null){
			leftNode.afterShow();
		}
		//遍历右节点
		if(rightNode != null){
			rightNode.afterShow();
		}
		System.out.print(value + " ");
	}

	//前序查找
	public TreeNode frontSearch(int m) {
		if(value == m){
			return this;
		}
		TreeNode temp = null;
		//查找左节点
		if(leftNode != null){
			temp = leftNode.frontSearch(m);
		}
		if(temp!=null) 
			return temp;
		//查找右节点
		if(rightNode != null){
			temp = rightNode.frontSearch(m);
		}
		if(temp!=null)
			return temp;
		
		return null;
	}

	//删除子树(根的value值是m的子树都删除)
	public void delete(int m) {
		TreeNode parent = this;
		//检查左节点
		if(parent.leftNode!=null && parent.leftNode.value==m){
			parent.leftNode = null;
			return ;
		}
		
		//检查左子树
		if(parent.leftNode != null){
			parent.leftNode.delete(m);
		}
		
		//检查右节点
		if(parent.rightNode!=null && parent.rightNode.value==m){
			parent.rightNode = null;
			return ;
		}
		
		//检查右子树
		if(parent.rightNode != null){
			parent.rightNode.delete(m);
		}
	}
}

说明:把遍历和查找操作都放入TreeNode类时为了方便遍历某一颗子树或者仅在某一颗子树中进行查找。


二叉树的实现:

public class BinaryTree {
	TreeNode root; //根节点

	public BinaryTree(TreeNode root) {
		this.root = root;
	}
	
	public BinaryTree() {
	}
	
	public void setRoot(TreeNode root) {
		this.root = root;
	}
	
	public TreeNode getRoot() {
		return root;
	}

	//前序遍历
	public void frontShow() {
		if(root != null){
			root.frontShow();
			System.out.println();
		}
	}

	//中序遍历
	public void midShow() {
		if(root != null){
			root.midShow();
			System.out.println();
		}
	}

	//后序遍历
	public void afterShow() {
		if(root != null){
			root.afterShow();
			System.out.println();
		}
	}

	//前序查找
	public TreeNode frontSearch(int i) {
		return root.frontSearch(i);
	}

	//删除子树
	public void delete(int i) {
		if(root.value==i){
			root = null;
			return ;
		}
		root.delete(i);
	}
}


测试代码:

/*
 * 测试二叉树的创建、遍历、查找和删除功能
 */
public class TestBinaryTree {
	public static void main(String[] args){
		//创建一颗树
		BinaryTree binTree = new BinaryTree();
		//创建一个根节点
		TreeNode root  = new TreeNode(1);
		binTree.setRoot(root);
		//创建一个左节点
		TreeNode rootL = new TreeNode(2);
		//将左节点添加到根节点的左孩子
		root.setLeftNode(rootL);
		//创建一个右节点
		TreeNode rootR = new TreeNode(3);
		//将右节点添加到根节点的右孩子
		root.setRightNode(rootR);
		
		//为第二层的左节点添加两个节点
		rootL.setLeftNode(new TreeNode(4));
		rootL.setRightNode(new TreeNode(5));
		//为第二层的右节点添加两个节点
		rootR.setLeftNode(new TreeNode(6));
		rootR.setRightNode(new TreeNode(7));
		
		//前序遍历二叉树
		binTree.frontShow();  
		//中序遍历二叉树
		binTree.midShow();
		//后序遍历二叉树
		binTree.afterShow();
		
		System.out.println("-------------------------------------------");
		
		//前序查找
		TreeNode result = binTree.frontSearch(3);
		System.out.println(result);
		System.out.println(result==rootR);
		
		System.out.println("-------------------------------------------");
		
		//删除子树
		binTree.delete(2);
		binTree.frontShow();
	}
}

顺序存储二叉树的实现:

  按照层次遍历的顺序将二叉树的元素存储在数组中;形成顺序存储的二叉树。需要注意的是:当树的节点为空时也需要存储;以方便通过数组下标计算查找指定节点的孩子节点和父节点。

/*
 * 顺序存储的二叉树,用数组存储一颗二叉树
 * 或者根据数组构建一颗二叉树
 */
public class ArrayBinaryTree {
	int[] data;

	public ArrayBinaryTree(int[] data) {
		super();
		this.data = data;
	}
	
	public void frontShow(){
		frontShow(0);
	}

	//前序遍历二叉树
	private void frontShow(int index) {
		if(data==null || data.length<=0){
			return ;
		}
		
		if(data[index] ==0){//表示空子树
			return ;
		}
		System.out.print(data[index] + " ");
		
		//遍历左子树
		if( (2*index+1)<data.length ){
			frontShow(2*index+1);
		}
		
		//遍历右子树
		if( (2*index+2)<data.length ){
			frontShow(2*index+2);
		}
	}
	
	public static void main(String[] args)	{
		int[] arr = {1,2,3,4,5,6,7};
		ArrayBinaryTree tree = new ArrayBinaryTree(arr);
		tree.frontShow();
	}
}

猜你喜欢

转载自blog.csdn.net/HC199854/article/details/105858562