0% found this document useful (0 votes)
18 views

Avl Tree Implementation 307

The document contains code for implementing an AVL tree to store stock prices dynamically for a stock market data system. The AVL tree allows efficient retrieval of the highest and lowest priced stocks by traversing the appropriate subtrees. The code includes functions for inserting, deleting, finding minimum/maximum nodes, and rebalancing the tree via left and right rotations to maintain the AVL tree balance property.

Uploaded by

Shyam Desai
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
18 views

Avl Tree Implementation 307

The document contains code for implementing an AVL tree to store stock prices dynamically for a stock market data system. The AVL tree allows efficient retrieval of the highest and lowest priced stocks by traversing the appropriate subtrees. The code includes functions for inserting, deleting, finding minimum/maximum nodes, and rebalancing the tree via left and right rotations to maintain the AVL tree balance property.

Uploaded by

Shyam Desai
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 15

SHYAM DESAI

ROLL NO 307
USN :01FE21BEC110
C DIV

AVL TREE IMPLEMENTATION

Problem Statement:
Consider a real-time stock market data system where you
need to keep track of the current stock prices and perform
efficient searches for finding the highest or lowest priced
stocks. Implement an AVL tree to store the stock prices, with
each node representing a stock and its associated price. The
tree should be dynamically updated as prices change, and you
should be able to quickly retrieve the highest or lowest priced
stock by traversing the appropriate subtree.

CODE:
#include <stdio.h>
#include <stdlib.h>

// Structure for a node in AVL tree


struct Node {
int stock; // Stock number
float price; // Stock price
struct Node* left; // Pointer to the left child
struct Node* right; // Pointer to the right child
int height; // Height of the node
};

// Function to get the height of a node


int getHeight(struct Node* node) {
if (node == NULL)
return 0;
return node->height;
}

// Function to get the maximum of two integers


int max(int a, int b) {
return (a > b) ? a : b;
}

// Function to create a new node with given stock and price


struct Node* createNode(int stock, float price) {
struct Node* newNode = (struct Node*)malloc(sizeof(struct
Node));
newNode->stock = stock;
newNode->price = price;
newNode->left = NULL;
newNode->right = NULL;
newNode->height = 1;
return newNode;
}

// Function to right rotate a subtree rooted with y


struct Node* rightRotate(struct Node* y) {
struct Node* x = y->left;
struct Node* T2 = x->right;

// Perform the rotation


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

// Update heights
y->height = max(getHeight(y->left), getHeight(y->right)) + 1;
x->height = max(getHeight(x->left), getHeight(x->right)) + 1;

return x;
}

// Function to left rotate a subtree rooted with x


struct Node* leftRotate(struct Node* x) {
struct Node* y = x->right;
struct Node* T2 = y->left;

// Perform the rotation


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

// Update heights
x->height = max(getHeight(x->left), getHeight(x->right)) + 1;
y->height = max(getHeight(y->left), getHeight(y->right)) + 1;

return y;
}

// Function to get the balance factor of a node


int getBalanceFactor(struct Node* node) {
if (node == NULL)
return 0;
return getHeight(node->left) - getHeight(node->right);
}

// Function to insert a node into the AVL tree


struct Node* insertNode(struct Node* node, int stock, float price) {
// Perform the normal BST insertion
if (node == NULL)
return createNode(stock, price);

if (price < node->price)


node->left = insertNode(node->left, stock, price);
else if (price > node->price)
node->right = insertNode(node->right, stock, price);
else // Duplicate prices are not allowed in this example
return node;

// Update the height of the current node


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

// Check the balance factor and rebalance if needed


int balanceFactor = getBalanceFactor(node);

// Left Left Case


if (balanceFactor > 1 && price < node->left->price)
return rightRotate(node);

// Right Right Case


if (balanceFactor < -1 && price > node->right->price)
return leftRotate(node);
// Left Right Case
if (balanceFactor > 1 && price > node->left->price) {
node->left = leftRotate(node->left);
return rightRotate(node);
}

// Right Left Case


if (balanceFactor < -1 && price < node->right->price) {
node->right = rightRotate(node->right);
return leftRotate(node);
}

return node;
}

// Function to find the node with the lowest price (leftmost node)
struct Node* findLowestPrice(struct Node* node) {
if (node == NULL || node->left == NULL)
return node;
return findLowestPrice(node->left);
}

// Function to find the node with the highest price (rightmost node)
struct Node* findHighestPrice(struct Node* node) {
if (node == NULL || node->right == NULL)
return node;
return findHighestPrice(node->right);
}

// Function to find and delete the node with the given price
struct Node* deleteNode(struct Node* root, float price) {
if (root == NULL)
return root;

if (price < root->price)


root->left = deleteNode(root->left, price);
else if (price > root->price)
root->right = deleteNode(root->right, price);
else {
// Node to be deleted is found

// Case 1: No child or only one child


if (root->left == NULL || root->right == NULL) {
struct Node* temp = root->left ? root->left : root->right;

// No child case
if (temp == NULL) {
temp = root;
root = NULL;
} else // One child case
*root = *temp; // Copy the contents of the non-empty child

free(temp);
} else {
// Case 2: Two children
struct Node* successor = findLowestPrice(root->right); // Find
the inorder successor (smallest in the right subtree)

// Copy the inorder successor's data to this node


root->stock = successor->stock;
root->price = successor->price;

// Delete the inorder successor


root->right = deleteNode(root->right, successor->price);
}
}

// If the tree had only one node, then return


if (root == NULL)
return root;
// Update the height of the current node
root->height = 1 + max(getHeight(root->left), getHeight(root-
>right));

// Check the balance factor and rebalance if needed


int balanceFactor = getBalanceFactor(root);

// Left Left Case


if (balanceFactor > 1 && getBalanceFactor(root->left) >= 0)
return rightRotate(root);

// Left Right Case


if (balanceFactor > 1 && getBalanceFactor(root->left) < 0) {
root->left = leftRotate(root->left);
return rightRotate(root);
}

// Right Right Case


if (balanceFactor < -1 && getBalanceFactor(root->right) <= 0)
return leftRotate(root);

// Right Left Case


if (balanceFactor < -1 && getBalanceFactor(root->right) > 0) {
root->right = rightRotate(root->right);
return leftRotate(root);
}

return root;
}

// Function to display the stock prices in ascending order (in-order


traversal)
void displayStockPrices(struct Node* node) {
if (node != NULL) {
displayStockPrices(node->left);
printf("Stock: %d, Price: %.2f\n", node->stock, node->price);
displayStockPrices(node->right);
}
}

// Function to get the stock count


int getStockCount(struct Node* node) {
if (node == NULL)
return 0;
return 1 + getStockCount(node->left) + getStockCount(node-
>right);
}
// Function to display the stock prices in descending order (reverse
in-order traversal)
void displayStockPricesDescending(struct Node* node) {
if (node != NULL) {
displayStockPricesDescending(node->right);
printf("Stock: %d, Price: %.2f\n", node->stock, node->price);
displayStockPricesDescending(node->left);
}
}

int main() {
struct Node* root = NULL;
int stock;
float price;
int numStocks;

printf("Enter the number of stocks: ");


scanf("%d", &numStocks);

printf("Enter the stock prices:\n");


for (int i = 0; i < numStocks; i++) {
printf("Stock %d price: ", i + 1);
scanf("%f", &price);
printf("Stock %d number: ", i + 1);
scanf("%d", &stock);
root = insertNode(root, stock, price);
}

// Displaying the stock prices in ascending order


printf("\nStock prices in ascending order:\n");
displayStockPrices(root);

// Displaying the stock prices in descending order


printf("\nStock prices in descending order:\n");
displayStockPricesDescending(root);

// Finding the lowest and highest priced stocks


struct Node* lowestStock = findLowestPrice(root);
struct Node* highestStock = findHighestPrice(root);

if (lowestStock != NULL)
printf("\nLowest priced stock: Stock %d, Price: %.2f\n",
lowestStock->stock, lowestStock->price);

if (highestStock != NULL)
printf("Highest priced stock: Stock %d, Price: %.2f\n",
highestStock->stock, highestStock->price);
// Deleting a node with a specific price
float deletePrice;
printf("\nEnter the price of the stock to delete: ");
scanf("%f", &deletePrice);
root = deleteNode(root, deletePrice);

// Displaying the stock prices after deletion


printf("\nStock prices after deletion:\n");
displayStockPrices(root);

// Displaying the number of stocks


int stockCount = getStockCount(root);
printf("\nNumber of stocks: %d\n", stockCount);

return 0;
}
OUTPUT SCREEN SHOT:

You might also like