Skip to content

For implementing Inorder,preorder and postorder in one code #541

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
#include<stdio.h>
#include<stdlib.h>

struct node
{
int data;
struct node *left;
struct node *right;
};

struct node* create()
{
struct node *newnode;
int x;
char ch;

printf("Enter data for the current node: ");
scanf("%d", &x);
if(x == -1)
{
return NULL;
}
else
{
newnode = (struct node*)malloc(sizeof(struct node));
newnode->data = x;

printf("Do you have a left child for node %d? (y/n): ", x);
scanf(" %c", &ch);

if (ch == 'y' || ch == 'Y') {
printf("Enter data for the left child of node %d: ", x);
newnode->left = create(); // Recursively create left subtree
} else {
newnode->left = NULL;
}

printf("Do you have a right child for node %d? (y/n): ", x);
scanf(" %c", &ch);

if (ch == 'y' || ch == 'Y') {
printf("Enter data for the right child of node %d: ", x);
newnode->right = create(); // Recursively create right subtree
} else {
newnode->right = NULL;
}
return newnode;
}
}

void preorder(struct node *root)
{
if(root != NULL)
{
printf("%d ", root->data);
preorder(root->left);
preorder(root->right);
}
}

void postorder(struct node *root)
{
if(root != NULL)
{
postorder(root->left);
postorder(root->right);
printf("%d ", root->data);
}
}

void inorder(struct node *root)
{
if(root != NULL)
{
inorder(root->left);
printf("%d ", root->data);
inorder(root->right);
}
}

int main()
{
struct node *root = NULL;
printf("Enter elements of the binary tree:\n");
root = create();

printf("Sequence of preorder traversal (root, left, right):\n");
preorder(root);
printf("\n");

printf("Sequence of postorder traversal (left, right, root):\n");
postorder(root);
printf("\n");

printf("Sequence of inorder traversal (left, root, right):\n");
inorder(root);
printf("\n");

return 0;
}
30 changes: 30 additions & 0 deletions Tree/ThreeInOneTraversal.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
# Binary Tree-DSA

In data structures and algorithms (DSA), a tree is a hierarchical data structure consisting of nodes connected by edges. Each node has a parent node and zero or more child nodes. Trees are widely used in computer science for organizing and structuring data efficiently. They are important because they provide a way to represent relationships between data entities, facilitate efficient search, insertion, deletion, and traversal operations, and are used in various applications such as file systems, database indexing, compiler design, and more.

## Explanation of code

### Struct Definition (struct node):

This defines a structure called node, which represents a node in a binary tree. Each node contains an integer data and pointers to its left and right child nodes.

### Function create():

1. This function is used to create a binary tree recursively.
2. It prompts the user to enter data for each node.
3. If the entered data is -1, it returns NULL, indicating the end of the current branch.
4. Otherwise, it creates a new node, assigns the entered data to it, and prompts the user to enter data for its left and right children recursively.

### Traversal Functions (preorder(), postorder(), inorder()):

1. These functions perform preorder, postorder, and inorder traversals of the binary tree, respectively.
2. Preorder traversal visits the root node first, then its left subtree, and finally its right subtree.
3. Postorder traversal visits the left subtree, then the right subtree, and finally the root node.
4. Inorder traversal visits the left subtree, then the root node, and finally the right subtree.
5.Each function recursively traverses the tree, printing the data of each node in the specified order.

### main() Function:

1. This function serves as the entry point of the program.
2. It creates the binary tree by calling the create() function.
3. Then, it traverses the tree using the preorder(), postorder(), and inorder() functions, printing the sequence of traversal for each.