树的种类很多,有二叉树、平衡二叉树、B树、B+树、哈夫曼树、B-树,B*树、红黑树、trie树等。
二叉树(有限个节点的集合)的基本定义如下: 这个集合可以是空集,也可以是一个根节点和两棵不相交的子二叉树组成的集合,其中一棵树叫作根的左子树,另一棵树叫作根的右子树。
二叉树的代码实现如下:
package com.threetop.www;
/**
* 二叉树节点的定义:二叉树的左右链表示法
* @author wjgs
*
*/
public class BinaryTreeNode {
private int data; //数据
private BinaryTreeNode LeftChild; //左孩子
private BinaryTreeNode RightChild; //右孩子
public int getData()
{
return data;
}
public void setData(int data)
{
this.data=data;
}
public BinaryTreeNode getLeftChild()
{
return LeftChild;
}
public void setLeftChild(BinaryTreeNode left)
{
this.LeftChild=left;
}
public BinaryTreeNode getRightChild()
{
return RightChild;
}
public void setRightChild(BinaryTreeNode right)
{
this.RightChild=right;
}
}
package com.threetop.www;
/**
* 二叉树的实现
* @author wjgs
*
*/
public class BinaryTree {
//********************二叉树的创建*********************
private BinaryTreeNode root; //根节点
//构造函数实现初始化
public BinaryTree()
{
}
//构造函数重载
public BinaryTree(BinaryTreeNode root){
this.root=root;
}
public void setRoot(BinaryTreeNode root){
this.root=root;
}
public BinaryTreeNode getRoot()
{
return this.root;
}
//***********************************************************
//*************************二叉树的清空**************************
/**
* 清除某个子树的所有节点
* @param node
*/
public void clearNode(BinaryTreeNode node){
if(node!=null)
{
clearNode(node.getLeftChild()); //递归的删除左子节点
clearNode(node.getRightChild()); //递归的删除右子节点
node=null;
}
}
/**
* 清空树
*/
public void clear()
{
clearNode(root);
}
//***********************************************************
//******************************判断二叉树是否为空*****************************
public boolean isEmpty()
{
return root==null;
}
//***********************************************************
//***************************求二叉树的高度********************************
public int height()
{
return height(root);
}
public int height(BinaryTreeNode node)
{
if(node==null)
{
return 0;
}
else{
//递归获取左子树的高度
int l=height(node.getLeftChild());
//递归获取右子树的高度
int r=height(node.getRightChild());
return l>r?l+1:r+1;
}
}
//***********************************************************
//****************************求二叉树的节点数*******************************
/**
* 获取二叉树的节点数
* @return
*/
public int size()
{
return size(root);
}
public int size(BinaryTreeNode node){
if(node==null)
{
return 0;
}
else
{
//递归获取左子树和右子树的节点数,最终相加+1
return 1+size(node.getLeftChild())+size(node.getRightChild());
}
}
//***********************************************************
//******************************返回某个节点的父节点*****************************
public BinaryTreeNode getParent(BinaryTreeNode node)
{
return (root==null||node==root)?null:getParent(root,node);
}
public BinaryTreeNode getParent(BinaryTreeNode subTree,BinaryTreeNode node)
{
if(subTree==null)
{
//如果子树为空
return null;
}
if(subTree.getLeftChild()==node||subTree.getRightChild()==node)
{
return subTree;
}
BinaryTreeNode parent=null;
//先递归查找左子树
if((parent=getParent(subTree.getLeftChild(),node))!=null)
{
return parent;
}
else
{
//递归地在右子树上查找
return getParent(subTree.getRightChild(),node);
}
}
//***********************************************************
//*****************************返回左右子树******************************
/**
* 获取某个节点的左子树
* @param node
* @return
*/
public BinaryTreeNode getLeftTree(BinaryTreeNode node)
{
return node.getLeftChild();
}
/**
* 获取某个节点的右子树
* @param node
* @return
*/
public BinaryTreeNode getRightTree(BinaryTreeNode node)
{
return node.getRightChild();
}
//***********************************************************
//***************************二叉树的插入********************************
/**
* 给某个节点插入左子节点
* @param parent
* @param newNode
*/
public void insertLeft(BinaryTreeNode parent,BinaryTreeNode newNode)
{
parent.setRightChild(newNode);
}
/**
* 给某个节点插入右子节点
* @param parent
* @param newNode
*/
public void insertRight(BinaryTreeNode parent,BinaryTreeNode newNode)
{
parent.setRightChild(newNode);
}
//***********************************************************
//***************************二叉树的遍历********************************
/**
* 先序遍历
* @param node
*/
public void preOrder(BinaryTreeNode node)
{
if(node!=null)
{
visited(node);
preOrder(node.getLeftChild());
preOrder(node.getRightChild());
}
}
/**
* 中序遍历
* @param node
*/
public void inOrder(BinaryTreeNode node)
{
if(node!=null)
{
preOrder(node.getLeftChild());
visited(node);
preOrder(node.getRightChild());
}
}
/**
* 后序遍历
* @param node
*/
public void postOrder(BinaryTreeNode node)
{
if(node!=null)
{
preOrder(node.getLeftChild());
preOrder(node.getRightChild());
visited(node);
}
}
//输出节点的数据
private void visited(BinaryTreeNode node) {
// TODO Auto-generated method stub
System.out.print(" "+node.getData());
}
}