0% found this document useful (0 votes)
5 views8 pages

Binary Tree Program

The document presents a C++ program for a binary tree, including functions for inserting, deleting, and traversing nodes in in-order, pre-order, and post-order. It defines a Node structure and provides a menu-driven interface for user interaction. The program allows users to perform various operations on the binary tree through a console menu.

Uploaded by

Abbas Iqbal
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)
5 views8 pages

Binary Tree Program

The document presents a C++ program for a binary tree, including functions for inserting, deleting, and traversing nodes in in-order, pre-order, and post-order. It defines a Node structure and provides a menu-driven interface for user interaction. The program allows users to perform various operations on the binary tree through a console menu.

Uploaded by

Abbas Iqbal
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

Binary Tree Program

#include <iostream>
using namespace std;

// Define the structure of a tree node


struct Node {

int data;
Node* left;
Node* right;

Node(int value)
{
data = value;
left = NULL;
right = NULL;
}

};
// Function to insert a node into the binary tree
Node* insert(Node* root, int value) {
if (root == NULL) {
return new Node(value);
}
if (value < root->data) {
root->left = insert(root->left, value);
} else if (value > root->data) {
root->right = insert(root->right, value);
}
return root;
}

// Helper function to find the minimum value node


Node* findMin(Node* root) {
while (root && root->left != NULL) {
root = root->left;
}
return root;
}
// Function to delete a node from the binary tree
bool deleteNode(Node*& root, int value, bool& isDeleted) {
if (root == NULL) {
return false; // The value was not found
}
if (value < root->data)
{
return deleteNode(root->left, value, isDeleted);
}
else if (value > root->data)
{
return deleteNode(root->right, value, isDeleted);
}
else
{
isDeleted = true; // Mark that a node was found and will be
deleted

// Node with only one child or no child


if (root->left == NULL) {
Node* temp = root->right;
delete root;
root = temp;
return true;
} else if (root->right == NULL) {
Node* temp = root->left;
delete root;
root = temp;
return true;
}

// Node with two children: Get the inorder successor (smallest in


the right subtree)
Node* temp = findMin(root->right);
root->data = temp->data;
return deleteNode(root->right, temp->data, isDeleted);
}
}

// Function to perform in-order traversal


void inOrderTraversal(Node* root) {
if (root != NULL) {
inOrderTraversal(root->left);
cout << root->data << " ";
inOrderTraversal(root->right);
}
}

// Function to perform pre-order traversal


void preOrderTraversal(Node* root) {
if (root != NULL) {
cout << root->data << " ";
preOrderTraversal(root->left);
preOrderTraversal(root->right);
}
}

// Function to perform post-order traversal


void postOrderTraversal(Node* root) {
if (root != NULL) {
postOrderTraversal(root->left);
postOrderTraversal(root->right);
cout << root->data << " ";
}
}
int main() {
Node* root = NULL;
int choice, value;
bool isDeleted; // Declare outside the switch-case

while (true) {
cout << "\nMenu:\n";
cout << "1. Insert\n";
cout << "2. Delete\n";
cout << "3. In-order Traversal\n";
cout << "4. Pre-order Traversal\n";
cout << "5. Post-order Traversal\n";
cout << "6. Exit\n";
cout << "Enter your choice: ";
cin >> choice;

switch (choice) {
case 1:
cout << "Enter value to insert: ";
cin >> value;
root = insert(root, value);
cout << "Value " << value << " inserted successfully.\n";
break;
case 2:
cout << "Enter value to delete: ";
cin >> value;
isDeleted = false; // Reset before calling the function
deleteNode(root, value, isDeleted);
if (isDeleted) {
cout << "Value " << value << " deleted successfully.\n";
} else {
cout << "Value " << value << " does not exist in the tree.\n";
}
break;
case 3:
cout << "In-order Traversal: ";
inOrderTraversal(root);
cout << endl;
break;
case 4:
cout << "Pre-order Traversal: ";
preOrderTraversal(root);
cout << endl;
break;
case 5:
cout << "Post-order Traversal: ";
postOrderTraversal(root);
cout << endl;
break;
case 6:
return 0;
default:
cout << "Invalid choice. Please try again.\n";
}
}
}

You might also like