Skip to content
当前页

入门推荐

  • 书籍
    • 大话数据结构
  • 视频
    • 浙江大学的数据结构
  • 实操
    • Leetbook
    • 剑指offer

大话数据结构

开源书籍

常见数据类型

  • 线性表
    • 顺序存储
    • 链式存储
      • 单链表
      • 静态链表
      • 循环链表
      • 双向链表
  • 栈与队列

常见二叉树

  • 完美二叉树:所有层的节点都被完全填满
  • 完全二叉树:仅允许最底层的节点不完全填满,且最底层的节点必须从左至右依次连续填充
  • 完满二叉树:除了叶节点之外,其余所有节点都有两个子节点
  • 平衡二叉树:左子树和右子树的高度之差的绝对值不超过 1

二叉树遍历

  • 层序遍历(广度优先搜索)
  • 深度优先搜索
    • 前序遍历:先root,后左右
    • 中序遍历:先左,后root,再右
    • 后序遍历:先左,后右,再root ::: 前中后序,可以理解为何时处理root节点 :::

层序遍历

js
function levelOrder(root) {
  const queue = [root]
  const list = []
  while (queue.length) {
    const node = queue.shift()
    list.push(node.val)
    node.left && queue.push(node.left)
    node.right && queue.push(node.right)
  }
  return list
}

深度优先搜索

js
const list = []

// 前序
function preOrder(root) {
  if (root === null) return;
  list.push(root.val)
  preOrder(root.left)
  preOrder(root.right)
}
// 中序
function preOrder(root) {
  if (root === null) return;
  preOrder(root.left)
  list.push(root.val)
  preOrder(root.right)
}
// 后序
function preOrder(root) {
  if (root === null) return;
  preOrder(root.left)
  preOrder(root.right)
  list.push(root.val)
}

二叉树的数组表示

  • 完美二叉树按照层序遍历,直接平铺为数组
  • 非完美二叉树在空位补空值
  • 第i个节点,其左节点索引为 2i+1,右节点为 2i+2

二叉搜索树

满足: 左节点 < root < 右节点的树称为二叉搜索树

  • 二叉搜索树的中序遍历序列是升序的

二叉搜索树的常见应用

  • 用作系统中的多级索引,实现高效的查找、插入、删除操作

AVL树,又称平衡二叉搜索树

同时满足二叉搜索树和平衡二叉树的成为平衡二叉搜索树