构建查找二叉树

查找二叉树的特点是根节点的左孩子都小于根节点,右孩子都大于根节点,可以联想到二分查找。

构造查找二叉树的Java实现如下:

public class SearchBinaryTree {
    private TreeNode root;
    public class TreeNode{
        private int key;
        private int data;
        private TreeNode leftChild;
        private TreeNode rightChild;
        private TreeNode parent;
        public TreeNode(int key, int data) {
            super();
            this.key = key;
            this.data = data;
            this.leftChild=null;
            this.rightChild=null;
            this.parent=null;
        }

        public int getKey() {
            return key;
        }

        public void setKey(int key) {
            this.key = key;
        }

        public int getData() {
            return data;
        }

        public void setData(int data) {
            this.data = data;
        }

        public TreeNode getLeftChild() {
            return leftChild;
        }

        public void setLeftChild(TreeNode leftChild) {
            this.leftChild = leftChild;
        }

        public TreeNode getRightChild() {
            return rightChild;
        }

        public void setRightChild(TreeNode rightChild) {
            this.rightChild = rightChild;
        }

        public TreeNode getParent() {
            return parent;
        }

        public void setParent(TreeNode parent) {
            this.parent = parent;
        }
    }

    /**
     * 构建查找二叉树
     */
    public TreeNode put(int data){
        TreeNode node=null;
        TreeNode parent=null;
        if (root==null){
            node=new TreeNode(0,data);
            root=node;
            return node;
        }
        node=root;
        while(node!=null){
            parent=node;
            if (data<node.data){
                node=node.leftChild;
            }else if(data>node.data){
                node=node.rightChild;
            }else{
                return node;
            }
        }
        node=new TreeNode(0,data);
        if (data<parent.data){
            parent.leftChild=node;
        }else{
            parent.rightChild=node;
        }
        return node;
    }

    /**
     * 中序遍历查找二叉树判断构建二叉树是否正确
     */
    public void midOrder(TreeNode node){
        if (node==null){
            return ;
        }
        midOrder(node.leftChild);
        System.out.println("当前节点为:" + node.data);
        midOrder(node.rightChild);
    }
    public static void main(String[] args){
        SearchBinaryTree searchBinaryTree=new SearchBinaryTree();
        int[] array=new int[]{50,30,20,44,88,33,87,16,77};
        for (int arr:array) {
            searchBinaryTree.put(arr);
        }
        searchBinaryTree.midOrder(searchBinaryTree.root);
    }
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值