Preorder Tree Traversal of Binary Tree in C Direction (Inorder) Anti- Clockwise Rule Center Left Right(CLR)

Understanding PreOrder Traversal in Binary Tree in C

Preorder Tree traversal is where we try print the center most node first, i.e. the root node first and then we go ahead and print the left and then right node. The order of printing thus in anti-clockwise format.

Implementing PreOrder Traversal

• The direction of traversal for preorder is anti-clockwise
• Rule followed is CLR (Center-Left-Right)

What we mean by above is that we try to visit the central node i.e. the root node first and once, there are no central node, we move to left node and right thus forming an anti-clockwise direction.

Check the example below – Explanation for PreOrder Traversal in Binary Tree

• Start Traversing
• Central Node (1): Print, Traverse left
• Central Node (2): Print, Traverse left
• Central Node (4) : Print Traverse Left
• Left Node : Print Traverse Right
• Recur up to Subtree of 2 Print Right node 5
• The whole left subtree of Root node has been printed now go to right subtree of root node
• Central Node (2) : Print, traverse Left
• Central Node (6), print traverse left
• No more left node traverse right print 10
• Traverse right for 7 central node print, recur left
• Print 11 (left node)
• Print 12 (right node)
//PrepInsta's program for PreOrder Tree traversal in Binary Tree
#include <stdio.h>
#include <stdlib.h>

// Basic struct of Tree
struct node
{
int val;
struct node *left, *right;
};

// Function to create a new Node
struct node* newNode(int item)
{
struct node* temp =  (struct node *)malloc(sizeof(struct node));
temp->val = item;
temp->left = temp->right = NULL;
return temp;
}

// Function print the node in preorder format, when insertion is complete
void preorder(struct node* root)
{
if (root != NULL)
{
printf("%d ", root->val); preorder(root->left); preorder(root->right); } } // Here we are finding where to insert the new node so BST is followed struct node* insert(struct node* node, int val) { /* If the tree(subtree) is empty, return a new node by calling newNode func.*/ if (node == NULL) return newNode(val); /* Else, we will do recursion down the tree to further subtrees */ if (val < node->val) node->left = insert(node->left, val); else if (val > node->val) node->right = insert(node->right, val); /* (Safety) return the node's pointer which is unchanged */ return node; } int main() { /* Our BST will look like this 200 / \ 120 280 / \ / \ 80 160 240 320 */ struct node* root = NULL; root = insert(root, 200); insert(root, 120); insert(root, 80); insert(root, 160); insert(root, 280); insert(root, 240); insert(root, 320); // Finally printing the tree using preorder preorder(root); return 0; }

Output –

200 120 80 160 280 240 320 In the above example first root node i.e. 1 get traversed then, using the CLR rule it will traverse the left node i.e. 25 get traversed, then again CLR rule get applied and 7 will get traversed now no left node is left here so it will traverse right node i.e. 15 here , then 14 , then using CLR remaining 22 and 28 will get traversed. The image below will help you to understand in a better way. 