二叉查找树的构建及遍历

*二叉树:由一个称为根的元素和两棵不同的子二叉树组成。
二叉查找树(没有重复元素)特点:每一个结点左子树中的值都小于该结点的值,右子树中结点的值都大于该结点的值。
完全二叉树:二叉树除了最后一层以外的每一层都是满的,而且最后一层的叶子都是在左边。*

二叉树结点的定义
class TreeNode{
Object element;
TreeNode left;
TreeNode right;
public TreeNode(Object o){
element=o;
}
}

向二叉树中插入元素
若为空,使用新元素创建一个根结点,否则,查找新元素的父结点,然后比较与父结点的大小,选择插入左子树或右子树。

public boolean insert(Object o){
        if(root == null){
            root = new TreeNode(o);
        }
        else {
            TreeNode parent = null;
            TreeNode current = root;
            while(current != null)
                if(((Comparable)o).compareTo(current.element) < 0){
                    parent = current;
                    current = current.left;
                }
                else if (((Comparable)o).compareTo(current.element) > 0){
                    parent = current;
                    current = current.right;
                }
                else
                    return false;

                if(((Comparable)o).compareTo(parent.element) < 0)
                    parent.left = new TreeNode(o);
                else
                    parent.right = new TreeNode(o);

        }
        size++;
        return true;
    }

二叉树的遍历(递归)

 /*中序遍历*/
    public void inorder(){
        inorder(root);
    }
    private void inorder(TreeNode root){
        if(root == null) return;
        inorder(root.left);
        System.out.print(root.element + " ");
        inorder(root.right);
    }
    /*后序遍历*/
    public void postorder(){
        postorder(root);
    }
    public void postorder(TreeNode root){
        if(root == null) return;
        inorder(root.left);
        inorder(root.right);
        System.out.print(root.element + " ");
    }
    /*前序遍历*/
    public void preorder(){
        postorder(root);
    }
    public void preorder(TreeNode root){
        if(root == null) return;
        inorder(root.left);
        inorder(root.right);
        System.out.print(root.element + " ");
    }
    /*查找指定元素*/
    public Boolean search(Object o){
        TreeNode current=root;
        if(current==null) return false;
        while(current!=null){
            if((((Comparable)o).compareTo(current.element)==0)) return true;
            if(((Comparable)o).compareTo(current.element)>0){
                current=current.right;
            }
            else{
                current=current.left;
            }
        }
        return false;
    }

完整代码


public class BinaryTree {
    private TreeNode root;
    private int size = 0;

    public BinaryTree(){}
    /*构造函数*/
    public BinaryTree(Object[] objects){
        for(int i = 0;i< objects.length; i++)
            insert(objects[i]);
    }
    /*构建二叉树*/
    public boolean insert(Object o){
        if(root == null){
            root = new TreeNode(o);
        }
        else {
            TreeNode parent = null;
            TreeNode current = root;
            while(current != null)
                if(((Comparable)o).compareTo(current.element) < 0){
                    parent = current;
                    current = current.left;
                }
                else if (((Comparable)o).compareTo(current.element) > 0){
                    parent = current;
                    current = current.right;
                }
                else
                    return false;

                if(((Comparable)o).compareTo(parent.element) < 0)
                    parent.left = new TreeNode(o);
                else
                    parent.right = new TreeNode(o);

        }
        size++;
        return true;
    }
    /*中序遍历*/
    public void inorder(){
        inorder(root);
    }
    private void inorder(TreeNode root){
        if(root == null) return;
        inorder(root.left);
        System.out.print(root.element + " ");
        inorder(root.right);
    }
    /*后序遍历*/
    public void postorder(){
        postorder(root);
    }
    public void postorder(TreeNode root){
        if(root == null) return;
        inorder(root.left);
        inorder(root.right);
        System.out.print(root.element + " ");
    }
    /*前序遍历*/
    public void preorder(){
        postorder(root);
    }
    public void preorder(TreeNode root){
        if(root == null) return;
        inorder(root.left);
        inorder(root.right);
        System.out.print(root.element + " ");
    }
    /*获取树的长度*/
    public int getSize(){
        return size;
    }
    /*查找元素*/
    public Boolean search(Object o){
        TreeNode current=root;
        if(current==null) return false;
        while(current!=null){
            if((((Comparable)o).compareTo(current.element)==0)) return true;
            if(((Comparable)o).compareTo(current.element)>0){
                current=current.right;
            }
            else{
                current=current.left;
            }
        }
        return false;
    }
    /*定义树结点*/
    private static class TreeNode{
        Object element;
        TreeNode left;
        TreeNode right;
        public TreeNode(Object o){
           element=o;
        }
    }
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值