0% found this document useful (0 votes)
2 views5 pages

Q. Binary Search Tree.: Using

The document presents a C++ implementation of a Binary Search Tree (BST) with functions for creating nodes, inserting values, and performing an in-order traversal. It initializes a BST with specific values and allows the user to insert new values and print the in-order traversal through a command-line interface. The output demonstrates the in-order traversal before and after inserting new values.

Uploaded by

roshnising8892
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)
2 views5 pages

Q. Binary Search Tree.: Using

The document presents a C++ implementation of a Binary Search Tree (BST) with functions for creating nodes, inserting values, and performing an in-order traversal. It initializes a BST with specific values and allows the user to insert new values and print the in-order traversal through a command-line interface. The output demonstrates the in-order traversal before and after inserting new values.

Uploaded by

roshnising8892
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/ 5

Q. Binary Search Tree.

#include <iostream>
using namespace std;

struct BinaryTree
{
int data;
BinaryTree *left;
BinaryTree *right;
};

BinaryTree *NewNode, *root, *temp;

/*

{
n1
/ \
n2 n3
/ \
n4 n5
}

*/

void InOrder(BinaryTree *temp)


{
if (temp == nullptr)
{
return;
}

InOrder(temp->left);
cout << temp->data << " ";
InOrder(temp->right);
}

BinaryTree *NewNodeCreation(int val)


{
NewNode = new BinaryTree();
NewNode->data = val;
NewNode->left = nullptr;
NewNode->right = nullptr;
return NewNode;
}

void Insert(BinaryTree *temp)


{
if (temp == nullptr)
{
return;
}

if (temp->data > NewNode->data)


{
if (temp->left != nullptr)
{
Insert(temp->left);
return;
}
else
{
temp->left = NewNode;
return;
}
}

if (temp->data < NewNode->data)


{
if (temp->right != nullptr)
{
Insert(temp->right);
return;
}
else
{
temp->right = NewNode;
return;
}
}
}

int main()
{
int i = 1, val;
root = NewNodeCreation(10);
root->left = NewNodeCreation(4);
root->right = NewNodeCreation(12);
root->left->left = NewNodeCreation(2);
root->left->right = NewNodeCreation(5);

cout << "Inorder :- ";


InOrder(root);
cout << endl;

while (i != 0)
{
cout << "Enter Choice :\n0. End\n1. Insert\n2. Print Inorder\nEnter :
";
cin >> i;
switch (i)
{
case 1:
{
cout << "Enter data : ";
cin >> val;
NewNode = NewNodeCreation(val);
Insert(root);
break;
}
case 2:
{
cout << "Inorder :- ";
InOrder(root);
cout << endl;
break;
}
case 0:
{
break;
}
default:
cout<<"Invadlid.";
break;
}
}

return 0;
}

Output:

Inorder :- 2 4 5 10 12

Enter Choice :

0. End

1. Insert
2. Print Inorder

Enter : 1

Enter data : 3

Enter Choice :

0. End

1. Insert

2. Print Inorder

Enter : 2

Inorder :- 2 3 4 5 10 12

Enter Choice :

0. End

1. Insert

2. Print Inorder

Enter : 1

Enter data : 11

Enter Choice :

0. End

1. Insert

2. Print Inorder

Enter : 2

Inorder :- 2 3 4 5 10 11 12

Enter Choice :

0. End

1. Insert

2. Print Inorder

Enter : 1

Enter data : 15

Enter Choice :

0. End

1. Insert

2. Print Inorder
Enter : 2

Inorder :- 2 3 4 5 10 11 12 15

Enter Choice :

0. End

1. Insert

2. Print Inorder

Enter : 1

Enter data : 6

Enter Choice :

0. End

1. Insert

2. Print Inorder

Enter : 2

Inorder :- 2 3 4 5 6 10 11 12 15

Enter Choice :

0. End

1. Insert

2. Print Inorder

Enter : 0

You might also like