【redis源码学习】rax,我愿称之为“升级版字典树”,2024年最新java面试算法大全

本文深入探讨Redis源码中的rax数据结构,它被称为“升级版字典树”。作者通过介绍如何在插入过程中处理不同情况,揭示了rax的内部工作机制,包括压缩节点的分裂和插入算法。此外,还分享了丰富的Java学习资源,涵盖大厂面试、项目实战等,旨在帮助开发者系统提升技能。
摘要由CSDN通过智能技术生成

先自我介绍一下,小编浙江大学毕业,去过华为、字节跳动等大厂,目前阿里P7

深知大多数程序员,想要提升技能,往往是自己摸索成长,但自己不成体系的自学效果低效又漫长,而且极易碰到天花板技术停滞不前!

因此收集整理了一份《2024年最新Java开发全套学习资料》,初衷也很简单,就是希望能够帮助到想自学提升又不知道该从何学起的朋友。
img
img
img
img
img
img

既有适合小白学习的零基础资料,也有适合3年以上经验的小伙伴深入学习提升的进阶课程,涵盖了95%以上Java开发知识点,真正体系化!

由于文件比较多,这里只是将部分目录截图出来,全套包含大厂面经、学习笔记、源码讲义、实战项目、大纲路线、讲解视频,并且后续会持续更新

如果你需要这些资料,可以添加V获取:vip1024b (备注Java)
img

正文

  • will set h->iskey. */

raxSetData(h,data);

rax->numele++;

return 1; /* Element inserted. */

}

/* If the node we stopped at is a compressed node, we need to

  • split it before to continue.

  • Splitting a compressed node have a few possible cases.

  • Imagine that the node ‘h’ we are currently at is a compressed

  • node contaning the string “ANNIBALE” (it means that it represents

  • nodes A -> N -> N -> I -> B -> A -> L -> E with the only child

  • pointer of this node pointing at the ‘E’ node, because remember that

  • we have characters at the edges of the graph, not inside the nodes

  • themselves.

  • In order to show a real case imagine our node to also point to

  • another compressed node, that finally points at the node without

  • children, representing ‘O’:

  • "ANNIBALE" -> "SCO" -> []
    
  • When inserting we may face the following cases. Note that all the cases

  • require the insertion of a non compressed node with exactly two

  • children, except for the last case which just requires splitting a

  • compressed node.

    1. Inserting “ANNIENTARE”
  •           |B| -> "ALE" -> "SCO" -> []
    
  • "ANNI" -> |-|
    
  •           |E| -> (... continue algo ...) "NTARE" -> []
    
    1. Inserting “ANNIBALI”
  •              |E| -> "SCO" -> []
    
  • "ANNIBAL" -> |-|
    
  •              |I| -> (... continue algo ...) []
    
    1. Inserting “AGO” (Like case 1, but set iscompr = 0 into original node)
  •        |N| -> "NIBALE" -> "SCO" -> []
    
  • |A| -> |-|
    
  •        |G| -> (... continue algo ...) |O| -> []
    
    1. Inserting “CIAO”
  • |A| -> "NNIBALE" -> "SCO" -> []
    
  • |-|
    
  • |C| -> (... continue algo ...) "IAO" -> []
    
    1. Inserting “ANNI”
  • "ANNI" -> "BALE" -> "SCO" -> []
    
  • The final algorithm for insertion covering all the above cases is as

  • follows.

  • ============================= ALGO 1 =============================

  • For the above cases 1 to 4, that is, all cases where we stopped in

  • the middle of a compressed node for a character mismatch, do:

  • Let $SPLITPOS be the zero-based index at which, in the

  • compressed node array of characters, we found the mismatching

  • character. For example if the node contains “ANNIBALE” and we add

  • “ANNIENTARE” the $SPLITPOS is 4, that is, the index at which the

  • mismatching character is found.

    1. Save the current compressed node $NEXT pointer (the pointer to the
  • child element, that is always present in compressed nodes).

    1. Create “split node” having as child the non common letter
  • at the compressed node. The other non common letter (at the key)

  • will be added later as we continue the normal insertion algorithm

  • at step “6”.

  • 3a. IF $SPLITPOS == 0:

  • Replace the old node with the split node, by copying the auxiliary
    
  • data if any. Fix parent's reference. Free old node eventually
    
  • (we still need its data for the next steps of the algorithm).
    
  • 3b. IF $SPLITPOS != 0:

  • Trim the compressed node (reallocating it as well) in order to
    
  • contain $splitpos characters. Change chilid pointer in order to link
    
  • to the split node. If new compressed node len is just 1, set
    
  • iscompr to 0 (layout is the same). Fix parent's reference.
    
  • 4a. IF the postfix len (the length of the remaining string of the

  • original compressed node after the split character) is non zero,
    
  • create a "postfix node". If the postfix node has just one character
    
  • set iscompr to 0, otherwise iscompr to 1. Set the postfix node
    
  • child pointer to $NEXT.
    
  • 4b. IF the postfix len is zero, just use $NEXT as

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值