PROGRAM 3.a, B, C
PROGRAM 3.a, B, C
AIM:
To write a program to perform the operations of binary search tree.
ALGORITHM:
Step 1.Start the process.
Step 5. If the key does not match with the value in the root, search its subtrees.
Step 6. If the value of the key is less than the root value, search the left
subtree.
Step 7. If the value of the key is greater than the root value, search the right
subtree.
Step 8. If the key is not found in the tree, return unsuccessful search.
CODE:
#include <stdio.h>
#include <stdlib.h>
struct node {
int key;
struct node *left, *right;
};
struct node* newNode(int item)
{
struct node* temp
= (struct node*) malloc( sizeof(struct node));
temp->key = item;
temp->left = temp->right = NULL;
return temp;
}
struct node* insert(struct node* node, int key)
{
return node;
}
void inorder(struct node* root)
{
if (root != NULL) {
inorder(root->left);
printf("%d ", root->key);
inorder(root->right);
}
}
int main()
{
struct node* root = NULL;
root = insert(root, 50);
insert(root, 30);
insert(root, 20);
insert(root, 40);
insert(root, 70);
insert(root, 60);
insert(root, 80);
inorder(root);
return 0;
}
Output
20 30 40 50 60 70 80
Descriptio Max 90-100% 80-89% 70-79% 60-59% 50%
n
Algorithm
& coding
Output
Execution
Viva
Total
Result:
The program is executed successfully and output is
verified.
Ex.no:3.b Traversal on binary trees
Date:
AIM:
To write a program to perform traversal operations on binary trees.
ALGORITHM:
Step 1:Startthe process.
CODE:
#include <stdio.h>
#include <stdlib.h>
struct node {
int item;
struct node* left;
struct node* right;
};
void inorderTraversal(struct node* root) {
if (root == NULL) return;
inorderTraversal(root->left);
printf("%d ->", root->item);
inorderTraversal(root->right);
}
void preorderTraversal(struct node* root) {
if (root == NULL) return;
printf("%d ->", root->item);
preorderTraversal(root->left);
preorderTraversal(root->right);
}
void postorderTraversal(struct node* root) {
if (root == NULL) return;
postorderTraversal(root->left);
postorderTraversal(root->right);
printf("%d ->", root->item);
}
struct node* createNode(value) {
struct node* newNode = malloc(sizeof(struct node));
newNode->item = value;
newNode->left = NULL;
newNode->right = NULL;
return newNode;
}
struct node* insertLeft(struct node* root, int value) {
root->left = createNode(value);
return root->left;
}
struct node* insertRight(struct node* root, int value) {
root->right = createNode(value);
return root->right;
}
int main() {
struct node* root = createNode(1);
insertLeft(root, 2);
insertRight(root, 3);
insertLeft(root->left, 4);
printf("Inorder traversal \n");
inorderTraversal(root);
AIM:
To write a program to perform operations on AVL trees.
ALGORITHM:
Step 1.Start the process.
Step 5. If the key does not match with the value in the root, search its
subtrees.
Step 6. If the value of the key is less than the root value, search the left
subtree
Step 7. If the value of the key is greater than the root value, search the
right subtree.
Step 8. If the key is not found in the tree, return unsuccessful search.
CODE:
#include <stdio.h>
#include <stdlib.h>
struct Node
{
int key;
struct Node *left;
struct Node *right;
int height;
};
return x;
}
y->left = x;
x->right = T2;
return y;
}
OUTPUT:
400 200 100 300 500 600