Open main menu
Home
Random
Recent changes
Special pages
Community portal
Preferences
About Wikipedia
Disclaimers
Incubator escapee wiki
Search
User menu
Talk
Dark mode
Contributions
Create account
Log in
Editing
Tree traversal
(section)
Warning:
You are not logged in. Your IP address will be publicly visible if you make any edits. If you
log in
or
create an account
, your edits will be attributed to your username, along with other benefits.
Anti-spam check. Do
not
fill this in!
====Advancing to the next or previous node==== The <code>node</code> to be started with may have been found in the binary search tree <code>bst</code> by means of a standard '''search''' function, which is shown here in an implementation without parent pointers, i.e. it uses a <code>stack</code> for holding the ancestor pointers. '''procedure''' search(bst, key) // returns a (node, stack) node β bst.root stack β '''empty stack''' '''while''' node β '''null''' stack.push(node) '''if''' key = node.key '''return''' (node, stack) '''if''' key < node.key node β node.left '''else''' node β node.right '''return''' ('''null''', '''empty stack''') The function '''inorderNext'''<ref name="Pfaff"/>{{rp|60}} returns an in-order-neighbor of <code>node</code>, either the {{nowrap|in-order-''suc''cessor}} (for <code>dir=1</code>) or the {{nowrap|in-order-''prede''cessor}} (for <code>dir=0</code>), and the updated <code>stack</code>, so that the binary search tree may be sequentially in-order-traversed and searched in the given direction <code>dir</code> further on. '''procedure''' inorderNext(node, dir, stack) newnode β node.child[dir] '''if''' newnode β '''null''' '''do''' node β newnode stack.push(node) newnode β node.child[1-dir] '''until''' newnode = '''null''' '''return''' (node, stack) // node does not have a dir-child: '''do''' '''if''' stack.isEmpty() '''return''' ('''null''', '''empty stack''') oldnode β node node β stack.pop() // parent of oldnode '''until''' oldnode β node.child[dir] // now oldnode = node.child[1-dir], // i.e. node = ancestor (and predecessor/successor) of original node '''return''' (node, stack) Note that the function does not use keys, which means that the sequential structure is completely recorded by the binary search treeβs edges. For traversals without change of direction, the ([[Amortized analysis|amortised]]) average complexity is <math>\mathcal{O}(1) ,</math> because a full traversal takes <math>2 n-2</math> steps for a BST of size <math>n ,</math> 1 step for edge up and 1 for edge down. The worst-case complexity is <math>\mathcal{O}(h) </math> with <math>h</math> as the height of the tree. All the above implementations require stack space proportional to the height of the tree which is a [[call stack]] for the recursive and a parent (ancestor) stack for the iterative ones. In a poorly balanced tree, this can be considerable. With the iterative implementations we can remove the stack requirement by maintaining parent pointers in each node, or by [[#Morris in-order traversal using threading|threading the tree]] (next section).
Edit summary
(Briefly describe your changes)
By publishing changes, you agree to the
Terms of Use
, and you irrevocably agree to release your contribution under the
CC BY-SA 4.0 License
and the
GFDL
. You agree that a hyperlink or URL is sufficient attribution under the Creative Commons license.
Cancel
Editing help
(opens in new window)