Binary search tree: find: Difference between revisions
No edit summary |
No edit summary |
||
Line 1: | Line 1: | ||
:TREE-SEARCH (x, k) | == General Information == | ||
'''Algorithmic Problem:''' [[Sorted Sequence:find]] | |||
'''Type of algorithm:''' loop | |||
'''Auxiliary data:''' A pointer '''''p''''' of type "pointer to binary search tree node of type <math>\kappa</math>". | |||
== Abstract view == | |||
'''Invariant:''' After <math>i\geq 0</math> Iterations. | |||
# The pointer '''''p''''' points to a tree node '''''v''''' on height level '''''i''''' (or is void). | |||
# The key '''''K''''' is in the range of '''''v'''''. | |||
'''Variant:''' '''''i''''' is increased by 1. | |||
'''Break condition:''' Either it is <math>p = void</math> or, otherwise, <math>p.key = K</math>. | |||
== Induction basis == | |||
'''Abstract view:''' Set '''''p''''':= '''''root'''''. | |||
'''Implementation:''' Obvious | |||
'''Proof:''' Nothing to show | |||
== Induction step == | |||
'''Abstract view:''' If '''''p''''' points to a node but not with key '''''K''''', '''''p''''' descends in the appropriate direction, left or right. | |||
'''Implementation:''' | |||
# If <math>p = void</math>, terminate the algorithm and return '''''false'''''. | |||
# Otherwise, if <math>p.key = K</math>, terminate the algorithm and return '''''true'''''. | |||
# Otherwise: | |||
## If <math>K < p.key</math>, set <math>p := left</math>. | |||
## If <math>K > p.key</math>, set <math>p := right</math>. | |||
'''Correctnes:''' Obvious. | |||
== Complexity == | |||
'''Statement:''' Linear in the length of the sequence in the worst case (more precisely, linear in the height of the tree). | |||
'''Proof:''' Obvious. | |||
== Implementation == | |||
TREE-SEARCH (x, k) | |||
:if x= NIL or k = key[x] | |||
::then return x | |||
:if k < key[x] | |||
::then return TREE-SEARCH(left[x], k) | |||
:else return TREE-SEARCH(right[x], k) | |||
[[Category:Binary_Search_Tree]] | [[Category:Binary_Search_Tree]] |
Revision as of 19:55, 23 September 2014
General Information
Algorithmic Problem: Sorted Sequence:find
Type of algorithm: loop
Auxiliary data: A pointer p of type "pointer to binary search tree node of type [math]\displaystyle{ \kappa }[/math]".
Abstract view
Invariant: After [math]\displaystyle{ i\geq 0 }[/math] Iterations.
- The pointer p points to a tree node v on height level i (or is void).
- The key K is in the range of v.
Variant: i is increased by 1.
Break condition: Either it is [math]\displaystyle{ p = void }[/math] or, otherwise, [math]\displaystyle{ p.key = K }[/math].
Induction basis
Abstract view: Set p:= root.
Implementation: Obvious
Proof: Nothing to show
Induction step
Abstract view: If p points to a node but not with key K, p descends in the appropriate direction, left or right.
Implementation:
- If [math]\displaystyle{ p = void }[/math], terminate the algorithm and return false.
- Otherwise, if [math]\displaystyle{ p.key = K }[/math], terminate the algorithm and return true.
- Otherwise:
- If [math]\displaystyle{ K \lt p.key }[/math], set [math]\displaystyle{ p := left }[/math].
- If [math]\displaystyle{ K \gt p.key }[/math], set [math]\displaystyle{ p := right }[/math].
Correctnes: Obvious.
Complexity
Statement: Linear in the length of the sequence in the worst case (more precisely, linear in the height of the tree).
Proof: Obvious.
Implementation
TREE-SEARCH (x, k)
- if x= NIL or k = key[x]
- then return x
- if k < key[x]
- then return TREE-SEARCH(left[x], k)
- else return TREE-SEARCH(right[x], k)