Binary search tree: insert: Difference between revisions
No edit summary |
|||
Line 1: | Line 1: | ||
[[Category:Binary Search Tree]] | [[Category:Binary Search Tree]] | ||
<div class="plainlinks" style="float:right;margin:0 0 5px 5px; border:1px solid #AAAAAA; width:auto; padding:1em; margin: 0px 0px 1em 1em;"> | |||
<div style="font-size: 1.8em;font-weight:bold;text-align: center;margin:0.2em 0 1em 0">Binary Search Tree<br>Insert</div> | |||
<div style="font-size: 1.2em; margin:.5em 0 1em 0; text-align:center">[[Sorted sequence]]</div> | |||
<div style="font-size: 1.2em; margin:.5em 0 .5em 0;text-align:center">[[File:olw_logo1.png|20px]][https://openlearnware.tu-darmstadt.de/#!/resource/binary-search-tree-1938 Openlearnware]<br>See Chapter 4</div> | |||
</div> | |||
== General Information == | == General Information == | ||
'''Algorithmic problem:''' Sorted sequence: insert | '''Algorithmic problem:''' Sorted sequence: insert |
Revision as of 09:27, 1 October 2014
General Information
Algorithmic problem: Sorted sequence: insert
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.
- The Key K is in the range of v.
Variant: i increased by 1.
Break condition: One of the following two conditions is fulfilled:
- It is [math]\displaystyle{ p.key \geq K }[/math] and [math]\displaystyle{ p.left = void }[/math].
- It is [math]\displaystyle{ p.key \leq K }[/math] and [math]\displaystyle{ p.right = void }[/math].
Induction Basis
Abstract view: If the tree is empty, a new root with key K is created; otherwise, p is initialized so as to point to the root.
Implementation:
- If [math]\displaystyle{ root = void }[/math]:
- Create a new binary search tree node and let root point to it.
- Set [math]\displaystyle{ root.key := K, root.left := void }[/math]. and [math]\displaystyle{ root.right := void }[/math].
- Otherwise, set [math]\displaystyle{ p := root }[/math].
Proof: Obvious.
Induction Step
Abstract view: If the direction where to go next is void, insert K in that empty slot and terminate the algorithm. Otherwise, proceed in that direction.
Implementation:
- If [math]\displaystyle{ p.key = K }[/math]:
- If [math]\displaystyle{ p.left = void }[/math]:
- Create a new node and let [math]\displaystyle{ p.left }[/math] point to it.
- Set [math]\displaystyle{ p.left.key := K }[/math], [math]\displaystyle{ p.left.left := void }[/math], and [math]\displaystyle{ p.left.right := void }[/math].
- Terminate the algorithm.
- Otherwise, if [math]\displaystyle{ p.right = void }[/math]:
- Create a new node and let [math]\displaystyle{ p.right }[/math] point to it.
- Set [math]\displaystyle{ p.left.key := K }[/math], [math]\displaystyle{ p.left.left := void }[/math], and [math]\displaystyle{ p.left.right := void }[/math].
- Terminate the algorithm.
- Otherwise, set [math]\displaystyle{ p := p.left }[/math].
- If [math]\displaystyle{ p.left = void }[/math]:
- Otherwise (that is, [math]\displaystyle{ p.key \gt K }[/math]):
- If [math]\displaystyle{ p.left = void }[/math]:
- Create a new node and let [math]\displaystyle{ p.left }[/math] point to it.
- Set [math]\displaystyle{ p.left.key := K }[/math], [math]\displaystyle{ p.left.left := void }[/math], and [math]\displaystyle{ p.left.right := void }[/math].
- Terminate the algorithm.
- Otherwise, set [math]\displaystyle{ p := p.left }[/math].
- If [math]\displaystyle{ p.left = void }[/math]:
- Otherwise (that is, [math]\displaystyle{ p.key \lt K }[/math]):
- If [math]\displaystyle{ p.right = void }[/math]:
- Create a new node and let [math]\displaystyle{ p.right }[/math] point to it.
- Set [math]\displaystyle{ p.right.key := K }[/math], [math]\displaystyle{ p.right.left := void }[/math], and [math]\displaystyle{ p.right.right := void }[/math].
- Terminate the algorithm.
- Otherwise, set [math]\displaystyle{ p := p.right }[/math].
- If [math]\displaystyle{ p.right = void }[/math]:
Copmplexity
Statement: Linear in the length of the sequence in the worst case (more precisely, linear in the height of the tree).
Proof: Obvious.
Pseudocode
TREE-INSERT(T, z)
- y = Null
- x = root(T)
- while x ≠ NULL
- y = x
- if key[z] < key[x]
- then x = left[x]
- then x = right[x]
- p[z] = y
- if y = NULL
- then root[T] = z //Tree was empty
- else if key[z] < key[y]
- then left[y] = z
- else right[y] = z