[personal profile] sassa_nf
Тут хтось поскаржився, що порядок обчислень в Х-і не зрозумілий. Ну, ось вам імперативний варіант, не обляпайтесь:
function walk2(preorder, inorder) {
  let node = {parent: {}};
  while(preorder.length !== 0) {
    if (preorder[preorder.length - 1] === node.parent.value) {
      preorder.pop();
      node.parent.left = node;
      node = node.parent;
      continue;
    }
    node.parent = {value: inorder.pop(), parent: node.parent, right: node};
    node = {parent: node.parent};
  }
  return node;
}
Мені аж цікаво, як я раніше в такому розбирався?

Date: 2016-07-24 05:18 pm (UTC)
From: [identity profile] nivanych.livejournal.com
> не обляпайтесь:

Кусаю крупные куски, жЫр во все стороны, вкуснотищщаа!
Ой, обляпался...
;-)

Date: 2016-07-24 09:34 pm (UTC)
From: [identity profile] juan-gandhi.livejournal.com
Well, weird; first I asked myself if we roll it all back and try to write it immutably, that is, start building the tree starting from leaves... then I figure, it was my solution (big deal, use call stack instead)

Date: 2016-07-24 09:45 pm (UTC)
From: [identity profile] sassa-nf.livejournal.com
What I find weird is that the immutable solution forces you to look at it inductively. You start expressing yourself in terms of subtrees (how do we know we have the whole left subtree for a node; how do we know we have the whole right subtree), because you can no longer think about "setting" pointers, or tweaking them later.

This imperative piece was obtained to see if we can do the same in reverse.

Date: 2016-07-25 12:27 am (UTC)
From: [identity profile] juan-gandhi.livejournal.com
Right. Just all my experience with mutable pointers tells me - it's a hell.

Date: 2016-07-25 03:38 am (UTC)
From: [identity profile] nponeccop.livejournal.com
Да даже удаление элемента из середины двунаправленного списка.

Date: 2016-07-25 04:24 am (UTC)
From: [identity profile] thedeemon.livejournal.com
То ли дело удаление элемента из середины двунаправленного списка в хаскеле или расте! :)

Date: 2016-07-25 05:08 am (UTC)
From: [identity profile] juan-gandhi.livejournal.com
Особенно в условиях "concurrency".

Date: 2016-07-28 12:58 pm (UTC)
From: [identity profile] zeit-raffer.livejournal.com
Но тут такое дело: этому удалению можно научить любого школьника, если просто заставить его научиться (при наличии достаточного времени или мотивации). А написать диссертацию Криса Окасаки любой школьник не сможет (впрочем, это дискуссионный вопрос).

Profile

sassa_nf

February 2026

S M T W T F S
1234567
891011121314
15161718192021
222324252627 28

Style Credit

Expand Cut Tags

No cut tags
Page generated May. 23rd, 2026 01:48 am
Powered by Dreamwidth Studios