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));
}
}
分享到:
相关推荐
二叉树排序二叉树排序二叉树排序二叉树排序二叉树排序二叉树排序二叉树排序
二叉树排序算法及 demo
java 常见排序算法的实现 有冒泡、选择、快速、比较等常见的排序算法 还包括二叉树的实现
堆排序 二叉树排序 关键路径 Dijkstra算法,C语言,已实现
CPU:T6670 2.20GHZ 内存:2G 系统:WIN7 用VB调用VC编写的二叉树排序DLL排序,10万个数排序只需要0.3秒,100万个数排序只需要5.3秒左右
编写递归算法,计算二叉树中叶子结点的数目
二叉树的递归算法:建立二叉树、遍历二叉树.doc 多多指教
编写算法判别给定二叉树是否为完全二叉树,用递归实现
遇到算法问题,去找博客?去翻自己之前写过的几千行代码? 我整理好了xmind,分享给大家。html格式的,导入xmind就行。
一、目的 用二叉树排序 二、实验内容 二叉树排序 三、实验使用环境 C++
运用xml相关技术,实现二叉树的排序。先输入一组数字,排序之后插入到数据库,最后通过xml导出。
很好用的算法介绍 而且是有详细解析的哟 很多的内容都是值得反复推敲和思考的
二叉树遍历算法二叉树遍历算法二叉树遍历算法二叉树遍历算法二叉树遍历算法二叉树遍历算法
用二叉树先序遍历算法创建一组数据构成的二叉树排序,然后用二叉树中序遍历算法实现数据排序输出。
数据结构与算法之二叉树 资源源于不但搜索,自由源于不但努力
写一算法,判断一棵二叉树是否是一棵二叉排序树。
(3)中序遍历二叉树:非递归算法(最好也能实现先序,后序非递归算法)。 (4)求二叉树的高度 。 (5)求二叉树的叶子个数。 (6)对于树中每一个元素值为x的结点,删去以它为根的子树,并释放相应的空间。 (8...
使用二叉树算法写的排序功能,可以对任意数据进行排序。含源代码及示例。
二叉树的递归算法:建立二叉树、遍历二叉树