Question: Below is my code for Double linked list embedded into a Binary search tree in c++. I can't get anything after: //while (getline(inputFile, line)) //
Below is my code for Double linked list embedded into a Binary search tree in c++. I can't get anything after:
//while (getline(inputFile, line))
// {
//
num = stoi(line);
// new_node->data = num;
// if (root == NULL)
to run. Please Help I am trying to be able to put insert a file that contains the following:
7711,6837,2525,5542,230,1674,4456,2742,5492,7456,6626,1998,3139,6167,4371,6540,3420,1068,8863,6438,3429,9465,6147,7448,8781,4959,5797,8730,1883,7676,5751,7481,2979,2759,2278,7200,6876,1916,1701,4467,7730,4154,8826,8495,5765,8701,1894,8450,6157,1419,9909,8512,8848,7141,1197,9604,2512,4328,5373,1150,6500,9624,6202,9642,7172,9625,8344,7655,6199,2946,8144,8227,1573,3627,6875,1275,7355,4870,6713,6684,5696,9814,7867,4839,6296,5122,7378,6176,4146,9877,1565,5054,5605,9464,7271,9756,8268,2947,3044,4106,8089,5876,8077,5616,5397,5811,6688,5097,8402,5457,2583,1789,6357,5271,3411,2536,5244,6853,1326,8597,7529,2714,9728,3717,3509,6593,2293,6366,6960,2886,8608,4274,9268,2497,1631,6638,7557,6517,1257,9924,9365,3030,3760,4841,7669,4646,7367,8757,1108,2884,9486,3926,7775,6860,6996,5330,8655,8036,4176,6221
into the Binary Search tree.
#include
#include
#include
#include
using namespace std;
typedef struct BST
{
int data;
struct BST *left, *right;
} node;
class Binary_Tree
{
public:
void insert(node *root, node *new_node)
{
if (new_node->data < root->data)
{
if (root->left == NULL)
root->left = new_node;
else
insert(root->left, new_node);
}
if (new_node->data > root->data)
{
if (root->right == NULL)
root->right = new_node;
else
insert(root->right, new_node);
}
}
public:
node * search(node *root, int key, node **parent)
{
node *temp;
temp = root;
while (temp != NULL)
{
if (temp->data == key)
{
cout << " The Element " << temp->data << "is Present" << endl;
return temp;
}
*parent = temp;
if (temp->data > key)
temp = temp->left;
else
temp = temp->right;
}
return NULL;
}
public:
void inorder(node *temp)
{
if (temp != NULL)
{
inorder(temp->left);
cout << "\t" << temp->data;
inorder(temp->right);
}
}
public:
node * get_node()
{
node *temp;
temp = (node *)malloc(sizeof(node));
temp->left = NULL;
temp->right = NULL;
return temp;
}
};
int main()
{
Binary_Tree obj;
int option;
//char ans = 'N';
int key;
node *new_node, *root, *tmp, *parent;
node *get_node();
root = NULL;
//input from number file
ifstream inputFile;
string line;
int num;
inputFile.open("C:/Users/Jake/Downloads/random_input");
if (inputFile.)
{
while (getline(inputFile, line))
{
num = stoi(line);
new_node->data = num;
if (root == NULL)
root = new_node;
else
obj.insert(root, new_node);
cout<<"num: " + num;
}
}
/*cout<<"------------------------------------------- "<
cout<<" **** Program For Binary Search Tree **** "<
while(1)
{
cout<<" 1.Insert The Element";
cout<<" 2.Search The Element";
cout<<" 3.Print Tree in inorder:";
cout<<" 4.Exit";
cout<<" Select your choice :";
cin>>option;
switch (option)
{
case 1:
do
{
new_node = obj.get_node();
cout<<" Enter The Element";
cin>>new_node->data;
if (root == NULL)
root = new_node;
else
obj.insert(root, new_node);
cout<<" Do you Want To insert More Elements?(y/n)";
ans = getch();
} while (ans == 'y');
break;
case 2:
cout<<" Enter Element to be searched :";
cin>>key;
tmp=obj.search(root, key, &parent);
cout<<" Parent of node " break; case 3: if (root == NULL) { cout<<" Tree Is Empty"< } else { cout<<" The Inorder display : "; obj.inorder(root); } break; case 4: exit(0); } }*/ }
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
