From 957d7c787a279dad8895bf7efe39db92c2967cf9 Mon Sep 17 00:00:00 2001 From: rohith Date: Wed, 25 Oct 2023 19:52:26 +0530 Subject: [PATCH 1/2] done_tree_traversal_in_C --- DSA_Codesheet/C/treetransversal.c | 78 +++++++++++++++++++++++++++++++ 1 file changed, 78 insertions(+) create mode 100644 DSA_Codesheet/C/treetransversal.c diff --git a/DSA_Codesheet/C/treetransversal.c b/DSA_Codesheet/C/treetransversal.c new file mode 100644 index 0000000..cefa06a --- /dev/null +++ b/DSA_Codesheet/C/treetransversal.c @@ -0,0 +1,78 @@ +#include +#include +#include +struct node +{ + int data; + struct node *left,*right; +}; +typedef struct node Node; +bool first; +// Function to perform In-Order Traversal +void inOrderTraversal(Node* root) { + if (root == NULL) return; + inOrderTraversal(root->left); + if (!first) printf(" -> "); + printf("%d",root->data); + first = false; + inOrderTraversal(root->right); +} +// Function to perform Post-Order Traversal +void postOrderTraversal(Node* root) { + if (root == NULL) return; + postOrderTraversal(root->left); + postOrderTraversal(root->right); + if (!first) printf(" -> "); + printf("%d",root->data); + first = false; +} + +// Function to perform Pre-Order Traversal +void preOrderTraversal(Node* root) { + if (root == NULL) return; + if (!first) printf(" -> "); + printf("%d",root->data); + first = false; + preOrderTraversal(root->left); + preOrderTraversal(root->right); +} +Node * createNode(int data){ + Node *root=malloc(sizeof(Node)); + root->data=data; + root->left=NULL; + root->right=NULL; +} +int main() { + // Creating a sample binary tree + Node* root = createNode(1); + root->left = createNode(12); + root->right = createNode(9); + root->left->left = createNode(5); + root->left->right = createNode(6); + + + first = true; + + printf( "In-Order Traversal: "); + inOrderTraversal(root); + printf("\n"); + + first = true; + printf( "Post-Order Traversal: "); + postOrderTraversal(root); + printf("\n"); + + first = true; + printf( "Pre-Order Traversal: "); + preOrderTraversal(root); + printf("\n"); + + // Clean up: Delete nodes to avoid memory leaks + free(root->left->left); + free(root->left->right); + free(root->left); + free(root->right); + free(root); + + return 0; +} From 85c0fcf2d38583bce4fe691d1f7aaf686391e840 Mon Sep 17 00:00:00 2001 From: rohith Date: Wed, 25 Oct 2023 20:03:34 +0530 Subject: [PATCH 2/2] done_changes_to_README --- README.md | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index dbdcbaa..8baa712 100644 --- a/README.md +++ b/README.md @@ -173,6 +173,14 @@ Distributed under the MIT License. See `LICENSE` for more information.
Yashaswini B - + + + + GovindaRohith +
+ GovindaRohith +
+ +