void in-order(struct treenode *tree)
{
if(tree != NULL)
{
in-order(tree→ left);
printf(“%d”,tree→ root);
in-order(tree→ right);
}
}
Sure, here’s an example of C code to perform an in-order traversal on a binary tree:
#include <stdio.h>
#include <stdlib.h>
// Define the structure for a binary tree node
struct TreeNode {
int data;
struct TreeNode *left;
struct TreeNode *right;
};
// Function to perform in-order traversal of a binary tree
void inorderTraversal(struct TreeNode *root) {
if (root != NULL) {
inorderTraversal(root->left);
printf("%d ", root->data);
inorderTraversal(root->right);
}
}
// Function to create a new binary tree node
struct TreeNode* createNode(int data) {
struct TreeNode* newNode = (struct TreeNode*)malloc(sizeof(struct TreeNode));
newNode->data = data;
newNode->left = NULL;
newNode->right = NULL;
return newNode;
}
int main() {
// Creating a sample binary tree
struct TreeNode *root = createNode(1);
root->left = createNode(2);
root->right = createNode(3);
root->left->left = createNode(4);
root->left->right = createNode(5);
printf("In-order traversal of the binary tree: ");
inorderTraversal(root);
printf("\n");
return 0;
}
This code defines a structure TreeNode
to represent nodes in the binary tree. The inorderTraversal
function performs the in-order traversal recursively. Finally, in the main
function, a sample binary tree is created and the inorderTraversal
function is called to print out the nodes in the in-order traversal order.