Several bug fixes.

This commit is contained in:
krahets 2023-10-04 02:30:31 +08:00
parent 0e3d2ce4bb
commit 4355f8d49f
7 changed files with 8 additions and 9 deletions

View file

@ -47,7 +47,7 @@ class ArrayHashMap {
int index = hashFunc(key); int index = hashFunc(key);
Pair *pair = buckets[index]; Pair *pair = buckets[index];
if (pair == nullptr) if (pair == nullptr)
return nullptr; return "";
return pair->val; return pair->val;
} }

View file

@ -50,8 +50,8 @@ class HashMapChaining {
return pair->val; return pair->val;
} }
} }
// 若未找到 key 则返回 nullptr // 若未找到 key 则返回空字符串
return nullptr; return "";
} }
/* 添加操作 */ /* 添加操作 */

View file

@ -37,7 +37,7 @@ class HashMapChaining {
} }
/* 查询操作 */ /* 查询操作 */
public string get(int key) { public string? get(int key) {
int index = hashFunc(key); int index = hashFunc(key);
// 遍历桶,若找到 key 则返回对应 val // 遍历桶,若找到 key 则返回对应 val
foreach (Pair pair in buckets[index]) { foreach (Pair pair in buckets[index]) {

View file

@ -59,7 +59,7 @@ class HashMapOpenAddressing {
} }
/* 查询操作 */ /* 查询操作 */
public string get(int key) { public string? get(int key) {
// 搜索 key 对应的桶索引 // 搜索 key 对应的桶索引
int index = findBucket(key); int index = findBucket(key);
// 若找到键值对,则返回对应 val // 若找到键值对,则返回对应 val

View file

@ -29,7 +29,7 @@ class HashMapChaining:
"""负载因子""" """负载因子"""
return self.size / self.capacity return self.size / self.capacity
def get(self, key: int) -> str: def get(self, key: int) -> str | None:
"""查询操作""" """查询操作"""
index = self.hash_func(key) index = self.hash_func(key)
bucket = self.buckets[index] bucket = self.buckets[index]

View file

@ -475,7 +475,6 @@ $$
node.borrow().val.hash(&mut hasher); node.borrow().val.hash(&mut hasher);
let hash = hasher.finish(); let hash = hasher.finish();
// 节点对象 RefCell { value: ListNode { val: 42, next: None } } 的哈希值为15387811073369036852 // 节点对象 RefCell { value: ListNode { val: 42, next: None } } 的哈希值为15387811073369036852
``` ```
=== "C" === "C"

View file

@ -156,7 +156,7 @@ AVL 树既是二叉搜索树也是平衡二叉树,同时满足这两类二叉
use std::rc::Rc; use std::rc::Rc;
use std::cell::RefCell; use std::cell::RefCell;
/* AVL 树节点类型 */ /* AVL 树节点结构体 */
struct TreeNode { struct TreeNode {
val: i32, // 节点值 val: i32, // 节点值
height: i32, // 节点高度 height: i32, // 节点高度
@ -165,7 +165,7 @@ AVL 树既是二叉搜索树也是平衡二叉树,同时满足这两类二叉
} }
impl TreeNode { impl TreeNode {
/* AVL 树节点构造方法 */ /* 构造方法 */
fn new(val: i32) -> Rc<RefCell<Self>> { fn new(val: i32) -> Rc<RefCell<Self>> {
Rc::new(RefCell::new(Self { Rc::new(RefCell::new(Self {
val, val,