multipolygon
multipolygon

Reputation: 2234

If traversing a tree data-structure in the direction away from the root-node (and toward the leaves), is that direction "up" or "down"?

I am currently debugging someone else's code that flips the "ups" and "downs" relative to the convention I am accustomed to.

Upvotes: 1

Views: 306

Answers (2)

Tom
Tom

Reputation: 44533

Down. As @Shamim mentioned, this aligns with the "depth-first" terminology.

In computer science, trees are usually drawn with the root node on top, and the leaves at the bottom (i.e. upside down). It makes little sense but I presume it's easier than correctly estimating how much space you'll need to draw it "the right way up".

There's a similar question on Quora: Why are trees in computer science generally drawn upside-down from how trees are in real life?

And the Wikipedia article seems to agree.

Upvotes: 1

Shamim Hafiz - MSFT
Shamim Hafiz - MSFT

Reputation: 22064

It may depend how you draw the Tree on paper, but a better term to use is traversing deeper. That is nodes away from root are located deeper than nodes closer, as used in the term Depth-First-Search.

Upvotes: 1

Related Questions