0% found this document useful (0 votes)
17 views7 pages

Practical No 3

The document contains C++ code for a BinaryTree class that allows for inserting nodes, performing in-order traversals, copying the tree, counting leaf and internal nodes, calculating height, mirroring the tree, and erasing the tree. The main function facilitates user input to build the tree and demonstrates the various functionalities of the BinaryTree class. The code includes error handling for invalid insertion choices and manages memory through a destructor.

Uploaded by

48Raj Patle
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
17 views7 pages

Practical No 3

The document contains C++ code for a BinaryTree class that allows for inserting nodes, performing in-order traversals, copying the tree, counting leaf and internal nodes, calculating height, mirroring the tree, and erasing the tree. The main function facilitates user input to build the tree and demonstrates the various functionalities of the BinaryTree class. The code includes error handling for invalid insertion choices and manages memory through a destructor.

Uploaded by

48Raj Patle
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

#include<iostream>

#include<vector>
#include<algorithm>
using namespace std;

class TreeNode
{
private:
int data;
TreeNode* left;
TreeNode* right;

public:
TreeNode() : data(0), left(NULL), right(NULL) {}
TreeNode(int value) : data(value), left(NULL), right(NULL) {}

friend class BinaryTree;


};

class BinaryTree
{
private:
TreeNode* root;
vector<int> leaf;
vector<int> internal;

TreeNode* insert(TreeNode* node, int value)


{
if (node == NULL)
{
return new TreeNode(value);
}

char choice;
cout << "Current node: " << node->data << ". Insert " << value << " to the left or
right? (l/r): ";
cin >> choice;

if (choice == 'l' || choice == 'L')


{
if (node->left == NULL)
{
node->left = new TreeNode(value);
}
else
{
insert(node->left, value);
}
}
else if (choice == 'r' || choice == 'R')
{
if (node->right == NULL)
{
node->right = new TreeNode(value);
}
else
{
insert(node->right, value);
}
}
else
{
cout << "Invalid choice! Please choose 'l' or 'r'.\n";
insert(node, value);
}

return node;
}

void inorderTraversal(TreeNode* node)


{
if (node == NULL) return;

inorderTraversal(node->left);
cout << node->data << " ";
inorderTraversal(node->right);
}

TreeNode* copyTree(TreeNode* node)


{
if (node == NULL)
return NULL;

TreeNode* newNode = new TreeNode(node->data);


newNode->left = copyTree(node->left);
newNode->right = copyTree(node->right);
return newNode;
}
void countLeavesAndInternal(TreeNode* node)
{
if (node == NULL) return;

if (node->left == NULL && node->right == NULL)


{
leaf.push_back(node->data);
}
else
{
internal.push_back(node->data);
}

countLeavesAndInternal(node->left);
countLeavesAndInternal(node->right);
}

void eraseTree(TreeNode* node)


{
if (node == NULL) return;

eraseTree(node->left);
eraseTree(node->right);
delete node;
}

int height(TreeNode* node)


{
if (node == NULL)
return -1;
int leftHeight = height(node->left);
int rightHeight = height(node->right);
return 1 + max(leftHeight, rightHeight);
}

void mirror(TreeNode* node)


{
if (node == NULL)
return;
swap(node->left, node->right);

mirror(node->left);
mirror(node->right);
}

public:
BinaryTree() : root(NULL) {}

void insert(int value)


{
root = insert(root, value);
}

void inorderTraversal()
{
inorderTraversal(root);
cout << endl;
}

BinaryTree copyTree()
{
BinaryTree newTree;
newTree.root = copyTree(root);
return newTree;
}

void countLeavesAndInternal()
{
leaf.clear();
internal.clear();
countLeavesAndInternal(root);
}

void eraseTree()
{
eraseTree(root);
root = NULL;
}

void printLeaves()
{
cout << "Leaf nodes: ";
for (int value : leaf)
{
cout << value << " ";
}
cout << endl;
}

void printInternal()
{
cout << "Internal nodes: ";
for (int value : internal)
{
cout << value << " ";
}
cout << endl;
}

int getHeight()
{
return height(root);
}

void mirrorTree()
{
mirror(root);
}

~BinaryTree()
{
eraseTree();
}
};

int main()
{
BinaryTree tree;

int value;
char cont = 'y';

while (cont == 'y' || cont == 'Y')


{
cout << "Enter the value to insert: ";
cin >> value;
tree.insert(value);

cout << "Do you want to add another node? (y/n): ";
cin >> cont;
}

cout << "\nIn-order traversal of the tree: ";


tree.inorderTraversal();

tree.countLeavesAndInternal();
tree.printLeaves();
tree.printInternal();

BinaryTree copiedTree = tree.copyTree();


cout << "In-order traversal of copied tree: ";
copiedTree.inorderTraversal();

cout << "Height of the tree: " << tree.getHeight() << endl;

tree.mirrorTree();
cout << "In-order traversal after mirroring the tree: ";
tree.inorderTraversal();

tree.eraseTree();
cout << "In-order traversal of erased tree: ";
tree.inorderTraversal();

return 0;
}

You might also like