重学数据结构与算法(四)

本文介绍了哈希表和二叉搜索树两种重要的数据结构。哈希表通过哈希函数实现快速存取,但面临冲突处理问题;二叉搜索树是一种有序二叉树,支持高效查找操作。文章详细阐述了它们的实现原理、冲突解决策略以及遍历和查找方法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

重学数据结构与算法(四)

·哈希表

  1. hash:把任意长度的输入通过hash函数变为固定长度的输出,这个输出就是hash值(散列值)例如:MD5

  2. hash表(散列表):是根据关键码可以直接访问的数据结构,关键码通过哈希函数得到固定长度的散列值,将散列值作为索引来利用数组存储对应的数据,这种数组就是哈希表。

    /* 哈希表 */
    class hashTable {
      constructor () {
        this.items = []
      }
      // 存储数据
      set (val, key) {
        let i = this.hash(key)
        this.items[i] = val
      }
      // 获取数据
      get (key) {
        let i = this.hash(key)
        return this.items[i]
      }
      // 一个最最最最简单的hash函数 
      hash (key) {
        return key.charCodeAt() % 37
      }
    }
    const ht = new hashTable()
    ht.set(1234, 'zsjfjg')
    ht.set(345, 'A')
    ht.set(0000, 'asdfg')
    console.log(ht.items)
    console.log(ht.get('A')) // 345
    
  3. 冲突处理:一个好的哈希算法会尽可能就减小冲突的可能,但是实际情况中冲突是不可避免的,如果解决不了冲突问题,用哈希表来存储数据是没有意义的,因为产生冲突后会覆盖原来的数据,造成数据丢失。

  • 开发寻址法
  • 链地址法:在数组的value处存储一个指向链表的地址,所有元素依次存入链表
  • 再散列法:再次用哈希算法得到一个关键码

·二叉搜索树

  1. 二叉搜索树:又称二叉排序树、二叉查找树。一种特殊的二叉树,如果左子树不空,则左子树上的所有节点都小于根结点,如果右子树不空,则右子树上的结点都大于根结点,且左右子树也都是二叉搜索树。
  2. 遍历:先序遍历、中序遍历、后序遍历。序:指根结点被访问的顺序
  3. 查找:查找结点
/* 
二叉搜索树
 */

// 结点类
class Node {
  constructor (val) {
    this.value = val
    this.left = null
    this.right = null
  }
}
// 二叉搜索类
class BinarySearchTree {
  constructor () {
    this.root = null
  }
  // 结点比较大小
  relaNode (node, newnode) {
    if (node.value > newnode) {
      if (node.left === null) {
        node.left === newnode
      } else {
        this.relaNode(node.left, newnode)
      }
    }
    if (node.value < newnode) {
      if (node.right === null) {
        node.right === newnode
      } else {
        this.relaNode(node.right, newnode)
      }
    }
  }
  // 存储
  set (val) {
    const newnode = new Node(val)
    if (this.root === null) {
      this.root = newnode
    } else {
      this.relaNode(this.root, newnode)
    }
  }
  // DLR 先序遍历
  dlr (node, cb) {
    if (node.value === null) {
      return
    }
    cb(node.value)
    this.dlr(node.left, cb)
    this.dlr(node.right, cb)
  }
  // LDR 中序遍历
  dlr (node, cb) {
    if (node.value === null) {
      return
    }
    this.dlr(node.left, cb)
    cb(node.value)
    this.dlr(node.right, cb)
  }
  // LRD 后序遍历
  dlr (node, cb) {
    if (node.value === null) {
      return
    }
    this.dlr(node.left, cb)
    this.dlr(node.right, cb)
    cb(node.value)
  }
  // 回调函数
  cb (val) {
    const arr = []
    arr.push(val)
  }
  // 查询
  search (val) {
    const node = this.root
    while (node.value !== val) {
      if (node.value > val) {
        node = node.left
      } else if (node.value < val) {
        node = node.right
      } else {
        return true
      }
    }
  }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值