Binary search tree: traverse: Difference between revisions
No edit summary |
|||
(One intermediate revision by the same user not shown) | |||
Line 29: | Line 29: | ||
'''Break condition:''' <math>S</math> is empty. | '''Break condition:''' <math>S</math> is empty. | ||
'''Remark:''' The number of iterations accomplished so far is the natural induction parameter. For conciseness, the induction parameter is omitted in the following. | |||
== Induction Basis == | == Induction Basis == |
Latest revision as of 13:41, 3 March 2017
Genaral Information
Algorithmic problem: Sorted sequence: traverse
Type of algorithm: loop
Auxiliary data:
- A stack S whose elements are pairs consisting of
- a natural number seenChildren in the range
and - a binary search tree node node.
- a natural number seenChildren in the range
- Pointers elem and elem' to stack elements.
Abstract View
Invariant: Before and after each iteration:
contains all nodes on the path (called the current path) from the root to some binary search tree node (called the current node) in the order from the root to the current node (in particular, the current node is the top element of ).- Let elem be some element of the stack:]
- If
, neither nor one of the keys in the subtree rooted at has been appended to so far; possibly, some or all keys in the subtree rooted at have already been appended. - If
, all keys in the subtree rooted at and, afterwards, have already been appended to ; possibly, some or all keys in the subtree rooted at have been appended as well. - If
, all keys in the subtree rooted at have been appended to
- If
Variant: Indentify the current content of
- is either empty (which, clearly, is tantamount to
being empty), - or it is lexicographically larger than the string immediately before the iteration.
Break condition:
Remark: The number of iterations accomplished so far is the natural induction parameter. For conciseness, the induction parameter is omitted in the following.
Induction Basis
Abstract view: Initialize
Implementation:
- Create a new stack element
. - Set
. - Set
. - Ensure that
is empty. .
Proof: All invariants are trivially fulfilled.
Induction Step
Abstract view:
- If the left child of the current node has not yet been examined:
- If the left child of the current node exists, proceed to the left child.
- Otherwise, if the right child of the current node has not yet been examined:
- Append the key of the current node to
. - If the right child of the current node exists, proceed to the right child.
- Append the key of the current node to
- Otherwise (that is, left and right child examined), proceed to the parent of the current node.
Implementation:
- Set
. - Set
- If
:- If
:- Create a new element
. - Set
. - Set
. .
- Create a new element
- Otherwise:
- Set
. .
- Set
- If
- Otherwise, if
:- If
:- Create a new elem
. - Set
. - Set
. .
- Create a new elem
- Otherwise, set
.
- If
- Otherwise, (that is
). .- If
is empty, terminate the algorithm. - Set
- Set
. - If
and then: .
Correctness: Invariant #1 follows immediately from the fact that each push operation simply extends the current path and each pop operation cuts off the end node of the current path. Next consider Invariant #2.
Invariant #2 is trivially maintained both for
Complexity
Statement: Linear in the length of the sequence in the worst case.
Proof: Each iteration of the loop takes constant time. Each node is visited in a most three iterations, viz. once with
Pseudocode
- INORDER-TREE-WALK(x)
- if x ≠ NULL
- INORDER-TREE-WALK(left[x])
- print key[x]
- INORDER-TREE-WALK(right[x])
- if x ≠ NULL