📣 Back to School开学季 - VIP通行证5折优惠!蓝莓、Offer多多同步优惠
楼主: complete_46
跳转到指定楼层
上一主题 下一主题
收起左侧

[公开课] Berkeley CS 61B Data Structures(in Java) Homework7 加分+讨论帖

 
🔗
vincentli1 2018-4-4 14:46:57 | 只看该作者
全局:

感觉代码重复量有点高,已经特意把几个重复的多的写成method了,但感觉还是很难避免大量的if else。
我重写了一个 insert(Tree234Node node, int key)的方法然后用recursive的方法实现,debug的时候要舒服很多。
回复

使用道具 举报

🔗
greatlim 2018-4-6 16:24:13 | 只看该作者
全局:
  1. Inserting 84.
  2. 84

  3. Inserting 7.
  4. 7 84

  5. Inserting 22.
  6. 7 22 84

  7. Inserting 95.
  8. (7)22(84 95)

  9. Inserting 50.
  10. (7)22(50 84 95)

  11. Inserting 11.
  12. (7 11)22(50 84 95)

  13. Inserting 37.
  14. (7 11)22(37 50)84(95)

  15. Inserting 60.
  16. (7 11)22(37 50 60)84(95)

  17. Inserting 1.
  18. (1 7 11)22(37 50 60)84(95)

  19. Inserting 23.
  20. (1 7 11)22(23 37)50(60)84(95)

  21. Inserting 16.
  22. ((1)7(11 16)22(23 37))50((60)84(95))

  23. Inserting 100.
  24. ((1)7(11 16)22(23 37))50((60)84(95 100))

  25. Inserting 28.
  26. ((1)7(11 16)22(23 28 37))50((60)84(95 100))

  27. Inserting 86.
  28. ((1)7(11 16)22(23 28 37))50((60)84(86 95 100))

  29. Inserting 49.
  30. ((1)7(11 16)22(23)28(37 49))50((60)84(86 95 100))

  31. Inserting 81.
  32. ((1)7(11 16)22(23)28(37 49))50((60 81)84(86 95 100))

  33. Inserting 51.
  34. ((1)7(11 16)22(23)28(37 49))50((51 60 81)84(86 95 100))

  35. Inserting 99.
  36. ((1)7(11 16)22(23)28(37 49))50((51 60 81)84(86)95(99 100))

  37. Inserting 75.
  38. ((1)7(11 16)22(23)28(37 49))50((51)60(75 81)84(86)95(99 100))

  39. Inserting 66.
  40. ((1)7(11 16)22(23)28(37 49))50((51)60(66 75 81))84((86)95(99 100))

  41. Inserting 4.
  42. ((1 4)7(11 16))22((23)28(37 49))50((51)60(66 75 81))84((86)95(99 100))

  43. Inserting 80.
  44. (((1 4)7(11 16))22((23)28(37 49)))50(((51)60(66)75(80 81))84((86)95(99 100)))

  45. Final tree:
  46.                100
  47.                99
  48.           95
  49.                86
  50.      84
  51.                81
  52.                80
  53.           75
  54.                66
  55.           60
  56.                51
  57. 50
  58.                49
  59.                37
  60.           28
  61.                23
  62.      22
  63.                16
  64.                11
  65.           7
  66.                4
  67.                1

  68. Process finished with exit code 0
复制代码
回复

使用道具 举报

🔗
nyjahchill 2018-6-2 21:14:19 | 只看该作者
全局:

交作业了~
总的来说这次作业考虑的条件很多,但实际上其实难度不太大,这次忽略的一点是在给同level的节点分配完child后,忘了将child的parent指定为新的节点
回复

使用道具 举报

🔗
ff12 2018-6-5 22:13:27 | 只看该作者
全局:
折腾好久才debug完毕,之前设置好node.child后,忘记设置child.parent,导致一直不对


屏幕快照 2018-06-05 下午10.10.35.png (39.7 KB, 下载次数: 1)

屏幕快照 2018-06-05 下午10.10.35.png
回复

使用道具 举报

🔗
renyi 2018-8-11 10:55:25 | 只看该作者
全局:
分类讨论到天荒地老。。。不知道有没有优化的方法TAT
更多图片 小图 大图
组图打开中,请稍候......
回复

使用道具 举报

🔗
copyrightly 2018-8-25 06:03:56 | 只看该作者
全局:
Final tree:
               100
               99
          95
               86
     84
               81
               80
          75
               66
          60
               51
50
               49
               37
          28
               23
     22
               16
               11
          7
               4
               1

tree's size: 22
回复

使用道具 举报

🔗
shendezhuti 2019-5-22 16:29:27 | 只看该作者
全局:


渣渣只能靠参考别人的代码运行成功 下面是我本次运行成功参考的文章以及code

1.https://blog.csdn.net/qy844083265/article/details/80716065
2.https://github.com/convexshiba/C ... 7/dict/Tree234.java

有个注意的地方是1中博客中的说法当我们要split一个3keys结点的时候,要去除原结点中的被移到其他节点中的key,
但是在2中code没有做移除的操作,只有一个keys=1;这让我一度认为2中code存在问题,后来发现
readme.pdf中有一段话解释了为什么在本次hw中不需要这样处理   The "keys" field is the number of keys stored in the node, and must be 1, 2, or
3.  The fields "key1", "key2", and "key3" are filled in (in order) with the int
keys stored in the node.  If keys == 1, the value of key2 doesn’t matter.  If
keys <= 2, the value of key3 doesn’t matter.  
还需要好好理解一下整个思路,妈耶,写个insert都这么费力了,要是手写remove,原地去世。
回复

使用道具 举报

🔗
lesliere 2019-6-24 02:11:15 | 只看该作者
全局:
debug真的弄了很久,前前后后大概搞了4、5个小时1. 时间主要花忘了新的node的parent指针,而且每个单独的情况居然都为这个专门debug了一下,脑子又在偷懒,应该想到其他也会有类似情况,而不是改一点是一点。。。

2. 分类写的很繁琐,一方面要精简,另一方面有同学指出是有recursive的方法的,等刷题再来看好了,先赶赶进度。。。
3. 还有一个是我对insert的逻辑理解不太到位,一开始我以为在find的过程中,一遇到3个node的就拆,然后相应代码跟test code就不对,可是不然到了该插key的地方eject上去parent如果本来有3个就又爆了。。。我总觉得我的想法比较符合lecture notes,它说“Specifically, whenever insert() encounters a 3-key node, the middle key is ejected, and is placed in the parent node instead. Since the parent was previously treated the same way, the parent has at most two keysand always has room for a third.” 我最后改了一下是可以过testcode,但感觉如果遇到parent再爆好像就不行了。。。好想知道大家怎么处理的呀~~~~

屏幕快照 2019-06-24 上午1.52.32.png (88.6 KB, 下载次数: 1)

屏幕快照 2019-06-24 上午1.52.32.png
回复

使用道具 举报

🔗
Alansong641 2020-2-8 15:54:37 | 只看该作者
全局:
附上截图:


花了三个小时,一个小时写,两个小时debug(手动微笑)

主要考验的是对数据结构的熟悉以及特殊情况的分析:

只说特殊情况吧,主要有三点值得注意的地方:
1、当root有三个key和当non-root 有三个key时有不同的解法,前者是root中间的key 不变,将两边的key各新创建一个node(left和right);后者是 将node中间的key2提到parent里,因为parent也是这么处理的,所以parent最多有两个key,可以有一个空位给key2插。【但是插完后parent有三个key了】只新创建一个node即可。

2、root的parent问题,假设我们depth+1,那么原来root里面的child1-4(如果不是null的话)分别分配给新创建的left和right。但是它们的parent变量指向的还是原来的root,所以要重新分配。

3、有点迷糊的一个点,也是花时间最长的一个地方在于【restructure的顺序和insert判断的顺序之间的关系】。比如说从root出发,一路上遇到三个key的进行restruct,然后在进行判断要insert的key应该在哪个child方向走。但是注意这时node所在的中间点已经upstair到它的parent了,所以我们如果进行了restructure,我们要使node=node.parent。回到parent重新选择要走的方向,直至到leaf。(insert一定在leaf上,restructure的目的就是确保我们插入leaf的时候,有空位给我们插)

3.1、这里有一个很特殊的情况在于如果parent原来有两个,restruct后变成了三个key。而我们回滚到parent判断时要插入的key比这三个key都大,因此我们要走child4,这个child4是必有的。(因为我们刚在下面restruct,原来有三个child,新建了个就是四个child)

回复

使用道具 举报

🔗
zengxh 2020-2-11 16:46:54 | 只看该作者
全局:
呀 写完啦! 如图!一直在坚持的做 但发了hw1发现现在不加学分了 所以后面的几个就都没有传了,这次的这个搞了快两个小时,纪念一下~

微信图片_20200211164430.png (144.46 KB, 下载次数: 0)

微信图片_20200211164430.png
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 注册账号
隐私提醒:
  • ☑ 禁止发布广告,拉群,贴个人联系方式:找人请去🔗同学同事飞友,拉群请去🔗拉群结伴,广告请去🔗跳蚤市场,和 🔗租房广告|找室友
  • ☑ 论坛内容在发帖 30 分钟内可以编辑,过后则不能删帖。为防止被骚扰甚至人肉,不要公开留微信等联系方式,如有需求请以论坛私信方式发送。
  • ☑ 干货版块可免费使用 🔗超级匿名:面经(美国面经、中国面经、数科面经、PM面经),抖包袱(美国、中国)和录取汇报、定位选校版
  • ☑ 查阅全站 🔗各种匿名方法

本版积分规则

>
快速回复 返回顶部 返回列表