TE
TechEcho
Home24h TopNewestBestAskShowJobs
GitHubTwitter
Home

TechEcho

A tech news platform built with Next.js, providing global tech news and discussions.

GitHubTwitter

Home

HomeNewestBestAskShowJobs

Resources

HackerNews APIOriginal HackerNewsNext.js

© 2025 TechEcho. All rights reserved.

An Encoded Tree Traversal

65 pointsby dmitover 6 years ago

3 comments

cat199over 6 years ago
&gt; There must be existing uses of this encoding, but I’ve been unable to find any or even determine what its name is.<p>not a tree guru.. but isn&#x27;t this somewhat related to &#x27;fractal tree indexing&#x27;:<p><a href="https:&#x2F;&#x2F;en.wikipedia.org&#x2F;wiki&#x2F;Fractal_tree_index" rel="nofollow">https:&#x2F;&#x2F;en.wikipedia.org&#x2F;wiki&#x2F;Fractal_tree_index</a><p><pre><code> Each internal node of a B-tree will contain a number of keys that is one less than its branching factor. The keys act as separation values which divide its subtrees. Keys in subtrees are stored in search tree order, that is, all keys in a subtree are between the two bracketing values. In this regard, they are just like B-trees. </code></pre> also, thanks for the plan9 stuff :D
ball_of_lintover 6 years ago
I like using a level-order traversal because it makes your indexing very simple. For a binary tree you start with 1 and each node has children 2 * i and 2 * i + 1, and parent i &#x2F; 2. You can do a inorder&#x2F;preorder&#x2F;postorder traversal using the typical methods, but for a level-order traversal all you need to do is walk through the backing array.<p>This also generalizes to k-trees. Start at node k-1. your children are ((i - k + 2) * k) through ((i - k + 2) * k) + (k - 1), and your parent is ((i &#x2F; k) - 2 + k) with integer division. Once again this all fits nicely into a flat backing array and requires no extra memory to store children or parents.
评论 #19251837 未加载
评论 #19249760 未加载
评论 #19249529 未加载
bra-ketover 6 years ago
great interview question
评论 #19251841 未加载
评论 #19249909 未加载