当前位置: 首页 > news >正文

数据结构和算法——树结构

二叉树

又叫二叉排序树。

节点是数量为,2^{n}-1,n为层数。

满二叉树:所有的叶子节点都在最后一层。

完全二叉树:如果所有叶子节点都在最后一层和倒数第二层,而且每个叶子节点都有左右子节点。

完全二叉树
完全二叉树

前序遍历

1、先输出当前节点(初始是root节点)。

2、如果左子节点不为空,则递归继续前序遍历。

3、如果右子节点不为空,则递归继续前序遍历。

class HeroNode {private int no;private String name;private HeroNode left, right;
}
    public HeroNode preOrderSearch(int no) {if (this.no == no) {return this;}HeroNode resNode;if (this.left != null) {resNode = this.left.preOrderSearch(no);if (resNode != null) {return resNode;}}if (this.right != null) {resNode = this.right.preOrderSearch(no);if (resNode != null) {return resNode;}}return null;}

中序遍历

1、如果当前节点的左子节点不为空,则递归中序遍历。

2、输出当前节点。

3、如果当前节点的右子节点不为空,则递归中序遍历。

    public HeroNode infixOrderSearch(int no) {HeroNode resNode;if (this.left != null) {resNode = this.left.infixOrderSearch(no);if (resNode != null) {return resNode;}}if (this.no == no) {return this;}if (this.right != null) {resNode = this.right.infixOrderSearch(no);if (resNode != null) {return resNode;}}return null;}

后序遍历

1、如果当前节点的左子节点不为空,则递归后序遍历。

2、如果当前节点的右子节点不为空,则递归后序遍历。

3、输出当前节点。

    public HeroNode postOrderSearch(int no) {HeroNode resNode;if (this.left != null) {resNode = this.left.postOrderSearch(no);if (resNode != null) {return resNode;}}if (this.right != null) {resNode = this.right.postOrderSearch(no);if (resNode != null) {return resNode;}}if (this.no == no) {return this;}return null;}

二叉树节点的删除,如果是中间节点,则整个中间节点都删除。

    public void delNode(int no) {if (this.no == no) {return;}if (this.left != null) {if (this.left.no == no) {this.left = null;} else {this.left.delNode(no);}}if (this.right != null) {if (this.right.no == no) {this.right = null;} else {this.right.delNode(no);}}}

顺序存储二叉树

顺序二叉树通常是完全二叉树。

第n(n是下标)个元素的左子节点为 2 * n + 1

第n个元素的右子节点为 2 * n + 2

第n个元素的父节点为 (n - 1) / 2

堆排序用到顺序存储二叉树的结构。

线索化二叉树

充分的利用到了叶子节点的空指针。

class HeroNode {private int no;private String name;private HeroNode left, right;private int leftType; // 0 指向的是左子树 1指向前驱节点private int rightType; // 0 指向的是右子树 1指向后继节点
}

 对线索二叉树进行中序线索化的方法

    public void threadedNodes(HeroNode node) {if (node == null) {return;}threadedNodes(node.getLeft()); // 先线索化左子树// 再线索化当前节点if (node.getLeft() == null) { // 前驱node.setLeft(pre);node.setLeftType(1);}if (pre != null && pre.getRight() == null) { // 后继pre.setRight(node);pre.setRightType(1);}pre = node;threadedNodes(node.getRight()); // 最后线索化右子树}

线索化二叉树的中序遍历

 class ThreadedBinaryTree {private HeroNode root;private HeroNode pre = null; // 指向前驱节点public void threadedList() {HeroNode node = root;while (node != null) {while (node.getLeftType() == 0) { // 到左下角node = node.getLeft();}System.out.println(node);while (node.getRightType() == 1) {node = node.getRight();System.out.println(node);}node = node.getRight();}}
}

平衡二叉树

又叫AVL树。

B树

B+树

B*树

http://www.lryc.cn/news/194301.html

相关文章:

  • 【Java】Integer包装类
  • Web后端开发登录校验及JWT令牌,过滤器,拦截器详解
  • 大语言模型迎来重大突破!找到解释神经网络行为方法
  • zabbix内置宏、自动发现与注册
  • Oracle与Mysql语法区别
  • Jetpack:008-Icon与Image
  • 参数解析(牛客)
  • Linux网络编程系列之服务器编程——阻塞IO模型
  • 排序算法-基数排序法(RadixSort)
  • nginx绑定tomcat与tomcat联合使用的配置(nginx反向代理tomcat的配置说明)
  • 【Java】nextInt()后面紧接nextLine()读取不到数据/InputMismatchException异常的解决方案
  • 【传输层协议】UDP/TCP结构特点与原理(详解)
  • 哪种网站适合物理服务器
  • uni-app集成使用SQLite
  • Qt不能安装自己想要的版本,如Qt 5.15.2
  • 学信息系统项目管理师第4版系列28_组织级项目管理和量化项目管理
  • Bean实例化的三级缓存
  • Jenkins+Gitlab+Docker(Dockerfile)部署
  • Web前端-Vue2+Vue3基础入门到实战项目-Day4(组件的三大组成部分, 组件通信, 案例-组件版小黑记事本, 进阶语法)
  • 【大模型应用开发教程】01_大模型简介
  • Flume 简介及基本使用
  • 行业追踪,2023-10-11
  • Linux:进程控制
  • HTTP中的GET方法与POST方法
  • 2023年10月16日-10月22日,(光追+ue+osg继续按部就班进行即可。)
  • 【Docker】命令使用大全
  • 查找算法:二分查找、插值查找、斐波那契查找
  • python+django高校教室资源预约管理系统lqg8u
  • Potato靶机
  • 【环境搭建】linux docker-compose安装gitlab和redis