0% found this document useful (0 votes)
2 views

AVL tree algorithm ex no 11

Uploaded by

lavanya a
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

AVL tree algorithm ex no 11

Uploaded by

lavanya a
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

EX NO 11: AVL Tree program algorithm

Step 1: Start the program


Step 2:Create Node:
Step 2.1: Create a new node with key
Step 2.2: Set left and right child pointers to NULL.
Step 2.3: Set height to 1.
Step 3:Initialize AVL tree root to NULL.
Step 4: Loop until user chooses to stop:
Step 4.1: Prompt user for data.
Step 4.2: call Insert() to insert data into AVL tree.
Step 4.3: Ask user if they want to add more data.
Step 5: call inOrder() to print in-order traversal of AVL tree.
Step 6: call preOrder() to Print pre-order traversal of AVL tree.:
Step 7: Stop the program
Rotation Operations
rightRotate(struct Node* y)
Step 1: Set x to left child of y.
Step 2:Set T2 to right child of x.
Step 3: Set x's right child to y.
Step 4: Set y's left child to T2.
Step 5: Update y's height by taking maximum of y’s left height,y’s right height.
call getHeight()
Step6: Update x's height by taking maximum of x’s left height,x’s right height.
call getHeight()
Step 7: Return x.
leftRotate (struct Node* x)
Step 1:Set y to right child of x.
Step2: Set T2 to left child of y.
Step3: Set y's left child to x.
Step 4: Set x's right child to T2.
Step 5: Update x's height by taking maximum of x’s left height,x’s right height.
call getHeight()
Step 6: Update y's height by taking maximum of y’s left height,y’s right height.
call getHeight()
Step 7: Return y.
getHeight(struct Node* n)
Step 1:if (n == NULL)otherwise goto step 2
Step 1.2return 0
Step 2: return n->height
createNode(int key)
Step 1: create memory for the node and store it in node
Step 2: Assign node->key  key
Step 3: Assign node->left  NULL
Step 4: Assign node->right  NULL
Step 5: Assign node->height  1
Step 6: return node
max(int a, int b)

step 1:if(a > b) return a otherwise return b

getBalanceFactor(struct Node* n)

Step : 1 if (n == NULL)otherwise goto step 2


Step 1.1:return 0
step 2:return call getHeight(n->left) – call getHeight(n->right)
insert()
Step 1: If tree is empty, create new node with key k by calling createNode(key).
Step 2: If key k < node's key, recursively insert into left subtree.
Step 3: If key k > node's key, recursively insert into right subtree.
Step 4: If key k == node's key, return node.
Step 5: Update node's height.
Step 6: Calculate balance factor.
Step 7: If balance factor:
Step 7.1:> 1 and key k < left child's key: right rotate.
Step 7.2:< -1 and key k > right child's key: left rotate.
Step 7.3:> 1 and key k > left child's key: left rotate + right rotate.
Step 7.4:< -1 and key k < right child's key: right rotate + left rotate.
inOrder()
Step 1: If root is not equal to NULL
Step 1.1:Recursively traverse left subtree into inOrder()
Step 1.2: Print node's key.
Step 1.3: Recursively traverse right subtree into inOrder()
PreOrder()
Step 1: if the root is equal to NULL
Step 1.1: Print node's key.
Step 1.2: Recursively traverse left subtree into PreOrder()
Step 1.3: Recursively traverse right subtree into PreOrder()

You might also like