首先编写节点类
public class BinaryTreeNode //创建节点
{
private int id;
private String name;
private BinaryTreeNode left; //左节点索引 默认为空
private BinaryTreeNode right; //右节点索引 默认为空
public BinaryTreeNode(int id, String name) {
this.id = id;
this.name = name;
}
编写四个属性的get和set方法,此处略
public String toString() {
return "BinaryTreeNode{" +
"id=" + id +
", name='" + name + '\'' +
'}';
}
//节点的查找:不断递归,所有的比较都是回溯到某一节点没有子节点时,此时它就是this节点
//然后比较this.id == no,返回
public BinaryTreeNode froNodeResearch(int no) //前序查找某一节点(根左右)
{
System.out.println("进入前序遍历");
BinaryTreeNode resNode = null;
if (this.id == no) //先比较当前节点
return this;
if (this.left != null) //当前节点没找到的话,向左子数遍历递归查找
{
resNode = this.left.froNodeResearch(no);
if (resNode != null) //若找到,直接返回,不再继续查找
return resNode;
}
if (this.right != null) //左子树没有找到的话,像右子树遍历递归查找
{
resNode = this.right.froNodeResearch(no);
}
return resNode; //最后不管有没有找到都要返回,没找到返回null
}
public BinaryTreeNode midNodeResearch(int no) //中序查找某一节点(左根右)
{
BinaryTreeNode resnode = null;
if (this.left != null) {
resnode = this.left.midNodeResearch(no);
if (resnode != null)
return resnode;
}
System.out.println("进入中序遍历");
if (this.id == no) //当调用到某一节点没有左子节点时,此时比较这一节点
return this;
if (this.right != null)
resnode = this.right.midNodeResearch(no);
return resnode;
}
public BinaryTreeNode endNodeResearch(int no) //后序查找某一节点(左右根)
{
BinaryTreeNode resnode = null;
if (this.left != null) {
resnode = this.left.endNodeResearch(no);
if (resnode != null)
return resnode;
}
if (this.right != null)
{
resnode = this.right.endNodeResearch(no);
if (resnode != null)
return resnode;
}
System.out.println("进入后序遍历");
if (this.id == no) //左右子树都没有找到时
return this;
else return resnode;
}
public void deBinaryNode(int no) //节点的删除
{
if(this.left!=null) //当前节点的左子节点是否为待删节点,是,则置空,立即返回,不再判断
{
if (this.left.id == no)
{
this.left = null;
return;
}
}
if(this.right!=null) //当前节点的右子节点是否为待删节点,是,则置空
{
if(this.right.id==no)
{
this.right = null;
return;
}
}
if(this.left!=null) //以上都没有,像左子树递归删除
this.left.deBinaryNode(no);
if(this.right!=null) //向右子树递归删除
this.right.deBinaryNode(no);
}
}
编写二叉树类,调用节点的方法
public class BinaryTree {
private BinaryTreeNode root; //是二叉树的根节点,既是二叉树的属性,也是节点对象
public void set(BinaryTreeNode root)
{
this.root=root;
}
/二叉树的遍历查找
public BinaryTreeNode froOrderResearch(int no) //前序遍历查找
{
if(this.root!=null)
return this.root.froNodeResearch(no);
else
return null;
}
public BinaryTreeNode midOrderResearch(int no) //中序遍历查找
{
if(this.root!=null)
return this.root.midNodeResearch(no);
return null;
}
public BinaryTreeNode endOrderResearch(int no) //后序遍历查找
{
if(this.root!=null)
return this.root.endNodeResearch(no);
return null;
}
public void deBinaryTree(int no) //二叉树的删除,若待删节点是叶子节点,则删除叶子节点
{ //若待删节点是父节点,则删除父节点所在的内棵树
if(this.root!=null)
{
if(this.root.getId()==no)
{
this.root=null;
}
else this.root.deBinaryNode(no);
}
else System.out.println("这是一个空树");
}
测试类如下
public class BinaryTreeTest {
public static void main(String[] args) {
/手动创建节点,构建二叉树
BinaryTreeNode root=new BinaryTreeNode(1,"宋江");
BinaryTreeNode node2=new BinaryTreeNode(2,"吴用");
BinaryTreeNode node3=new BinaryTreeNode(3,"卢俊义");
BinaryTreeNode node4=new BinaryTreeNode(4,"林冲");
BinaryTreeNode node5=new BinaryTreeNode(5,"关胜");
root.setLeft(node2);
root.setRight(node3);
node3.setRight(node4);
node3.setLeft(node5);
///进行测试
BinaryTree bianrytree=new BinaryTree();
bianrytree.set(root);
BinaryTreeNode fronode=bianrytree.froOrderResearch(5); //前序遍历查找测试
if(fronode!=null) {
System.out.println("找到id为5的节点");
System.out.println(fronode);
}
else System.out.println("没有找到");
System.out.println();
BinaryTreeNode midnode=bianrytree.midOrderResearch(5); //中序遍历查找测试
if(midnode!=null) {
System.out.println("找到id为5的节点");
System.out.println(midnode);
}
else System.out.println("没有找到");
System.out.println();
BinaryTreeNode endnode=bianrytree.endOrderResearch(5); //后序遍历查找测试
if(endnode!=null) {
System.out.println("找到id为5的节点");
System.out.println(endnode);
}
else System.out.println("没有找到");*/
//节点删除
System.out.println("删除前");
bianrytree.froOrder();
bianrytree.deBinaryTree(3);
System.out.println("删除后");
bianrytree.froOrder();