0% found this document useful (0 votes)
8 views9 pages

Assignment 9

The document contains two C++ programs for binary tree operations. The first program constructs a binary tree from an array and checks if it is balanced, complete, full, or perfect. The second program implements a binary search tree with insertion, deletion, and traversal methods (inorder, preorder, postorder).

Uploaded by

aanya.10june
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)
8 views9 pages

Assignment 9

The document contains two C++ programs for binary tree operations. The first program constructs a binary tree from an array and checks if it is balanced, complete, full, or perfect. The second program implements a binary search tree with insertion, deletion, and traversal methods (inorder, preorder, postorder).

Uploaded by

aanya.10june
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/ 9

ASSIGNMENT-9

-Aanya saraswat
-102486005
-2F23

Q1
#include
<iostream>
#include
<queue>
#include
<cmath>

using namespace std;

struct Node
{ int data;
Node
*left;
Node
*right;
Node(int val) : data(val), left(nullptr), right(nullptr) {}
};

Node* insertLevelOrder(int arr[], int i, int


n) { Node* root = nullptr;
if (i < n) {
root = new Node(arr[i]);
root->left = insertLevelOrder(arr, 2 * i
+ 1, n); root->right =
insertLevelOrder(arr, 2 * i + 2, n);
}
return root;
}

int height(Node*
node) { if (!node)
return 0;
return 1 + max(height(node->left), height(node->right));
}

bool isBalanced(Node*
root) { if (!root)
return true;
int lh = height(root-
>left); int rh =
height(root->right);
return (abs(lh - rh) <= 1 && isBalanced(root->left) && isBalanced(root->right));
}
bool isCompleteUtil(Node* root, int index, int
count) { if (!root)
return true;
if (index >= count)
return false;
return (isCompleteUtil(root->left, 2 * index + 1,
count) && isCompleteUtil(root->right, 2 *
index + 2, count));
}

int countNodes(Node*
root) { if (!root)
return 0;
return 1 + countNodes(root->left) + countNodes(root->right);
}

bool isComplete(Node* root) {


int nodeCount = countNodes(root);
return isCompleteUtil(root, 0, nodeCount);
}

bool isFull(Node*
root) { if (!root)
return true;
if (!root->left && !root-
>right) return true;
if (root->left && root->right)
return (isFull(root->left) && isFull(root-
>right)); return false;
}

bool isPerfect(Node*
root) { int h =
height(root);
int count = countNodes(root);
return (count == pow(2, h) - 1 && isFull(root));
}

int main() {
int arr[] = {1, 2, 3, 4, 5, 6};
int n = sizeof(arr) / sizeof(arr[0]);
Node* root = insertLevelOrder(arr, 0, n);

cout << "Is Balanced: " << isBalanced(root)


<< endl; cout << "Is Complete: " <<
isComplete(root) << endl; cout << "Is Full: "
<< isFull(root) << endl;
cout << "Is Perfect: " << isPerfect(root) << endl;

int arr2[] = {1, 2, 3, 4, 5, 6, 7};


int n2 = sizeof(arr2) / sizeof(arr2[0]);
Node* root2 = insertLevelOrder(arr2, 0, n2);
cout << "\nTree 2:" << endl;
cout << "Is Balanced: " << isBalanced(root2)
<< endl; cout << "Is Complete: " <<
isComplete(root2) << endl; cout << "Is Full: "
<< isFull(root2) << endl;
cout << "Is Perfect: " << isPerfect(root2) << endl;

return 0;
}

Q2
#include

<iostream> using

namespace std;

struct Node
{ int data;
Node
*left;
Node
*right;
Node(int val) : data(val), left(nullptr), right(nullptr) {}
};

Node* insert(Node* root, int


data) { if (!root) {
return new Node(data);
}
if (data < root->data) {
root->left = insert(root->left, data);
} else if (data > root->data) {
root->right = insert(root->right, data);
}
return root;
}
void inorder(Node* root) {
if (root) {
inorder(root-
>left);
cout << root->data
<< " "; inorder(root-
>right);
}
}

void preorder(Node*
root) { if (root) {
cout << root->data
<< " "; preorder(root-
>left); preorder(root-
>right);
}
}

void postorder(Node*
root) { if (root) {
postorder(root->left);
postorder(root-
>right); cout << root-
>data << " ";
}
}

Node* minValueNode(Node* node)


{ Node* current = node;
while (current && current->left !=
nullptr) { current = current->left;
}
return current;
}

Node* deleteNode(Node* root, int


key) { if (!root) {
return root;
}
if (key < root->data) {
root->left = deleteNode(root->left, key);
} else if (key > root->data) {
root->right = deleteNode(root->right, key);
} else {
if (!root->left) {
Node* temp = root-
>right; delete root;
return temp;
} else if (!root->right)
{ Node* temp = root-
>left; delete root;
return temp;
}
Node* temp = minValueNode(root-
>right); root->data = temp->data;
root->right = deleteNode(root->right, temp->data);
}
return root;
}

int main() {
Node* root = nullptr;
root = insert(root,
50); root =
insert(root, 17); root
= insert(root, 72);
root = insert(root,
12); root =
insert(root, 23); root
= insert(root, 54);
root = insert(root,
76); root =
insert(root, 9); root
= insert(root, 14);
root = insert(root,
67);

cout << "Inorder Traversal:


"; inorder(root);
cout << endl;

cout << "Preorder


Traversal: "; preorder(root);
cout << endl;

cout << "Postorder Traversal: ";


postorder(root);
cout << endl;

int keyToDelete;
cout << "Enter the key to
delete: "; cin >> keyToDelete;

root = deleteNode(root, keyToDelete);

cout << "Inorder Traversal After


Deletion: "; inorder(root);
cout << endl;
return 0;
}

You might also like