Binary search tree: insert: Difference between revisions

From Algowiki
Jump to navigation Jump to search
(Created page with "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 //Tr...")
 
No edit summary
Line 13: Line 13:
:::then left[y] = z
:::then left[y] = z
:::else right[y] = z
:::else right[y] = z
[[Category:Binary Search Tree]]

Revision as of 22:04, 19 September 2014

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