楼主: 14417335
跳转到指定楼层
上一主题 下一主题
收起左侧

[高频题] 王位继承

🔗
jscw 2019-3-14 01:50:25 | 只看该作者
全局:
tbian 发表于 2019-3-13 23:43
请问怎么让birth 也是O(1), 我只能想出O(logn)

insert a node just before its younger brother. O(1)
回复

使用道具 举报

全局:
我还以为是白天晚上系列
回复

使用道具 举报

🔗
jscw 2019-3-14 06:28:03 | 只看该作者
全局:
jscw 发表于 2019-3-14 01:50
insert a node just before its younger brother. O(1)

sorry, should be "the youngest older brother"
Say, Here Tom has one son, Tom II, and two brothers, Frank and John. Here Tom points to Frank and Frank points to John via "brother pointer"
  
Tom <-> Tom II <-> Frank <-> John.

Say Tom has another child, Tom III, then Tom uses brother pointer to find Frank, and insert a new node Tom III just before Frank. O(1).
回复

使用道具 举报

🔗
tbian 2019-3-14 06:29:55 来自APP | 只看该作者
全局:
jscw 发表于 2019/03/06 14:09:30
put nodes into double linked list, the double listed list keep the order of inheritance.
Also, has...

一个人死了怎么改变树的结构,比如一个人死了,他的哥哥的子孙又出生了,会不会很buggy?
回复

使用道具 举报

🔗
tbian 2019-3-15 09:42:15 | 只看该作者
全局:
jscw 发表于 2019-3-14 01:50
insert a node just before its younger brother. O(1)

what if younger brother is dead, or his descendants dead? how do we handle death in treeNode, worse case still be O(N) to finish the preorder
回复

使用道具 举报

🔗
jscw 2019-3-15 11:56:24 | 只看该作者
全局:
tbian 发表于 2019-3-15 09:42
what if younger brother is dead, or his descendants dead? how do we handle death in treeNode, wors ...

death(Tom)

1) use HashMap to find Tom's Node in double linked list, O(1)
2) update the double linked list and associated brother pointer by removing Tom, O(1)
3) remove Tom from HashMap, O(1)

评分

参与人数 2大米 +10 收起 理由
admin + 5
14417335 + 5 很有用的信息!

查看全部评分

回复

使用道具 举报

全局:
jscw 发表于 2019-3-14 06:28
sorry, should be "the youngest older brother"
Say, Here Tom has one son, Tom II, and two brothers ...

你这里并没有update TomII 的brother pointer,所以在TomII有小孩的时候会出错
回复

使用道具 举报

🔗
bdhmwzfa 2019-3-20 15:37:14 | 只看该作者
全局:
14417335 发表于 2019-3-13 22:26
**** 本内容被作者隐藏 ****

请问楼主这是从哪copy过来的?
我一直怀疑是不是真的有很好的方法做到birth和death都O(1)
回复

使用道具 举报

🔗
jscw 2019-3-20 19:42:00 | 只看该作者
全局:
莫可可小姐 发表于 2019-3-20 07:26
你这里并没有update TomII 的brother pointer,所以在TomII有小孩的时候会出错

not exactly. the way to handle Tom II's children is the same as the way to handle Tom's children.

In a nutshell, each node has two kinds of pointers. One is for double linked list and the other is for their brothers. When birth/death event happens, all related pointers should be modified. HashMap saved the mapping between the name and the node address to have O(1) possible.

This approach is just a variation of skip-list. Nothing new, to be honesty.
http://ticki.github.io/blog/skip-lists-done-right/
回复

使用道具 举报

全局:
jscw 发表于 2019-3-20 19:42
not exactly. the way to handle Tom II's children is the same as the way to handle Tom's children.
...

这当然不是什么new technology, 可是按照你的说法,只maintain一个younger brother pointer 是做不到的,包括你的链接里面也写的很清楚,每一层下面也是一个list,而不是一个指针。当你新加入一个node的时候,当前只有parent信息,要如何update当前node的brother信息? 所以parent应该有一个child list. 这是我想说的
回复

使用道具 举报

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

本版积分规则

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