juan_gandhi: (VP)
Everybody knows that a binary tree is defined by a formula T(x)=1+x*T2(x), where x is the type of the value that the tree contains.

For the beginners: A tree is either empty (=1) or a triple: (x, t1, t2) where t1 and t2 are also binary trees of the aforementioned kind.

So there. Easy. You can see that this is a quadratic equation, and that it can be represented as a cubic root on a complex plane... it's not about this; let's see how we can store changes in trees, that is, differences, that is, derivatives.

dT/dx = T2 + 2*x*T*dT/dx, whereby we have
dT/dx = T2/(1-2*x*T)

Oh, wait, you probably heard already that List(x) is defined by a formula List(x)=1+x*List(x), right? Either empty (=1) or a pair (ok, a product) (x, List(x)), right?

but this equation, List(x)=1+x*List(x) has a solution, List(x)=1/(1-x). Remember this.

Now 1/(1-2*x*T) is a list, List(2*x*T)

So dT/dx = T2 * List(2*x*T)

An update of a tree can be represented as two trees and a list of triples (bool, x, T), where T is a tree.
But what is it?

The two trees are left and right subtrees of the point-of-change; and the list of triples is the path from the root to the point of change; each element of the path being this:
- left or right
- the changed value
- the intact subtree

On this picture you see gray left, black right, and the path is red values with brown intact subtrees.



Cool eh?

Note, I did not use any particular programming language.

Source: http://www.youtube.com/watch?v=YScIPA8RbVE&noredirect=1
juan_gandhi: (Default)
Некоторые люди полагают, что лазать по дереву нельзя без того, чтобы узлы держали указатели на родителей.

Потому и дерево, собственно. Если у нас меню, и один пункт повторяется в нескольких местах... короче, не получится. А ведь в принципе что, посет и посет.

Люди более продвинутые хранят сокровенное знание - ссылки на верх не имеют права на существование. Я таких встречал на интервью дважды. Они, возможно, не делятся этим знанием, потому что знают, что никто их не поддержит.

Так вот, читая Beautiful Code, я понял кое-что.

Конечно, ссылок на верх не надо. Родитель содержит списки детей, и всё.

Но когда мы браузим, то мы должны помнить, откуда пришли. Это и есть указатель на родителя. Тут вообще можно обобщать на графы (ну там решить вопрос с циклами)... или на категорию, ё.

Но главное, что стек (при dfs) и хранит всю необходимую информацию.

Стек - это же что-то вроде коданных. Codata. Происходит свёртка с данными.

На эту же тему - кванты неплохо бы в начальной школе преподавать. Да некому. Как 10000 лет назад некому было учить детей грамоте (нет ли тут русофобии).

Profile

juan_gandhi: (Default)
Juan-Carlos Gandhi

May 2025

S M T W T F S
    1 2 3
456 7 8 9 10
11 121314151617
18192021222324
25262728293031

Syndicate

RSS Atom

Most Popular Tags

Style Credit

Expand Cut Tags

No cut tags
Page generated May. 15th, 2025 02:20 pm
Powered by Dreamwidth Studios