Talk:Tree traversal

Iterative Inorder Traversal
Algorithm requires a check if a node has been already printed other wise it will always keep on printing the leftmost and its parent. —Preceding unsigned comment added by 66.235.10.126 (talk • contribs) 30 December 2005
 * Thanks! When doing the recursive to iterative translation I forgot to keep in mind that we somehow have to remember the code position at the last call. I'll ponder how to do this in a neat clean way. Deco 10:59, 30 December 2005 (UTC)

This is what I came up with in C/C++

void inorder(node* root) { bool done = false; Stack stack; node* current = root; while (!done) {       if (current) {           stack.push(current); current = current->left; }       else {            if ( stack.empty == false) {              current = stack.pop; cout << current->value; current = current->right; }             else done = true; }     }    } —Preceding unsigned comment added by 131.107.0.106 (talk • contribs) 31 December 2005

Iterative Traversal Redux
The currently presented iterative algorithm I find a little too obscure. For reference, here it is:

visit(root) { prev   := null current := root next   := null while current &ne; null { if prev == current.parent prev := current next := current.left if next == null or prev == current.left print current.value prev := current next := current.right if next == null or prev == current.right prev := current next := current.parent current := next } }

For a few days, the following version was in the article:

visit(root) { prev   := null current := root next   := null while current &ne; null { if prev == current.parent next := current.left if next == null or prev == current.left print current.value next := current.right if next == null or prev == current.right next := current.parent prev := current current := next } }

It's a little simpler because the "prev := current" has been factored out. But it doesn't work in some cases because of subtleties in the code. It's not a simple factoring because in the original the "prev := current" affected the if statements which followed it, which the factored version does not. Only occasionally does that matter.

One case it fails for is where the root has only one child, a left-hand child. After taking the first then clause ("next := current.left") it mistakenly executes the third then clause ("next := current.parent") causing  to be set to null and the loop to be exited. This is because the test "prev == current.right" succeeds since both values are null. In the original/current code, this failure was prevented by the earlier execution of "prev := current".

I think the current algorithm is too subtle to be a good example. I'm looking at modifications to clarify it. -R. S. Shaw 03:27, 9 June 2006 (UTC)


 * Ah, I see. I didn't take into account the fact that the flow of execution fell through to the next if statement.   Al  03:44, 9 June 2006 (UTC)
 * I agree. I wrote it. If you can come up with anything simpler that would be great. Deco 09:10, 9 June 2006 (UTC)


 * After looking at some alternatives, I've put in a new version of the algorithm. This one uses a nested subroutine (for something that could have just been duplicate code) because it seemed a little clearer despite the extra routine. -R. S. Shaw 00:17, 10 June 2006 (UTC)


 * Personally I find that more complicated, since "midvisit" doesn't seem to have a clear conceptual role. Maybe I'm missing something. Deco 01:20, 10 June 2006 (UTC)

This is the true iterative traversal: The one that doesn't use a stack at all. Those "iterative with stack" traversals should be called "pseudo-iterative" in my book. The disadvantage of the true iterative traversal is that it can only be done on trees where each node, in addition to pointers to its children, also holds a pointer to its parent. Medinoc (talk) 07:08, 22 October 2016 (UTC)

Flaws in All These Algorithms
I notice that all of the algorithms that I looked at on this page contain at least one major bug: They will fail if they are initially passed an empty tree. There is a better way to write them, that avoids this bug (or obligation on the caller if you prefer) and is just as efficient. For example, the preorder traversal could be written (using the same style):

visit(node) if (node == null) return print node.value visit(node.left) visit(node.right)

While this makes twice as many calls to "visit", it also avoids half the dereferences to node's children, so that's arguably a performance wash (it depends on the circumstances as to which will truly run faster). And it eliminates the major bug. All of these code examples can and should be rewritten in the same way.

I think it would also be clearer to call the functions "preorder" "postorder" and "inorder," respectively, instead of calling them all "visit." Cliff Shaffer, 10:25, 21 September 2006.

Rewrite
I rewrote the article, removing implementations, as they were probably incorrect, according to the various comments on them, and as implementations don't replace a good explanation. I consider the article to be a stub now, and valid and clean implementations could be added (I plan to). I also didn't keep the external links, that were not even really related to tree traversal, but merely to the use of graphs in the context of database, and probably came from a web development background (PHP and MySQL related links).

There are surely far better and more classic references to the subject, but the O'Reilly book is the one where I learned tree traversal and where I verified my stub. In particular, I suspect there is a treatment of trees and corresponding algorithms in The Art of Computer Programming, but I don't have it at hand.

Nowhere man 22:15, 1 December 2006 (UTC)

Reverse Polish Notation
The reverse polish notation used in HP calculators is the postfix, not the prefix notation. I don't know English enough to rewrite...

Changes on 14 Feb 07
To anyone watching this article, let me know what you think of the modifications. Restructured it quite a bit, no doubt slipped some typos/mistakes in, but I tried to make it look a bit more professional.

Also changed all the pseudocode to be consistent with one another (some were using curly braces to mark blocks, others just used whitespace - I started at the top where there was whitespace delimited blocks, and made the rest like that), and completely changed the algorithm for iterative/explicit stack based traversals. Let me know if I stuffed it up ! The original code definitely works, but it's possible I mucked up the pseudocode translation. Benefit of the new pseudocode is that at any point the direction could be changed (just use rights instead of lefts, lefts instead of rights), even mid traversal... and that the stack based implementation is virtually identical to the parent pointers implementation.

Also added an example for how to in-order traverse a threaded tree.

Phew. Over an hour down the drain, hope somebody finds it useful =P Themania 06:24, 28 February 2007 (UTC)


 * It looks great. I'll be reviewing it for stylistic concerns, but that's a lot of useful information you've added in. &mdash; Edward Z. Yang (Talk) 22:48, 28 February 2007 (UTC)
 * Thanks Edward =) apologies about reverting your revert - I'm pretty confident that the anomynous ip-logged user was correct, although reverting reverts should probably be mentioned on the talk page..? —The preceding unsigned comment was added by Themania (talk • contribs) 00:11, 1 March 2007 (UTC).
 * No, it's okay. It's tough to tell whether or not an edit is legit when there's no edit summary given. &mdash; Edward Z. Yang (Talk) 02:29, 1 March 2007 (UTC)
 * Would be, especially for non user edits. Just reassuring to know that there's people watching =) Themania 15:15, 1 March 2007 (UTC)

Problem with Iterative Solution
inorder(node)

while hasleftchild(node) do  node = node.left do  visit(node) if not (hasrightchild(node)) then node = node.right // <-- look here else node = node.right // <-- and here while hasleftchild(node) do    node = node.left while node ≠ null

This can't be right... Philfreo 18:59, 23 March 2007 (UTC)


 * You're right, it's wrong. The inner while should have been under the then.  I've fixed the article.  Note that this is not just some iterative solution, it is only for a threaded tree, which essentially always has pointers to skip directly to the next inorder node from a node without a right child, so it happens to have a very simple inorder walk because of the extra threading. -R. S. Shaw 03:31, 24 March 2007 (UTC)

Looking at the previous question, I'm still not sure how this can work. If node does not have a right child, how can you do "else node = node.right" ? According to the graph at the top of the article, once the inorder traversal got to 'A' and there are no child nodes, then it goes back to its parent node.

Maybe something is going on at visit(node) that I don't understand.

inorder(node) while hasleftchild(node) do   node = node.left do   visit(node) if (hasrightchild(node)) then node = node.right while hasleftchild(node) do       node = node.left else node = node.right //? here while node ≠ null

Ddgun (talk) 16:48, 12 February 2008 (UTC)


 * Apparently you're overlooking the word threaded. It's important.  See Threaded binary tree. -R. S. Shaw (talk) 22:34, 13 February 2008 (UTC)

Preorder Iterative Solution in Java
I think the following code is a nice pre-order iterative traversal, using Java code.

I do think that the comment about iterative solutions being easily derived from the recursive ones is not helpful - at least I had a hard time getting solutions that I liked.

I did find nice solutions to all three traversals at the following page: http://discuss.techinterview.org/default.asp?interview.11.318495.11

void preOrder (TreeNode root) { Stack  s = new Stack  ; TreeNode n = root; s.push (root); while (!s.empty) { n = s.pop; if (n != null) { System.out.print (" " + n); s.push (n.right); s.push (n.left); } // end if this node exists } // end while there are nodes to visit } // end method preOrder

nduchon@umuc.edu —Preceding unsigned comment added by 70.104.232.232 (talk) 16:00, August 30, 2007 (UTC)

Non-binary trees
This article seem to focus exclusively on the traversal of binary trees. My question is, if we want to include something about, for instance, traversal of directories on disk, should the focus of this article be changed, or should it be renamed to "binary tree traversal" and a more general article started? I am leaning towards the latter, but want more input than my own before I do it. W (talk) 13:55, 9 May 2008 (UTC)


 * I think we should just change everything to support all kinds of trees and mention somewhere how they can be done a little differently for binary trees. asmeurer  ( talk  |  contribs ) 18:42, 2 July 2012 (UTC)


 * Agreed. The use of the terms "left" and "right" is very misleading for a "tree traversal" article and might lead people who don't know any better to think that all trees are binary. — Preceding unsigned comment added by 82.9.176.129 (talk) 02:22, 24 February 2014 (UTC)


 * I agree, I was shocked to see only binary trees were used as illustrations (images) and implementations. It is mentioned in the article, but it really should be either renamed or rewritten to be more generic. The current article is misleading. PhiLho — Preceding undated comment added 22:55, 7 August 2014 (UTC)

Traversal - Example
Hi, i'm sry, but i don't really understand why the inorder-way in the example ends with "H I" and not with "I H". G's the first right child of the root. There's no left child of G, so we go to I and I' only child is H, so my intuition says there's no way in generell to "skip" a node. And is there a difference mentionable between "rightnode" and "rightchild" ? (btw is there a reason to write rightchild together or divided "right child"...). Sorry for faultily english. If someone's got the time please post me here that the question is answered. Thx a lot. --WissensDürster (talk) 19:29, 24 January 2009 (UTC)


 * I guess this answer would help you right now but maybe someone else will read it. I'd agree that the order is wrong for the "inorder" example. It doesn't make sense to skip this node. G I H should ne the right order! 188.101.213.177 (talk) 10:52, 15 August 2012 (UTC)

Traversal - Terminology
All the kinds of traversal say, e.g. 'visit the left subtree; visit the root; visit the right subtree' Surely it should be 'visit the node' rather than 'visit the root'. We do NOT keep going back to the root. JamesHDavenport (talk) 23:04, 17 March 2010 (UTC)

All of the algorithms for traversals detailed here (pre-order, in-order or symmetric-order, and post-order) are depth-first traversals. They traverse the nodes of the tree in the same order, but they "visit" them at different times. However, "visit the root" is correct terminology because it is a recursive algorithm. For example, when traversing the right sub-tree, "root" becomes the root of the right sub-tree. Xonev (talk) 19:57, 14 April 2010 (UTC)


 * Sure, but the article is titled "tree traversal", not "binary tree traversal", even though the term "left subtree" is completely meaningless for most other types of tree structure. The problem here is that the article uses very tree-type-specific and context-specific language and yet purports to be about trees in general. — Preceding unsigned comment added by 82.9.176.129 (talk) 02:27, 24 February 2014 (UTC)

iterativePostorder??
I'm not convinced iterativePostorder is correct. I've tried coding it up in Java and could not get it to work - although I accept it may be my own inability! A post-order tree traversal is the most difficult to implement of all the methods.

What I do know is that the pseudo code is poorly formatted with respect to the if-elseif-else block. The use of brackets may take up more lines but eliminates error and reader confusion.

I have a slight problem with the algorithms presented. They are primarily for binary trees with the nodes having left and right child nodes. However, the article title is "Tree Traversal" and not "Binary Tree Traversal". Thus, any algorithms presented should strictly be for general trees; ie a variable list of child nodes. This is what I'm trying to implement, the reason why I looked at this article and as a result have found the article confusing and of little use for general tree traversal. — Preceding unsigned comment added by 81.187.174.42 (talk) 09:48, 11 August 2011 (UTC)

References and proper citing
I have given a reference for the Recursive Traversals in C section. I've also put a Clearly, the right subtree has to be duplicated before the duplicate of the current node can be initialized completely. Therefore, I believe, "Post-order" is correct. - Jochen Burghardt (talk) 18:52, 16 November 2019 (UTC)


 * I agree completely with the rule "to duplicate, all child nodes need to be available (duplicated) before the parent node is finished". As far as I can see, the step "Display the data part of the current node" (in the article) does not catch the point. E.g. copying (duplicating) the key and data of the current node is commutative with each of the two recursive calls. As a result, tree duplication can be done in post-, pre-, or even in in-order mode. --Nomen4Omen (talk) 07:49, 19 February 2020 (UTC)


 * You have a point there. In the article, pre/in/post-order is defined only for algorithms that output the node data. If we stick to that definition, the paragraph about duplicating should be removed completely.
 * Alternatively, the definition could be generalized to an arbitrary subroutine that "visits" the node, like in the visitor design pattern. A call to  would then print  's data in most cases, but would duplicate the source node   in the tree duplication case. The   part of my above code could be changed to
 * with the additional subroutine definition:
 * I admit that this looks quite rigged up to me. On the other hand, I think it is impossible to write a similar visitor-based duplication routine that fits into the in- or the pre-order scheme, because the  subroutine will need the duplicated child nodes as parameters.
 * I suggest that unless we find a source for the generalized pre/in/post-order notion, we just delete the "duplication" paragraph, or we mention that pre/in/post doesn't apply in such cases. Would that be ok? - Jochen Burghardt (talk) 09:39, 19 February 2020 (UTC)
 * I admit that this looks quite rigged up to me. On the other hand, I think it is impossible to write a similar visitor-based duplication routine that fits into the in- or the pre-order scheme, because the  subroutine will need the duplicated child nodes as parameters.
 * I suggest that unless we find a source for the generalized pre/in/post-order notion, we just delete the "duplication" paragraph, or we mention that pre/in/post doesn't apply in such cases. Would that be ok? - Jochen Burghardt (talk) 09:39, 19 February 2020 (UTC)


 * Of course, a reliable source would be the best. And you are right that the “essential part” of your (first) -subroutine is its  -part. This  -part requires the existence and the size of the node to be copied. Doesn't this mean that your (first)  -subroutine is essentially pre-order? Because   has to come first and “look” at (i.e. visit) the node! All other operations commute, but have to follow look and  . --Nomen4Omen (talk) 15:03, 19 February 2020 (UTC)


 * I think I see your point: The duplication algorithm can as well be rigged up to fit the pre-order scheme, with the following  part:
 * and with the additional subroutine definition:
 * So it seems that pre/in/post depends on how the visitor pattern is mapped to a (non-object-oriented) imperative programming language: Is  allowed to have extra parameters (needed for post-order)? Is its result allowed to be used outside   (needed for pre-order)? If both is allowed,   could even be made fit the in-order scheme, I guess. - Jochen Burghardt (talk) 07:46, 20 February 2020 (UTC)
 * So it seems that pre/in/post depends on how the visitor pattern is mapped to a (non-object-oriented) imperative programming language: Is  allowed to have extra parameters (needed for post-order)? Is its result allowed to be used outside   (needed for pre-order)? If both is allowed,   could even be made fit the in-order scheme, I guess. - Jochen Burghardt (talk) 07:46, 20 February 2020 (UTC)
 * So it seems that pre/in/post depends on how the visitor pattern is mapped to a (non-object-oriented) imperative programming language: Is  allowed to have extra parameters (needed for post-order)? Is its result allowed to be used outside   (needed for pre-order)? If both is allowed,   could even be made fit the in-order scheme, I guess. - Jochen Burghardt (talk) 07:46, 20 February 2020 (UTC)

I saw that the grafix "Sorted_binary_tree_ALL.svg" is your work. It shows that the difference between the 3 modes is mainly the resulting sequence of acting at the instances. I'm sure that it is allowed to use the result of  outside. But it is certainly also allowed to  the children first, save the resulting pointers in local variables, and then   the current. It seems also to be obvious that  means act on key and data of current only and does not include [as in your last proposal] the looking at the children pointers, because this has to be done with all modes, pre-, in- and post-order. This means that every application where the sequence is irrelevant (as in our case) can be done in every order mode. --Nomen4Omen (talk) 10:26, 20 February 2020 (UTC)
 * Sorry, you were absolutely right. Duplicating is inherently post-order − see changed article. --Nomen4Omen (talk) 07:04, 22 February 2020 (UTC)


 * and in several references (textbooks or otherwise, for e.g. https://qr.ae/pNsElh), as an alternative, the output of an pre-order traversal collects the values of the nodes in a list. And that list is then subsequently used to duplicate/clone another binary tree. While other traversal outputs may also be used to clone a tree, they are neither common nor intuitive. I strongly recommend that we revert back to "Pre-order" - Kgashok (talk) 10:46, 6 August 2020 (UTC)


 * Isn't https://qr.ae/pNsElh a post-order example, because the parent is finished only after all children are finished ?
 * This is because  transports the pointer to the cloned current node to its parent — the last essential action in the function. --Nomen4Omen (talk) 19:19, 6 August 2020 (UTC)
 * The way I see it, the parent's data is copied first (line 6), then the left and then the right. That's the classical pre-order sequence. - Kgashok (talk) 03:34, 7 August 2020 (UTC)

Iterative Inorder Traversal Redux v2
Current implementation of iterative in-order traversal seems to be correct and looks beautiful by being so concise. Unfortunately there is one major flaw in it: current node is pushed onto the stack before the check if child is absent. It means, that all leaf nodes are pushed into the stack then immediately popped out.

My idea is quite simple - we all know that everything around is a trade-of: we trade speed for space, vise-versa, e.t.c.; where "pure" optimization is an elimination of unnecessary work/space... and given that we are talking about all the leaves here, the impact might be quite severe in some cases. Someone would argue that this implementation will benefit more in it's current state - as more comprehensible or maybe provided here for educational/introductory purposes only and not tied to optimization problem. But hit me if I'm wrong, this topic is so general, that this code will end up in many many projects around the globe. Arent' we are not just ruining the overall quality of the code, but also the coding hygiene of younger developers?

I run a couple of tests and achieved a speedup of 17%+ by just eliminating this unnecessary push/pop. Checked some other places around, like geeksforgeeks, first 5 videos on youtube e.t.c. - and, man, this dirty code is all over the place! Am I missing something?

Current implementation for the reference:

iterativeInorder(node) s ← empty stack while (not s.isEmpty or node ≠ null) if (node ≠ null) s.push(node) node ← node.left else node ← s.pop visit(node) node ← node.right

Possible implementation - just to describe rough idea here (i.e. didn't check this exact implementation thoroughly, as mine is a bit more complicated for supporting ranges)

s ← empty stack fillStack(node) while((child ← node.left) ≠ null) s.push(node); node ← child; return node iterativeInorder(node) node ← fillStack(node) while(true) visit(node); node ← node.right; if(node = null) if(s.isEmpty) return node ← stack.pop; else node ← fillStack(node)

DYefimov (talk) 01:59, 18 February 2020 (UTC)


 * In my opinion the most interesting (tree) traversal is the 1-step-next traversal to the next in-order neighbour either right or left of the current node (=up or down). This also means that  is completely to the hand of the caller. Does somebody know a source of such an approach? --Nomen4Omen (talk) 16:16, 19 February 2020 (UTC)

Vertical or Orthogonal Traversal is missing
There are applications of this in computational geometry as well. For e.g. https://www.cs.princeton.edu/courses/archive/spr15/cos226/lectures/99GeometricSearch.pdf

Kgashok (talk) 13:13, 7 August 2020 (UTC)

Generic tree algorithm, in-order nodes visit after last child--bug?
The algorithm presented for generic tree has it performing an in-order operation as well as a post-order operation after the last child. My expectation is the in-order operations should occur between children and only post-order after the last child. Does anyone agree? It could just be an out-by-one-error in the loop counter aka fence post problem. — Preceding unsigned comment added by 2A00:23C4:8585:7D01:BD32:6CC3:3CC1:84D7 (talk) 19:07, 13 January 2021 (UTC)


 * Agree!
 * And changed.
 * In addition, better distinction between "visit", "perform", and "perform recursively".
 * But still a source is missing.
 * –Nomen4Omen (talk) 09:44, 14 January 2021 (UTC)

What happened to the actually iterative traversal implementations?
All the current "iterative" traversal implementations use stacks, which makes them disguised recursive implementations. Why were the actual iterative ones (the ones requiring that each node keep a pointer to its parents in addition to children) replaced with those?Medinoc (talk) 10:58, 8 November 2022 (UTC)

There is an implementation in the threaded binary tree article, it probably got moved. Mathnerd314159 (talk) 16:08, 8 November 2022 (UTC)


 * Well I guess that isn't a full implementation. There was an implementation by Nomen4Omen that got deleted because it was unsourced; Nomen4Omen complained above but it doesn't seem to have been resolved. Mathnerd314159 (talk) 19:07, 8 November 2022 (UTC)
 * Bu if there were a reliably sourced implementation using the parent pointer, I would say it would go in the threaded article, because this article really seems to assume that pointers only go down. Mathnerd314159 (talk) 19:30, 8 November 2022 (UTC)