Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Okay, after your edit where you've introduced a requirement that the hashing result should be different for different tree layouts, you're only left with option to traverse the whole tree and write its structure to a single array.</p> <p>That's done like this: you traverse the tree and dump the operations you do. For an original tree that could be (for a left-child-right-sibling structure):</p> <pre><code>[1, child, 2, child, 3, sibling, 4, sibling, 5, parent, parent, //we're at root again sibling, 6, child, 7, child, 8, sibling, 9, parent, parent] </code></pre> <p>You may then hash the list (that is, effectively, a string) the way you like. As another option, you may even return this list as a result of hash-function, so it becomes collision-free tree representation.</p> <p>But adding <em>precise</em> information about the whole structure is not what hash functions usually do. The way proposed should compute hash function of every node as well as traverse the whole tree. So you may consider other ways of hashing, described below.</p> <hr> <p><strong>If you don't want to traverse the whole tree:</strong></p> <p>One algorithm that immediately came to my mind is like this. Pick a large prime number <code>H</code> (that's greater than maximal number of children). To hash a tree, hash its root, pick a child number <code>H mod n</code>, where <code>n</code> is the number of children of root, and recursively hash the subtree of this child.</p> <p>This seems to be a bad option if trees differ only deeply near the leaves. But at least it should run fast for not very tall trees.</p> <p><strong>If you want to hash less elements but go through the whole tree</strong>:</p> <p>Instead of hashing subtree, you may want to hash layer-wise. I.e. hash root first, than hash one of nodes that are its children, then one of children of the children etc. So you cover the whole tree instead of one of specific paths. This makes hashing procedure slower, of course.</p> <pre><code> --- O ------- layer 0, n=1 / \ / \ --- O --- O ----- layer 1, n=2 /|\ | / | \ | / | \ | O - O - O O------ layer 2, n=4 / \ / \ ------ O --- O -- layer 3, n=2 </code></pre> <p>A node from a layer is picked with <code>H mod n</code> rule.</p> <p>The difference between this version and previous version is that a tree should undergo quite an illogical transformation to retain the hash function.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload