Exe8 1 2
Exe8 1 2
#include <stdio.h>
#include<conio.h>
#include <stdlib.h>
int main() {
struct node* root = NULL;
struct node* found=NULL;
clrscr();
// Insert some nodes
root = insert(root, 50);
insert(root, 30);
insert(root, 20);
insert(root, 40);
insert(root, 70);
insert(root, 60);
insert(root, 80);
return 0;
}
ii) AIM: Traversing of BST.
Traversing a tree means visiting and outputting the value of each node in a particular order. The
traversing methods are Inorder, Preorder, and Post order tree traversal methods.
For Inorder, traverse from the left subtree to the root then to the right subtree.
Inorder => Left, Root, Right.
For Preorder, traverse from the root to the left subtree then to the right subtree.
For Post order, traverse from the left subtree to the right subtree then to the root.
Post order => Left, Right, Root.
#include <stdio.h>
#include<conio.h>
#include <stdlib.h>
// Define the node structure for the BST
struct node {
int data;
struct node *left;
struct node *right;
};
// Function to create a new node
struct node* newNode(int data) {
struct node* temp = (struct node*)malloc(sizeof(struct node));
temp->data = data;
temp->left = temp->right = NULL;
return temp;
}
// Function to insert a node into the BST
struct node* insert(struct node* root, int data) {
// If the tree is empty, create a new node and make it the root
if (root == NULL) {
return newNode(data);
}
int main() {
struct node* root = NULL;
clrscr();