# Is AVL tree insertion pseudocode alright? [closed]

I'm new to writing pseudo code. I otherwise code in C. While writing in C, I take special steps to check if pointers are null. Should I necessarily show it in pseudo code?

Here is my pseudo code of a function(and it's 2 helpers) that inserts a key into an AVL tree :

``````Insert_Element (Root , Key)
node = Root
if node is NULL         //if node is uninitialized
then
node = New Node           //initialize it
node[key] = key
node[parent] = NULL
else
while node is not NULL
do
parent_node = node
if key < node[key]
then
node = node[left]
else if key > node[key]
node = node[right]
else
Raise_duplicate_key_error         //raise an error saying that the key already exists
done
//location to insert the new key is now found
node = New Node
node[key] = key         //set the key
node[parent] = parent_node         //link to the parent node

if node[key] > parent_node[key]         //decide where the new node should be (Right or Left ?)
then
parent_node[right] = node
else
parent_node[left] = node

//Now to balance the tree

while node is not NULL
do
node = node[parent]
x = y
y = z
z = node[parent]
if node[balanced] is false
then
break
done
if z is NULL         //it means we couldn't find an imbalanced node the tree
then
return
else if (x is not NULL and y is not NULL) and ((y[left] is x and z[right] is y) or (y[right] is x and z[left] is y))
//if it needs a double rotation ( right followed by left linkage or left followed by right linkage exists)
then
Rotate_Tree y x
Rotate_Tree z y
else
Rotate_Tree z y         //it just needs a single rotation

end of Insert_Element

//Given a parent and a child node, Rotate_Tree rotates the tree at parent
//It makes child take the place of parent
Rotate_Tree parent child
if (parent is NULL) or (child is NULL)
return
if parent[right] is child
then
parent[right] = child[left]
child[left][parent] = child
child[parent] = parent[parent]
if parent[parent][left] is parent
then
parent[parent][left] = child
else if parent[parent][right] is parent
then
parent[parent][right] = child
parent[parent] = child
child[left] = parent
if parent[left] is child
then
parent[left] = child[right]
child[right][parent] = child
child[parent] = parent[parent]
if parent[parent][left] is parent
then
parent[parent][left] = child
else if parent[parent][right] is parent
then
parent[parent][right] = child
parent[parent] = child
child[right] = parent
end of Rotate_Tree

Balanced node
if difference between height(node[left]) and height(node[right]) <= 1
then
return 1
else
return 0
end of Balanced
``````

Should I be checking if `New Node` gave valid memory to the pointer? Is the purpose of pseudo code just to give an idea of what the program does, logically? Is mine strict enough?

-
This question appears to be off-topic because it is about pseudocode, which we do not review. –  Jamal Nov 21 at 3:59