二叉树基础操作 ,前中后序遍历,求二叉树高度,二叉搜索树(二叉排序树)Java实现 代码集合

版权声明:欢迎转载 https://blog.csdn.net/thezprogram/article/details/83241350

首先,定义一个树类Tree.java

public class Tree {
	public TreeNode root;
}

定义树节点类TreeNode.java

public class TreeNode {
	
	public TreeNode(int data) {
		this.data = data;
	}
	public int data;
	public TreeNode left;
	public TreeNode right;
	public void showData(){
		System.out.println(data);//节点访问操作
	}
}

二叉树的基本操作类 TreeUtils.java,
实现了增加树节点,删除树节点,搜索节点
生成二叉搜索树(左子树总小于右子树,中序遍历有序)

public class TreeUtils {
	public static void addNode(Tree t,TreeNode node){//将一个节点增加到已有二叉树(节点按二叉搜索树规则增加)
		if(t.root==null){
			t.root = node;
		}else{
			TreeNode current = t.root;//开始
			while(true){
				if(node.data<current.data){
					if(current.left==null){
						current.left = node;
						return;
					}else{
						current = current.left;
						continue;
					}
				}else{
					if(current.right==null){
						current.right = node;
						return;
					}else{
						current = current.right;
						continue;
					}
				}
			}
		}
	}
	
	public static void preOrderRecursive(TreeNode node){//前序遍历
		if(node!=null){
			node.showData();
			preOrderRecursive(node.left);
			preOrderRecursive(node.right);
		}
	}
	
	public static void inOrderRecursive(TreeNode node){//中序遍历
		if(node!=null){
			inOrderRecursive(node.left);
			node.showData();
			inOrderRecursive(node.right);
		}
	}

	public static void postOrderRecursive(TreeNode node){//后序遍历
		if(node!=null){
			postOrderRecursive(node.left);
			postOrderRecursive(node.right);
			node.showData();
		}
	}

	
	public static TreeNode search(Tree t,int data){//搜索节点
		if(t.root==null){
			return null;
		}
		TreeNode current = t.root;
		while(data!=current.data){
			if(data<current.data){
				current = current.left;
				if(current!=null){
					current.showData();
				}
			}else{
				current = current.right;
				if(current!=null){
					current.showData();
				}
			}
			if(current ==null){
				return null;
			}
		}
		current.showData();
		return current;
	}
	
	public static boolean delete(Tree tree ,int data){//删除节点
		
		TreeNode current=tree.root;
		TreeNode parent = current;
		boolean isLeftChild = true;
		while(current.data!=data){
			parent = current;
			if(data<current.data){
				current = current.left;
				isLeftChild = true;
			}else{
				current = current.right;
				isLeftChild = false;
			}
			if(current==null){
				return false;
			}
		}
		if(current.left==null&&current.right==null){//没有子节点的情况
			if(current==tree.root){
				tree.root=null;
			}
			if(isLeftChild){
				parent.left=null;
			}else{
				parent.right=null;
			}
		}else if(current.left==null){//只有右子树的情况
			if(current==tree.root){
				tree.root = current.right;
			}else if(isLeftChild){
				parent.left = current.right;
			}else{
				parent.right = current.right;
			}
		}else if(current.right==null){//只有左子树的情况
			if(current==tree.root){
				tree.root = current.left;
			}else if(isLeftChild){
				parent.left = current.left;
			}else{
				parent.right= current.left;
			}
		}else{//左右子树均有的情况
			
		}
		return false;
	}

	
	public static Tree getRandomTree(Tree t,int nodeNumber){//根据根节点生成一个指定节点数量的二叉搜索树
		if(t.root==null){
			throw new IllegalArgumentException("树至少有一个节点");
		}
		for(int i=0;i<nodeNumber;i++){
			int random = (int) (10*(Math.random()+Math.random()));
			TreeUtils.addNode(t, new TreeNode(random));
			System.out.println("random: "+random);
		}
		return t;
	}
}

计算二叉树高度测试 BinaryTreeHeightTest.java

public class BinaryTreeHeightTest {
	
	public static void main(String[] args) {
		Tree tree = new Tree();
		TreeNode root = new TreeNode(9);
		tree.root = root;
		TreeUtils.getRandomTree(tree, 10);
		System.out.println(getBTreeHeight(tree.root));
	}

	
	static int getBTreeHeight(TreeNode root){
		int leftHeight = 0;
		int rightHeight = 0;
		if(root.left==null&&root.right==null){
			return 1;
		}
		if(root.left!=null){
			leftHeight = getBTreeHeight(root.left);
		}
		if(root.right!=null){
			rightHeight = getBTreeHeight(root.right);
		}
		return 1+Math.max(leftHeight, rightHeight);
	}
}

二叉树遍历测试 BinaryTreeTraverse.java

public class BinaryTreeTraverse {
	
	public static void main(String[] args) {
		TreeNode root = new TreeNode(90);
		Tree tree = new Tree();
		tree.root = root;
		TreeUtils.getRandomTree(tree, 20);
		if(TreeUtils.search(tree, 1)==null){//搜索节点测试
			System.out.println("null");
		}
		System.out.println("-----search end-----");

//		TreeUtils.preOrderRecursive(tree.root);//先序遍历
		TreeUtils.inOrderRecursive(tree.root);// 中序遍历,二叉搜索树打印出会是有序的
//		TreeUtils.postOrderRecursive(tree.root);//后序遍历
	}
	
}

猜你喜欢

转载自blog.csdn.net/thezprogram/article/details/83241350