22F-3575 - Emaan Mahdi - Lab No 10
22F-3575 - Emaan Mahdi - Lab No 10
22F-3575 - Emaan Mahdi - Lab No 10
DATA LAB 10
EMAAN MAHDI
22F-3575
BS (CS) | 4-B
TASK NO 1:
#include<iostream>
using namespace std;
class Node
{
public:
int data;
Node* left;
Node* right;
int height;
Node(int val) : data(val), left(nullptr), right(nullptr), height(1) {}
};
x->right = y;
y->left = T2;
return x;
}
Node* RotateLeft(Node* x)
{
Node* y = x->right;
Node* T2 = y->left;
y->left = x;
x->right = T2;
return y;
}
Node* insert(Node* node, int val)
{
if (node == nullptr)
{
return new Node(val);
}
if (val < node->data)
{
node->left = insert(node->left, val);
}
else if (val > node->data)
{
node->right = insert(node->right, val);
}
else
return node;
int bf = balanceFactor(node);
int bf = balanceFactor(node);
return false;
}
bool search(Node* node, int val)
{
if (node == nullptr)
return false;
if (val == node->data)
return true;
return node->data;
}
int find2ndMin(Node* node)
{
if (node == nullptr)
return -1;
return -1;
}
void printInorder(Node* node)
{
if (node == nullptr)
return;
printInorder(node->left);
cout << node->data << " ";
printInorder(node->right);
}
void printPreorder(Node* node)
{
if (node == nullptr)
return;
printPostorder(node->left);
printPostorder(node->right);
cout << node->data << " ";
}
int main()
{
Node* root = nullptr;
cout << "Search 25: " << search(root, 25) << endl;
system("pause");
return 0;
}
TASK NO 2:
#include <iostream>
using namespace std;
struct Node {
int key;
int height;
Node* left;
Node* right;
};
Node* newNode(int key) {
Node* node = new Node();
node->key = key;
node->height = 1;
node->left = node->right = nullptr;
return node;
}
int height(Node* node) {
if (node == nullptr)
return 0;
return node->height;
}
Node* rightRotate(Node* y) {
Node* x = y->left;
Node* T2 = x->right;
x->right = y;
y->left = T2;
return x;
}
Node* leftRotate(Node* x) {
Node* y = x->right;
Node* 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;
}
int getBalance(Node* node) {
if (node == nullptr)
return 0;
return height(node->left) - height(node->right);
}
Node* insert(Node* node, int key) {
if (node == nullptr)
return newNode(key);
inorder(node->left);
cout << node->key << " ";
inorder(node->right);
}
int main()
{
Node* root = nullptr;
root = insert(root, 13);
root = insert(root, 8);
root = insert(root, 5);
root = insert(root, 9);
root = insert(root, 4);
root = insert(root, 6);
root = insert(root, 12);
root = insert(root, 2);
root = insert(root, 1);
root = insert(root, 3);
inorder(root);
return 0;
}