请选择 进入手机版 | 继续访问电脑版

湖南新梦想

 找回密码
 立即注册
搜索
热搜: 活动 交友 discuz
查看: 38|回复: 0

二叉树

[复制链接]

20

主题

20

帖子

146

积分

注册会员

Rank: 2

积分
146
发表于 2023-5-25 23:07:33 | 显示全部楼层 |阅读模式
[size=13.0667px]public class Node<T> {    // 定义一个 int 类型的变量 date 用于存储结点中的数据    private int  date;    // 定义一个 Node<T> 类型的变量 leftManager 表示该结点的左孩子    private Node<T> leftManager;    // 定义一个 Node<T> 类型的变量 rightManager 表示该结点的右孩子    private Node<T> rightManager; }public class TreeManager<T> {        private  Node<T> root;//根节点        private int szie; // 树的大小        public void add(int e) { // 添加节点方法            Node node = new Node(e); // 新建一个节点            if (root == null) { // 如果树为空,则将根节点设置为新节点                root = node;                szie++; // 计数器加一            } else {                Node current = root; // 从根节点开始遍历                Node parent;                while (current != null) { // 循环遍历树                    parent = current;                    if (e < current.getDate()) { // 如果待插入节点的值小于当前节点的值,则继续遍历左子树                        current = current.getLeftManager();                        if (current == null) { // 如果左子节点为空,则将新节点插入到当前节点的左子树中                            parent.setLeftManager(node);                            szie++; // 计数器加一                        }                    } else { // 否则,继续遍历右子树                        current = current.getRightManager();                        if (current == null) { // 如果右子节点为空,则将新节点插入到当前节点的右子树中                            parent.setRightManager(node);                            szie++;                        }                    }                }            }        }        public int getMax(){ // 查找树中的最大值            if (root==null){ // 如果树为空,则返回 -1                System.out.println("树为空.....");                return -1;            }else {                Node right=root; // 从根节点开始遍历,找到最右边的节点                while (right.getRightManager()!=null){                    right=right.getRightManager();                }                return right.getDate(); // 返回最右边节点的值            }        }        public int getMin(){ // 查找树中的最小值            if (root==null){ // 如果树为空,则返回 -1                System.out.println("树为空.....");                return -1;            }else {                Node left=root; // 从根节点开始遍历,找到最左边的节点                while (left.getLeftManager()!=null){                    left=left.getLeftManager();                }                return left.getDate(); // 返回最左边节点的值            }        }        public boolean getExists(int e){ // 查找节点是否存在方法            if (root==null){ // 如果树为空,则返回 false                System.out.println("树为空.....");                return false;            }            Node current = root; // 从根节点开始遍历            while (current!=null){                if (current.getDate()==e){ // 如果当前节点的值等于待查找节点的值,则返回 true                    return true;                }else if (e<current.getDate()){ // 否则,如果待查找节点的值小于当前节点的值,则继续遍历左子树                    current=current.getLeftManager();                }                current=current.getLeftManager(); // 否则,继续遍历右子树            }            return false; // 如果未找到,则返回 false        }        public void getList(){            if (root==null){ // 如果树为空,则返回 false                System.out.println("树为空.....");            }        }    public void list(){        getInfixOrder(root);    }    public void getInfixOrder(Node node){        if (node!=null){            getInfixOrder(node.getLeftManager());            System.out.print(node.getDate()+" ");            getInfixOrder(node.getRightManager());        }    }}

回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

QQ|Archiver|手机版|小黑屋|湖南新梦想 ( 湘ICP备18019834号-2 )

GMT+8, 2023-6-2 13:32 , Processed in 0.038537 second(s), 19 queries .

Powered by Discuz! X3.4 Licensed

Copyright © 2001-2020, Tencent Cloud.

快速回复 返回顶部 返回列表