小蠢鱼算法系列之二叉树排序

本文详细介绍了二叉树的定义、结构、创建、遍历及查找算法,并通过实例展示了如何实现这些功能。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

package com.foolfish.tree;
/**
 * @desc 二叉树算法
 * @author foolfish.chen
 *
 */
public class BinaryTree {
	private int nodeValue = 0; // 当前节点值
	private BinaryTree lChild = null;// 左孩子节点
	private BinaryTree rChild = null;// 右孩子节点
	
	public BinaryTree(int node,BinaryTree l, BinaryTree r){
		this.nodeValue = node;
		this.lChild = l;
		this.rChild = r;
	}

	/**
	 * @return the nodeValue
	 */
	public int getNodeValue() {
		return nodeValue;
	}

	/**
	 * @param nodeValue the nodeValue to set
	 */
	public void setNodeValue(int nodeValue) {
		this.nodeValue = nodeValue;
	}

	/**
	 * @return the lChild
	 */
	public BinaryTree getLChild() {
		return lChild;
	}

	/**
	 * @param child the lChild to set
	 */
	public void setLChild(BinaryTree child) {
		lChild = child;
	}

	/**
	 * @return the rChild
	 */
	public BinaryTree getRChild() {
		return rChild;
	}

	/**
	 * @param child the rChild to set
	 */
	public void setRChild(BinaryTree child) {
		rChild = child;
	}
	/**
	 * @desc 构造二叉树
	 * @param node
	 * @param nodeValue
	 */
	public void createTree(BinaryTree node,int nodeValue){
		/*********************************
		 * 判断当前值是否小于当前节点,若小于当前
		 * 节点,继续往左子树遍历,直到遍历到叶子
		 * 节点,又子树同理
		 *********************************/
		if(node.getNodeValue()>nodeValue){
			if(node.getLChild() != null){
				node.createTree(node.lChild, nodeValue);
			}else{
				node.setLChild(new BinaryTree(nodeValue,null,null));
			}
		}else{
			if(node.getRChild() != null){
				node.createTree(node.rChild, nodeValue);
			}else{
				node.setRChild(new BinaryTree(nodeValue,null,null));
			}
		}
	}
	/**
	 * @desc 打印从指定节点开始的树结构
	 * @param node
	 */
	public void printAll(BinaryTree node){
		// 从左边开始遍历
		if(node.getLChild() != null){
			printAll(node.getLChild());
		}
		System.out.print(node.getNodeValue()+",");
		// 从左边开始遍历
		if(node.getRChild() != null){
			printAll(node.getRChild());
		}
	}
	/**
	 * @desc 查找指定直
	 * @param node 节点信息
	 * @param Value 需要查找的值
	 */
	public int search(BinaryTree node,int Value){
		int returnValue = 0;
		/******************************
		 * 若当前节点大于要查找的值,查找左树
		 * 若当前节点小于要查找的值,查找右树
		 * 若当前节点等于要查找的值,放回当前值
		 ******************************/
		if(node.getNodeValue()>Value){
			returnValue = search(node.getLChild(), Value);
		}else if(node.getNodeValue()<Value){
			returnValue = search(node.getRChild(), Value);
		}else{
			return  node.getNodeValue();
		}
		return returnValue;
	}
	/**
	 * @desc main App
	 * @param args
	 */
	public static void main(String[] args){
		int[] array = {14,3,67,23,1,7,8,25,78,11,46,9};
		BinaryTree btree = new BinaryTree(array[0],null,null);
		for(int i = 1 ; i<array.length ; i++){
			btree.createTree(btree, array[i]);
		}
		/*********************************
		 * 打印排序后的值
		 *********************************/
		btree.printAll(btree);
		/*********************************
		 * 查找指定值
		 *********************************/
		System.out.println();
		System.out.println(btree.search(btree, 3));
	}
}
 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值