Exercise 12.3-1

Give a recursive version of the TREE-INSERT procedure.

NEW-TREE-INSERT(T, z):

1 RECURSIVE-TREE-INSERT(T, T.root, NIL, z)

RECURSIVE-TREE-INSERT(T, x, y, z)

01 if x == NIL:

02     z.p = y

03     if y == NIL:

04         T.root = z

05     else if z.key < y.key:

06         y.left = z

07     else y.right = z

08 else:

09     y = x

10     if z.key < x.key:

11         x = x.left

12     else x = x.right

13      RECURSIVE-TREE-INSERT(T, x, y, z)