0

Can someone help me, how I could traversal a balanced binary tree in order without recursion, stack, or morris traversal. I want to traverse it iteratively without modifying the tree. Thank you so much.

ecraig12345
  • 2,328
  • 1
  • 19
  • 26
  • Does this answer your question? [How can I traverse binary search tree without recursion?](https://stackoverflow.com/questions/33022427/how-can-i-traverse-binary-search-tree-without-recursion) – trincot Sep 30 '22 at 06:37
  • Thank you for your answer, but no, it's not good because it uses the stack, and I want no stack, recursion and no tree modification – Iulian Bodea Sep 30 '22 at 06:50
  • Is your tree threaded? Can you at least provide some code, and a data structure definition? Is "balanced" to be understood in the meaning of AVL trees, or do you have a *perfectly* balanced tree, like a *complete* binary tree? – trincot Sep 30 '22 at 08:01
  • Hello, the tree is perfectly balanced, and i want to traverse in order tree after is perfectly balanced – Iulian Bodea Sep 30 '22 at 08:50
  • 1
    So, can you confirm it is a [*complete* binary tree](https://en.wikipedia.org/wiki/Binary_tree#Types_of_binary_trees)? – trincot Sep 30 '22 at 09:28
  • Yes, confirm, only i want, is to see to do traversal inorder, without recursion, stack, morris traversal, beacause i have no idea, how to traversal the tree perfectly balanced without changing. if it can be done recursively without changing the tree, it must be possible to do it iteratively – Iulian Bodea Sep 30 '22 at 09:32
  • Are the keys strictly monotonic (no duplicates)? – Neil Oct 01 '22 at 05:34
  • i don't understand your question – Iulian Bodea Oct 01 '22 at 11:25
  • Are there guaranteed to not be any duplicate keys, _ie_ does the tree represent a set (or map, database), that could be put into a hash without any loss of information (except order), or could there be duplicates (multiset)? _Ie_, can there be `{1, 1, 1, 1, 1, 1, 1, 1, 1}`, or this would cause a collision and there would just be `{1}`? – Neil Oct 02 '22 at 02:11
  • Man, is a simple avl tree balanced, for example 3, 4, 5, 7, 10 must be printed in order, without stack, without recursive, without morris traversal, you have an ideea with the code or no ? – Iulian Bodea Oct 02 '22 at 12:53

1 Answers1

1

In the case where there are no duplicate keys, this corresponds to the tree representing a set (or map.) In that case, a backtracking approach will be O(log n) (AVL tree property) per key. One could get a faster run time by storing the nodes, (such as in recursion,) but often this is unfeasible.

  • If current is not null, descend next with the current key as the target. Whenever the left is taken, first assign an ancestor (before descending.)
  • There are three cases: current was null -> next = root; current == next has a right child, next <- next.right; or next does not have a right child, next <- ancestor (if it does not exist, finished.)
  • In the first two cases, descend on the left until you hit a leaf.

I'll use Wikipedia AVL Tree article example, without the balance factors, (this will work on any binary tree, but one is not guaranteed performance.)

AVL tree.

current path result
null root J C
C ancestor D D
D ancestor F F
F right G G
G ancestor J J
J right P N
N ancestor L L
L ancestor P P
P right V Q
Q ancestor S S
S right U U
U ancestor V V
V right X X
X ancestor null null

If the tree can have duplicate entries, this might be said to be a multiset. In this case, this will not work because this relies on the keys being unique.

Neil
  • 1,767
  • 2
  • 16
  • 22