Microproject Dsu
Microproject Dsu
Microproject Dsu
Given a Binary Search Tree, The task is to print the elements in inorder, preorder,and
Input :
Output :
Output :
Inorder Traversal: 8 12 20 22 25 30 40
Preorder Traversal: 22 12 8 20 30 25 40
At first traverse left subtree then visit the root and then traverse the right subtree.
The inorder traversal of the BST gives the values of the nodes in sorted order. To get the
#include <stdio.h>
#include <stdlib.h>
struct node {
int data;
};
node->data = data;
node->left = NULL;
node->right = NULL;
return (node);
if (node == NULL)
return;
// First recur on left child
printInorder(node->left);
printInorder(node->right);
// Driver code
int main()
root->left = newNode(2);
root->right = newNode(3);
root->left->left = newNode(4);
root->left->right = newNode(5);
// Function call
printInorder(root);
getchar();
return 0;
}
output :
At first visit the root then traverse left subtree and then traverse the right subtree.
#include <stdio.h>
#include <stdlib.h>
struct node {
int data;
};
{
struct node* node
node->data = data;
node->left = NULL;
node->right = NULL;
return (node);
if (node == NULL)
return;
printPreorder(node->left);
printPreorder(node->right);
// Driver code
int main()
root->left = newNode(2);
root->right = newNode(3);
root->left->left = newNode(4);
root->left->right = newNode(5);
// Function call
printPreorder(root);
getchar();
return 0;
Output :
At first traverse left subtree then traverse the right subtree and then visit the root.
#include <stdio.h>
#include <stdlib.h>
int data;
};
node->data = data;
node->left = NULL;
node->right = NULL;
return (node);
if (node == NULL)
return;
printPostorder(node->left);
// Then recur on right subtree
printPostorder(node->right);
// Driver code
int main()
root->left = newNode(2);
root->right = newNode(3);
root->left->left = newNode(4);
root->left->right = newNode(5);
// Function call
printPostorder(root);
getchar();
return 0;
Output :
2. https://www.geeksforgeeks.org/construct-tree-from-given-inorder-and-preorder-traversal/amp/
3.https://www.geeksforgeeks.org/check-if-given-preorder-inorder-and-postorder-traversals-are-of-same-tree/amp/
4. https://www.geeksforgeeks.org/construct-tree-inorder-level-order-traversals-set-2/amp/
5. https://www.geeksforgeeks.org/full-and-complete-binary-tree-from-given-preorder-and-postorder-
traversals/amp/
6.https://www.geeksforgeeks.org/pre-order-post-order-and-in-order-traversal-of-a-binary-tree-in-one-traversal-
using-recursion/amp/
7.https://www.geeksforgeeks.org/inorder-successor-in-binary-search-tree/amp/?
8.https://www.geeksforgeeks.org/leaf-nodes-preorder-binary-search-treeusing-recursion/amp/
9.https://www.geeksforgeeks.org/leaf-nodes-preorder-binary-search-treeusing-recursion/amp/
10.https://www.geeksforgeeks.org/maximum-sub-tree-sum-in-a-binary-tree-such-that-the-sub-tree-is-also-a-
bst/amp/