0% found this document useful (0 votes)
30 views6 pages

Wa0020.

The document discusses two problems: 1. Developing a C program to create a binary search tree from elements and perform a search operation to find an element. The program is provided. 2. Developing a C program to perform operations on an AVL tree including creation, insertion, and searching. The program for these AVL tree operations is provided.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
30 views6 pages

Wa0020.

The document discusses two problems: 1. Developing a C program to create a binary search tree from elements and perform a search operation to find an element. The program is provided. 2. Developing a C program to perform operations on an AVL tree including creation, insertion, and searching. The program for these AVL tree operations is provided.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 6

WEEK-10: Binary tree,BTree,b+Tree,AVL tree,Red black Tree

. Date :
27/12/23

Name:Likhitha Reddy

roll no : 22r21a0424

section : ece A

1.problem ststement

Develop a C Program to Create Binary search tree with the following elements: 78,
54,34,90,11,56,98,101,44,13,35,87 and perform search operation to search an element 101 .

program

#include <stdio.h>

#include <stdlib.h>

struct Node {

int data;

struct Node* left;

struct Node* right;

};

struct Node* createNode(int value) {

struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));

newNode->data = value;

newNode->left = NULL;

newNode->right = NULL;

return newNode;

struct Node* insert(struct Node* root, int value) {

if (root == NULL)

return createNode(value);
if (value < root->data)

root->left = insert(root->left, value);

else if (value > root->data)

root->right = insert(root->right, value);

return root;

struct Node* search(struct Node* root, int value) {

if (root == NULL || root->data == value)

return root;

if (value < root->data)

return search(root->left, value);

else

return search(root->right, value);

int main() {

struct Node* root = NULL;

int elements[] = {78, 54, 34, 90, 11, 56, 98, 101, 44, 13, 35, 87};

int numElements = sizeof(elements) / sizeof(elements[0]);

for (int i = 0; i < numElements; i++) {

root = insert(root, elements[i]);

int searchElement = 101;

struct Node* result = search(root, searchElement);

if (result != NULL)

printf("Element %d found .\n", searchElement);

else

printf("Element %d not found .\n", searchElement);

return 0;

}
4.problem statement
Develop a C program to perform the following operations on AVL tree

Create AVL tree

Insertion of elements into AVL tree

Searching of an element from AVL tree

program
#include <stdio.h>

#include <stdlib.h>

struct Node {

int key, height;

struct Node *left, *right;

};

int max(int a, int b) {

return (a > b) ? a : b;

int height(struct Node *node) {

return (node == NULL) ? 0 : node->height;

struct Node *newNode(int key) {

struct Node *node = (struct Node *)malloc(sizeof(struct Node));

node->key = key;

node->left = node->right = NULL;


node->height = 1;

return node;

struct Node *rightRotate(struct Node *y) {

struct Node *x = y->left, *T2 = x->right;

x->right = y, y->left = T2;

y->height = max(height(y->left), height(y->right)) + 1;

x->height = max(height(x->left), height(x->right)) + 1;

return x;

struct Node *leftRotate(struct Node *x) {

struct Node *y = x->right, *T2 = y->left;

y->left = x, x->right = T2;

x->height = max(height(x->left), height(x->right)) + 1;

y->height = max(height(y->left), height(y->right)) + 1;

return y;

struct Node *insert(struct Node *node, int key) {

if (node == NULL) return newNode(key);

if (key < node->key) node->left = insert(node->left, key);

else if (key > node->key) node->right = insert(node->right, key);

else return node;

node->height = 1 + max(height(node->left), height(node->right));

int balance = height(node->left) - height(node->right);

if (balance > 1 && key < node->left->key) return rightRotate(node);

if (balance < -1 && key > node->right->key) return leftRotate(node);

if (balance > 1 && key > node->left->key) {

node->left = leftRotate(node->left);

return rightRotate(node);
}

if (balance < -1 && key < node->right->key) {

node->right = rightRotate(node->right);

return leftRotate(node);

return node;

struct Node *search(struct Node *root, int key) {

if (root == NULL || root->key == key) return root;

if (root->key < key) return search(root->right, key);

else return search(root->left, key);

int main() {

struct Node *root = NULL;

root = insert(root, 10);

root = insert(root, 20);

root = insert(root, 30);

root = insert(root, 40);

root = insert(root, 50);

root = insert(root, 25);

int searchKey = 30;

struct Node *searchResult = search(root, searchKey);

if (searchResult != NULL)

printf("Key %d found in AVL tree.\n", searchKey);

else

printf("Key %d not found in AVL tree.\n", searchKey);

return 0;

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy