Tree Traversal in C Programming Language
985Program:
#include #include struct node { int data; struct node *leftChild; struct node *rightChild; }; struct node *root = NULL; void insert(int data) { struct node *tempNode = (struct node*) malloc(sizeof(struct node)); struct node *current; struct node *parent; tempNode->data = data; tempNode->leftChild = NULL; tempNode->rightChild = NULL; //if tree is empty if(root == NULL) { root = tempNode; } else { current = root; parent = NULL; while(1) { parent = current; //go to left of the tree if(data < parent->data) { current = current->leftChild; //insert to the left if(current == NULL) { parent->leftChild = tempNode; return; } } //go to right of the tree else { current = current->rightChild; //insert to the right if(current == NULL) { parent->rightChild = tempNode; return; } } } } } struct node* search(int data) { struct node *current = root; printf("Visiting elements: "); while(current->data != data) { if(current != NULL) printf("%d ",current->data); //go to left tree if(current->data > data) { current = current->leftChild; } //else go to right tree else { current = current->rightChild; } //not found if(current == NULL) { return NULL; } } return current; } void pre_order_traversal(struct node* root) { if(root != NULL) { printf("%d ",root->data); pre_order_traversal(root->leftChild); pre_order_traversal(root->rightChild); } } void inorder_traversal(struct node* root) { if(root != NULL) { inorder_traversal(root->leftChild); printf("%d ",root->data); inorder_traversal(root->rightChild); } } void post_order_traversal(struct node* root) { if(root != NULL) { post_order_traversal(root->leftChild); post_order_traversal(root->rightChild); printf("%d ", root->data); } } int main() { int i; int array[7] = { 28, 15, 36, 11, 20, 32, 44 }; for(i = 0; i < 7; i++) insert(array[i]); i = 31; struct node * temp = search(i); if(temp != NULL) { printf("[%d] Element found.", temp->data); printf("\n"); }else { printf("\n[ x ] Element not found (%d).\n", i); } i = 15; temp = search(i); if(temp != NULL) { printf("\n[%d] Element found.", temp->data); printf("\n"); }else { printf("[ x ] Element not found (%d).\n", i); } printf("\nPreorder traversal: "); pre_order_traversal(root); printf("\nInorder traversal: "); inorder_traversal(root); printf("\nPost order traversal: "); post_order_traversal(root); return 0; }
Output:
Visiting elements: 28 36 32 [ x ] Element not found (31). Visiting elements: 28 [15] Element found. Preorder traversal: 28 15 11 20 36 32 44 Inorder traversal: 11 15 20 28 32 36 44 Post order traversal: 11 20 15 32 44 36 28 Process returned 0 (0x0) execution time : 0.028 s Press any key to continue.
Explanation:
Traversal is a process to visit all the nodes of a tree and may print their values too. Because all nodes are connected via edges (links) we always start from the root (head) node. That is, we cannot randomly access a node in a tree. There are three ways which we use to traverse a tree −
- In-order Traversal
- Pre-order Traversal
- Post-order Traversal
We shall now look at the implementation of tree traversal in C programming language here using the following binary tree −
This Particular section is dedicated to Programs only. If you want learn more about Data Structure. Then you can visit below links to get more depth on this subject.
# C Tutorials
# JAVA Tutorials
# HTML Tutorials
# Computer Fundamental
# Data Structure
# DBMS Tutorials
SQL
# C# Language
# R Language
# PHP
# Python
# Vue JS