Java实现二叉树 增 删 查

二叉树的定义
树(tree)是一种抽象数据类型(ADT),用来模拟具有树状结构性质的数据集合。它是由n(n>0)个有限节点通过连接它们的边组成一个具有层次关系的集合。把它叫做“树”是因为它看起来像一棵倒挂的树,也就是说它是根朝上,而叶朝下的树。

二叉树是一种特殊的树,每个节点最多只能有两个子节点。(如图1.1)

二叉搜索树要求:若它的左子树不空,则左子树上所有结点的值均小于它的根结点的值; 若它的右子树不空,则右子树上所有结点的值均大于它的根结点的值; 它的左、右子树也分别为二叉排序树
在这里插入图片描述

/**
 * 二叉树
 */
public class BinartTree {

    private Node root;

    static class Node {
        private Integer id;
        private String value;
        private Node leftChild;
        private Node rightChild;

        public Node(Integer id, String value) {
            this.id = id;
            this.value = value;
        }
    }
    
    //查找
    public Node find(Integer id) {
        if (root.id == id) {
            return root;
        }
        Node current = root;
        while (true) {
            if (current.id < id) {
                current = current.leftChild;
                if (current == null) {
                    return null;
                }
                if (id == current.id) {
                    return current;
                }
            } else {
                current = current.rightChild;
                if (current == null) {
                    return null;
                }
                if (id == current.id) {
                    return current;
                }
            }
        }
    }


    //添加
    public void insert(Node node) {
        if (this.root == null) {
            root = node;
            return;
        }
        Node current = root;
        while (true) {
            if (node.id > current.id) {
                if (current.leftChild == null) {
                    current.leftChild = node;
                    break;
                }
                current = current.leftChild;
                if (current == null)
                    break;
            } else {

                if (current.rightChild == null) {
                    current.rightChild = node;
                    break;
                }
                current = current.rightChild;
                if (current == null)
                    break;
            }
        }
    }


    //删除
    public void delete(Integer id) {
        if (root.id == id) {
            root = null;
        }
        Node current = root;
        while (true) {
            if (current.id < id) {
                if (current.leftChild == null) {
                    break;
                }
                if (id == current.leftChild.id) {
                    current.leftChild = null;
                    break;
                }
                current = current.leftChild;
            } else {
                if (current.rightChild == null) {
                    break;
                }
                if (id == current.rightChild.id) {
                    current = null;
                    break;
                }
                current = current.rightChild;
            }
        }
    }


    public static void main(String[] args) {
        BinartTree t = new BinartTree();

        t.insert(new Node(3, "root"));
        t.insert(new Node(4, "left1"));
        t.insert(new Node(2, "right1"));
        t.insert(new Node(5, "left2"));
        t.insert(new Node(1, "right2"));

        Node node = t.find(5);

        t.delete(5);
        System.out.println(t);
    }
}

同意的思路去实现一个菜单树

@Data
@ToString
public class MenuTree {

    //当父id为null的时候为根节点
    private Menu root;

    @Data
    static class Menu {
        private String menuId;
        private String parentId;
        private String menuDescribe;
        private List<Menu> children;

        public Menu(String menuId, String parentId, String menuDescribe) {
            this.menuId = menuId;
            this.parentId = parentId;
            this.menuDescribe = menuDescribe;
        }
    }


    public void insert(List<Menu> menus) {
        for (Menu menu : menus) {
            if (menu.parentId == null) {
                root = menu;
            }
        }
        List<Menu> currents = new ArrayList<>();
        currents.add(root);

        while (true) {
            for (Menu current : currents) {
                List<Menu> childs = new ArrayList<>();
                for (Menu menu : menus) {
                    if (menu.parentId == current.menuId) {
                        childs.add(menu);
                    }
                }
                current.children = childs;
                currents = childs;
                if (currents == null || currents.size() == 0) {
                    return;
                }
            }
        }

    }


    public static void main(String[] args) {
        List<Menu> menuList = new ArrayList<Menu>();
        /*插入一些数据*/
        menuList.add(new Menu("1", null, "系统管理"));
        menuList.add(new Menu("4", "1", "权限管理"));
        menuList.add(new Menu("5", "1", "密码修改"));
        menuList.add(new Menu("6", "1", "新加用户"));
        menuList.add(new Menu("7", "4", "系统监控"));
        menuList.add(new Menu("8", "4", "在线用户"));
        menuList.add(new Menu("9", "5", "订阅区"));
        menuList.add(new Menu("10", "6", "未知领域"));

        MenuTree menuTree = new MenuTree();
        menuTree.insert(menuList);
        System.out.println(menuTree+"=============");
    }


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值