使用递归方式实现的二叉树

/*
 * 文 件 名:  Node.java
 * 修改时间:  2012-11-27
 */
package tree;

/**
 * 二叉树的节点
 * @version  [版本号, 2012-11-27]
 */
public class Node
{
    int iData;
    double dData;
    Node leftChild;
    Node rightChild;
    
    /**
     * <默认构造函数>
     */
    public Node(int iData, double dData)
    {
        this.iData = iData;
        this.dData = dData;
        this.leftChild = null;
        this.rightChild = null;
    }
    
    /**
     * 显示节点数据
     * @see [类、类#方法、类#成员]
     */
    public void displayNode()
    {
        System.out.println("{" + iData + "," + dData + "}");
    }
}
/*
 * 文 件 名:  BinaryTree.java
 * 修改时间:  2012-11-26
 */
package tree;

/**
 * 二叉树
 * @version  [版本号, 2012-11-26]
 */
public class BinaryTree
{
    //二叉树的根节点的引用
    private Node root;
    
    /**
     * <默认构造函数>
     */
    public BinaryTree()
    {
        root = null;
    }
    
    /**
     * 判断树是否为空
     * @return
     * @see [类、类#方法、类#成员]
     */
    public boolean isEmpty()
    {
        return root == null;
    }
    
    /**
     * 根据指定关键字查找节点
     * @param iData
     * @return
     * @see [类、类#方法、类#成员]
     */
    public Node find(int iData)
    {
        return find(iData, root);
    }
    
    /**
     * 使用递归方式查找节点
     * @param iData
     * @param root
     * @return
     * @see [类、类#方法、类#成员]
     */
    private Node find(int iData, Node root)
    {
        if (root == null)
        {
            return null;
        }
        else if (root.iData == iData)
        {
            return root;
        }
        else if (iData < root.iData)
        {
            return find(iData, root.leftChild);
        }
        else
        {
            return find(iData, root.rightChild);
        }
    }
    
    /**
     * 插入节点
     * @param iData
     * @param dData
     * @see [类、类#方法、类#成员]
     */
    public void insert(int iData, double dData)
    {
        Node node = new Node(iData, dData);
        if (isEmpty())
        {
            root = node;
        }
        else
        {
            insert(node, root);
        }
    }
    
    /**
     * 使用递归方式插入节点
     * @param node
     * @param root 子树的根节点,不可为空
     * @see [类、类#方法、类#成员]
     */
    private void insert(Node node, Node root)
    {
        if (node.iData < root.iData)
        {
            if (root.leftChild == null)
            {
                root.leftChild = node;
                return;
            }
            else
            {
                insert(node, root.leftChild);
            }
        }
        else
        {
            if (root.rightChild == null)
            {
                root.rightChild = node;
                return;
            }
            else
            {
                insert(node, root.rightChild);
            }
        }
    }
    
    /**
     * 根据指定关键字删除节点
     * @param iData
     * @see [类、类#方法、类#成员]
     */
    public void delete(int iData)
    {
        
    }
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值