6 Replies - 349 Views - Last Post: 12 May 2019 - 06:34 PM Rate Topic: -----

#1 Ahmad1011   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 11-May 19

I have error in reading .dat which is based on Trees

Posted 11 May 2019 - 02:19 AM

Problem :-
1)I want to Build an application which populates (Builds) AVL Binary Tree and then saves all nodes data into a file data.dat by using object IO. Each node should have following data Patient Name,Father Name,Age,CNIC,Address,Disease,Doctor Name,Patient ID (Numeric).

NIC and patient ID should be unique. Choice should be given to the user either build BST on basis of NIC or Patient ID.

2)Second application should read data from file and again build AVL-BST on the basis of either NIC or Patient ID. Then following tasks should be performed on the basis of user choice.
 Show record of the patient having greatest key.
 Show record of the patient having least key.
 Show record of all patients with respect to the key in ascending order.
 Show record of all patients with respect to the key in Descending order.
 Delete any record with given key.
 Add new record.
 Search any record on the basis of key and also show that after how many traversals record is found.

Note: key may be NIC or Patient ID.

-I have built 1st app But Problem is in 2nd app in which I can't read that tree which I saved in app 1 ......(using Object I/O)
These are source codes

1st app COde
//AVL Trees Grand Finale
#include<iostream>
#include<deque>
#include<algorithm>
#include<deque> //For breathfirst 
#include<string>
#include<fstream>
#include<conio.h>

using namespace std;

char cho;

class Patient
{
private:
	Patient* left;
	Patient* right;
	unsigned long long Patient_CNIC;
	int Patient_id;
	int  age;
	string Patient_Name;
	string Father_Name;
	string Doctor_Name;
	string Disease;

	Patient* insert(Patient*, int);
	int height(Patient*);
	int diff(Patient*);
	Patient* balance(Patient*);
	Patient* singleLeft(Patient*);
	Patient* singleRight(Patient*);
	Patient* leftRight(Patient*);
	Patient* rightLeft(Patient*);
	void breathFirst(Patient*);
public:
	Patient* root;
	void Traverse()
	{
		cout << "\nTraversing BreathFirst\n";
		breathFirst(root);
	}

	bool Check_CNIC(unsigned long long int);
	void GetData(Patient* record)
	{

		if (cho == '1')	//CNIC is Key , so cnic_or_pid=P_id
		{
			cout << "\nEnter Patient's Id\n";
			cin >> record->Patient_id;
		}
		else //Pid is key , getting CNIC
		{
			bool c = false; unsigned long long int k;
			while (c == false)
			{
				cout << "\nEnter CNIC\n";
				cin >> record->Patient_CNIC;
				cout << "entered";
				k = record->Patient_CNIC;
				if (Check_CNIC(k) == true)
					c = true;
			}
		}
		cout << "\nEnter Patient's Age\n";
		cin >> record->age;

		cout << "\nEnter Patient's Name\n";
		cin.ignore();
		getline(cin, record->Patient_Name);

		cout << "\nEnter his Father's Name\n";
		getline(cin, record->Father_Name);

		cout << "\nEnter Doctors's Name\n";
		getline(cin, record->Doctor_Name);

		cout << "\nEnter Patient's Disease\n";
		getline(cin, record->Disease);
	}

	void insert1(int);
};

int main()
{	
	Patient o;
	cout << "sizeof" << sizeof(o);
	cho = '1';

	

	ifstream iff;
	iff.open("GRAND.DAT", ios::binary);
	if (!iff)
	{
		cout << "Error Opening file";
	}
	iff.read(reinterpret_cast<char*>(&o), sizeof(o));


	//o.Traverse();


	//o.showRoot();

	cout << "\n";
	system("pause");
	return 0;
}



Patient* Patient::insert(Patient * tree, int value)
{
	if (root == NULL)
	{
		root = new Patient;
		if (cho == '1')//CNIC
			root->Patient_CNIC = value;
		else
			root->Patient_id = value;
		GetData(root);
		//	cout << "\n" << root->key << " inserted as root\n";
		root->left = NULL;
		root->right = NULL;
		return root;
	}
	else if (cho == '1')//CNIC
	{
		if (value < tree->Patient_CNIC)
		{
			if (tree->left == NULL)
			{
				tree->left = new Patient;
				tree->left->Patient_CNIC = value;
				GetData(tree->left);
				tree->left = NULL;
				tree->right = NULL;
				cout << endl << tree->left->Patient_CNIC << " inserted at left of " << tree->Patient_CNIC << endl;
			}
			else
			{
				tree->left = insert(tree->left, value);
				cout << endl << "Now Checking Balance function of " << tree->Patient_CNIC;
				tree = balance(tree);
			}
		}
		else if (value > tree->Patient_CNIC)
		{
			cout << "into value > tree->Patient_CNIC";
			if (tree->right == NULL)
			{
				tree->right = new Patient;
				tree->right->Patient_CNIC = value;
				GetData(tree->right);
				tree->left = NULL;
				tree->right = NULL;
				cout << endl << tree->right->Patient_CNIC << " inserted at right of " << tree->Patient_CNIC << endl;
			}
			else
			{
				tree->right = insert(tree->right, value);
				cout << endl << "Now Checking Balance function of " << tree->Patient_id;
				tree = balance(tree);
			}
		}
	}
	else if (cho == '2') //Patient id
	{
		if (value < tree->Patient_id)
		{
			if (tree->left == NULL)
			{
				tree->left = new Patient;
				tree->left->Patient_id = value;
				GetData(tree->left);
				tree->left = NULL;
				tree->right = NULL;
				cout << endl << tree->left->Patient_id << " inserted at left of " << tree->Patient_id << endl;
			}
			else
			{
				tree->left = insert(tree->left, value);
				cout << endl << "Now Checking Balance function of " << tree->Patient_id;
				tree = balance(tree);
			}
		}
		else if (value > tree->Patient_id)
		{
			cout << "into value > tree->Patient_id";
			if (tree->right == NULL)
			{
				tree->right = new Patient;
				tree->right->Patient_id = value;
				GetData(tree->right);
				tree->left = NULL;
				tree->right = NULL;
				cout << endl << tree->right->Patient_id << " inserted at right of " << tree->Patient_id << endl;
			}
			else
			{
				tree->right = insert(tree->right, value);
				cout << endl << "Now Checking Balance function of " << tree->Patient_CNIC;
				tree = balance(tree);
			}
		}
	}
	else
		cout << "\nValue already exists\n";
	root = tree;
	return tree;
}
int Patient::height(Patient * tree)
{
	int lh, rh;
	if (tree == NULL)
		return 0;
	if (tree->left == NULL)
		lh = 0;
	else
		lh = height(tree->left) + 1;
	if (tree->right == NULL)
		rh = 0;
	else
		rh = height(tree->right) + 1;
	return max(lh, rh);
}
int Patient::diff(Patient * tree)
{
	return height(tree->left) - height(tree->right);
}
Patient* Patient::balance(Patient * tree)
{
	//cout << "\n I am into the balance Function of " << tree->key << " Sir !\n";
	int bfactor = diff(tree);
	if (bfactor > 1) //Left Skewed
	{
		//cout << "\nSir! " << tree->key << "is Left skewed\n";
		if (diff(tree->left) > 0)
		{
			//cout << "\nSir! " << tree->key << "is going into Single Right Rotation\n";
			tree = singleRight(tree);
			//cout << "AFter rotation tree is \n"; breathFirst(tree);
		}
		else
		{
			//cout << "\nSir! " << tree->key << "is going into Left Right Double Rotation\n";
			tree = leftRight(tree);
		}
	}
	else if (bfactor < -1) //Right Skewed
	{
		//cout << "\nSir! " << tree->key << "is Right skewed\n";
		if (diff(tree->right) < 0)
		{
			//cout << "\nSir! " << tree->key << "is going into Single Left Rotation\n";
			singleLeft(tree);
		}
		else
		{
			//cout << "\nSir! " << tree->key << "is going into Right Left Double Rotation\n";
			rightLeft(tree);
		}
	}
	else
		cout << endl << "Already balanced Sir ! \n";
	//cout << "After rotation tree is \n"; breathFirst(tree);
	return tree;
}
Patient* Patient::singleLeft(Patient * tree)
{
	Patient* temp;
	temp = tree->right;
	tree->right = temp->left;
	temp->right = tree;
	return tree;
}
Patient* Patient::singleRight(Patient * tree)
{
	Patient* temp;
	temp = tree->left;
	tree->left = temp->right;
	temp->right = tree;
	return temp;
}
Patient* Patient::leftRight(Patient * tree)
{
	tree->left = singleLeft(tree->left);
	tree = singleRight(tree);
	return tree;
}
Patient* Patient::rightLeft(Patient * tree)
{
	tree->right = singleRight(tree->right);
	tree = singleLeft(tree);
	return tree;
}
bool Patient::Check_CNIC(unsigned long long int nic)
{
	int digits = 0;
	while (nic != 0)
	{
		digits++;
		nic /= 10;
	}
	if (digits != 13)
	{
		cout << "Digits =" << digits;
		cout << "Wrong CNIC\nEnter again";
		cout << "\nEnter CNIC\n";
		return false;
	}
	return true;
}

void Patient::breathFirst(Patient* tree)
{
	deque<Patient*> dq;
	dq.push_back(tree);
	while (!dq.size() == 0)
	{
		if (dq.front()->left != NULL)
			dq.push_back(dq.front()->left);
		if (dq.front()->right != NULL)
			dq.push_back(dq.front()->right);
		if (cho == '1')
			cout << dq.front()->Patient_CNIC << endl;
		else
			cout << dq.front()->Patient_id << endl;

		dq.pop_front();
	}
}




and 2nd app code is
//AVL Trees Grand Finale
#include<iostream>
#include<deque>
#include<algorithm>
#include<deque> //For breathfirst 
#include<string>
#include<fstream>
#include<conio.h>

using namespace std;

char cho;

class Patient
{
private:
	Patient* left;
	Patient* right;
	unsigned long long Patient_CNIC;
	int Patient_id;
	int  age;
	string Patient_Name;
	string Father_Name;
	string Doctor_Name;
	string Disease;

	Patient* insert(Patient*, int);
	int height(Patient*);
	int diff(Patient*);
	Patient* balance(Patient*);
	Patient* singleLeft(Patient*);
	Patient* singleRight(Patient*);
	Patient* leftRight(Patient*);
	Patient* rightLeft(Patient*);
	void breathFirst(Patient*);
public:
	Patient* root;
	void Traverse()
	{
		cout << "\nTraversing BreathFirst\n";
		if (root == NULL)
		{
			cout << "Root hi NULL hai Yaar";
		}
		else
		{
			cout << root->age;
		}
	}

	bool Check_CNIC(unsigned long long int);
	void GetData(Patient* record)
	{

		if (cho == '1')	//CNIC is Key ,
		{
			cout << "\nEnter Patient's Id\n";
			cin >> record->Patient_id;
		}
		else //Pid is key , getting CNIC
		{
			bool c = false; unsigned long long int k;
			while (c == false)
			{
				cout << "\nEnter CNIC\n";
				cin >> record->Patient_CNIC;
				cout << "entered";
				k = record->Patient_CNIC;
				if (Check_CNIC(k) == true)
					c = true;
			}
		}
		cout << "\nEnter Patient's Age\n";
		cin >> record->age;

		cout << "\nEnter Patient's Name\n";
		cin.ignore();
		getline(cin, record->Patient_Name);

		cout << "\nEnter his Father's Name\n";
		getline(cin, record->Father_Name);

		cout << "\nEnter Doctors's Name\n";
		getline(cin, record->Doctor_Name);

		cout << "\nEnter Patient's Disease\n";
		getline(cin, record->Disease);
	}

	void insert1(int);
};

int main()
{

	Patient o;
	cout << "sizeof" << sizeof(o);
	cho = '1';

	cout << "g";

	ifstream iff;
	iff.open("GRAND.DAT", ios::binary);
	if (!iff)
	{
		cout << "Error Opening file";
	}

	iff.read(reinterpret_cast<char*>(&o), sizeof(o));

	o.Traverse();


	//o.showRoot();

	cout << "\n";
	system("pause");
	return 0;
}



Patient* Patient::insert(Patient* tree, int value)
{
	if (root == NULL)
	{
		root = new Patient;
		if (cho == '1')//CNIC
			root->Patient_CNIC = value;
		else
			root->Patient_id = value;
		GetData(root);
		//	cout << "\n" << root->key << " inserted as root\n";
		root->left = NULL;
		root->right = NULL;
		return root;
	}
	else if (cho == '1')//CNIC
	{
		if (value < tree->Patient_CNIC)
		{
			if (tree->left == NULL)
			{
				tree->left = new Patient;
				tree->left->Patient_CNIC = value;
				GetData(tree->left);
				tree->left = NULL;
				tree->right = NULL;
				cout << endl << tree->left->Patient_CNIC << " inserted at left of " << tree->Patient_CNIC << endl;
			}
			else
			{
				tree->left = insert(tree->left, value);
				cout << endl << "Now Checking Balance function of " << tree->Patient_CNIC;
				tree = balance(tree);
			}
		}
		else if (value > tree->Patient_CNIC)
		{
			cout << "into value > tree->Patient_CNIC";
			if (tree->right == NULL)
			{
				tree->right = new Patient;
				tree->right->Patient_CNIC = value;
				GetData(tree->right);
				tree->left = NULL;
				tree->right = NULL;
				cout << endl << tree->right->Patient_CNIC << " inserted at right of " << tree->Patient_CNIC << endl;
			}
			else
			{
				tree->right = insert(tree->right, value);
				cout << endl << "Now Checking Balance function of " << tree->Patient_id;
				tree = balance(tree);
			}
		}
	}
	else if (cho == '2') //Patient id
	{
		if (value < tree->Patient_id)
		{
			if (tree->left == NULL)
			{
				tree->left = new Patient;
				tree->left->Patient_id = value;
				GetData(tree->left);
				tree->left = NULL;
				tree->right = NULL;
				cout << endl << tree->left->Patient_id << " inserted at left of " << tree->Patient_id << endl;
			}
			else
			{
				tree->left = insert(tree->left, value);
				cout << endl << "Now Checking Balance function of " << tree->Patient_id;
				tree = balance(tree);
			}
		}
		else if (value > tree->Patient_id)
		{
			cout << "into value > tree->Patient_id";
			if (tree->right == NULL)
			{
				tree->right = new Patient;
				tree->right->Patient_id = value;
				GetData(tree->right);
				tree->left = NULL;
				tree->right = NULL;
				cout << endl << tree->right->Patient_id << " inserted at right of " << tree->Patient_id << endl;
			}
			else
			{
				tree->right = insert(tree->right, value);
				cout << endl << "Now Checking Balance function of " << tree->Patient_CNIC;
				tree = balance(tree);
			}
		}
	}
	else
		cout << "\nValue already exists\n";
	root = tree;
	return tree;
}
int Patient::height(Patient * tree)
{
	int lh, rh;
	if (tree == NULL)
		return 0;
	if (tree->left == NULL)
		lh = 0;
	else
		lh = height(tree->left) + 1;
	if (tree->right == NULL)
		rh = 0;
	else
		rh = height(tree->right) + 1;
	return max(lh, rh);
}
int Patient::diff(Patient * tree)
{
	return height(tree->left) - height(tree->right);
}
Patient* Patient::balance(Patient * tree)
{
	//cout << "\n I am into the balance Function of " << tree->key << " Sir !\n";
	int bfactor = diff(tree);
	if (bfactor > 1) //Left Skewed
	{
		//cout << "\nSir! " << tree->key << "is Left skewed\n";
		if (diff(tree->left) > 0)
		{
			//cout << "\nSir! " << tree->key << "is going into Single Right Rotation\n";
			tree = singleRight(tree);
			//cout << "AFter rotation tree is \n"; breathFirst(tree);
		}
		else
		{
			//cout << "\nSir! " << tree->key << "is going into Left Right Double Rotation\n";
			tree = leftRight(tree);
		}
	}
	else if (bfactor < -1) //Right Skewed
	{
		//cout << "\nSir! " << tree->key << "is Right skewed\n";
		if (diff(tree->right) < 0)
		{
			//cout << "\nSir! " << tree->key << "is going into Single Left Rotation\n";
			singleLeft(tree);
		}
		else
		{
			//cout << "\nSir! " << tree->key << "is going into Right Left Double Rotation\n";
			rightLeft(tree);
		}
	}
	else
		cout << endl << "Already balanced Sir ! \n";
	//cout << "After rotation tree is \n"; breathFirst(tree);
	return tree;
}
Patient* Patient::singleLeft(Patient * tree)
{
	Patient* temp;
	temp = tree->right;
	tree->right = temp->left;
	temp->right = tree;
	return tree;
}
Patient* Patient::singleRight(Patient * tree)
{
	Patient* temp;
	temp = tree->left;
	tree->left = temp->right;
	temp->right = tree;
	return temp;
}
Patient* Patient::leftRight(Patient * tree)
{
	tree->left = singleLeft(tree->left);
	tree = singleRight(tree);
	return tree;
}
Patient* Patient::rightLeft(Patient * tree)
{
	tree->right = singleRight(tree->right);
	tree = singleLeft(tree);
	return tree;
}
bool Patient::Check_CNIC(unsigned long long int nic)
{
	int digits = 0;
	while (nic != 0)
	{
		digits++;
		nic /= 10;
	}
	if (digits != 13)
	{
		cout << "Digits =" << digits;
		cout << "Wrong CNIC\nEnter again";
		cout << "\nEnter CNIC\n";
		return false;
	}
	return true;
}

void Patient::breathFirst(Patient * tree)
{
	deque<Patient*> dq;
	dq.push_back(tree);
	while (!dq.size() == 0)
	{
		if (dq.front()->left != NULL)
			dq.push_back(dq.front()->left);
		if (dq.front()->right != NULL)
			dq.push_back(dq.front()->right);
		if (cho == '1')
			cout << dq.front()->Patient_CNIC << endl;
		else
			cout << dq.front()->Patient_id << endl;

		dq.pop_front();
	}
}




Please help me.

Is This A Good Question/Topic? 0
  • +

Replies To: I have error in reading .dat which is based on Trees

#2 Salem_c   User is offline

  • void main'ers are DOOMED
  • member icon

Reputation: 2355
  • View blog
  • Posts: 4,474
  • Joined: 30-May 10

Re: I have error in reading .dat which is based on Trees

Posted 11 May 2019 - 04:41 AM

> iff.read(reinterpret_cast<char*>(&o), sizeof(o));
There is so much wrong with your post.

1. Your 300+ lines of code are essentially duplicates. Neither shows how you write the file.

2. If we believe the attempt at reading is indicative of you doing something like write(), then this is all wrong as well.

read() and write only work with PoD (Plain-old-Data). That is, char, int, long, float, double, arrays of those things, or structs of those things.

It does not work with pointers, or classes containing virtual functions, or classes like std::string.

Nor does it magically traverse recursive data structures like a Tree, following your left/right pointers.

Before getting to where you are now, you should have prototyped your idea to make sure it would work. Never take for granted that just trying something new will work as you expect. It might work, which is good.

But if it doesn't work, then you need to develop the sense to make a side project to specifically test your assumptions and find out what does and doesn't actually work. For one thing, the side project will be a lot smaller, focus on an actual issue, and be much more amenable to being posted on forums.

So, without further ado - this.
#include<iostream>
#include<string>
#include<cstring>
#include<fstream>
using namespace std;

struct foo {
    string  word;
    foo ( string word ) {
        this->word = word;
    }
};

struct bar {
    char word[100];
    bar ( const char *word ) {
        strncpy(this->word,word,sizeof(this->word));
    }
};

int main ( int argc, char *argv[] ) {
    const char *msg = "This is a long text string to test";
    if ( argc > 1 ) {
        cout << "Write Test\n";
        foo a(msg);
        bar b(msg);
        ofstream o1("a.dat",ios::binary);
        ofstream o2("b.dat",ios::binary);
        o1.write(reinterpret_cast<char*>(&a), sizeof(a));
        o2.write(reinterpret_cast<char*>(&B)/>/>, sizeof(B)/>/>);
    }
    else
    {
        cout << "Read Test\n";
        foo a("");
        bar b("");
        ifstream o1("a.dat",ios::binary);
        ifstream o2("b.dat",ios::binary);
        o1.read(reinterpret_cast<char*>(&a), sizeof(a));
        o2.read(reinterpret_cast<char*>(&B)/>/>, sizeof(B)/>/>);
        cout << "A=" << a.word << endl;
        cout << "B=" << b.word << endl;
    }
}


$ g++ -g -Wall bar.cpp
$ ./a.out 1
Write Test
$ hd a.dat
00000000  60 a0 79 00 00 00 00 00  22 00 00 00 00 00 00 00  |`.y.....".......|
00000010  22 00 00 00 00 00 00 00  b0 b5 ce 56 ff 7f 00 00  |"..........V....|
00000020
$ hd b.dat
00000000  54 68 69 73 20 69 73 20  61 20 6c 6f 6e 67 20 74  |This is a long t|
00000010  65 78 74 20 73 74 72 69  6e 67 20 74 6f 20 74 65  |ext string to te|
00000020  73 74 00 00 00 00 00 00  00 00 00 00 00 00 00 00  |st..............|
00000030  00 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00  |................|
*
00000064


Notice how the std::string, when saved as a bit-copy, does NOT contain the string itself.

On a side note, your class Patient should just contain the patient data.
Use a separate Tree class which organises all your patients into a composite data structure.
class TreeOfP {
    Patient *patient;
    TreeOfP *left;
    TreeOfP *right;
};


This post has been edited by Salem_c: 11 May 2019 - 04:42 AM

Was This Post Helpful? 2
  • +
  • -

#3 Ahmad1011   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 11-May 19

Re: I have error in reading .dat which is based on Trees

Posted 11 May 2019 - 08:09 AM

Very Good Bro....I was confused on this that whether pointers or trees can be saved to .dat or not

I am very thankful to you dear............
Was This Post Helpful? 0
  • +
  • -

#4 Ahmad1011   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 11-May 19

Re: I have error in reading .dat which is based on Trees

Posted 12 May 2019 - 10:23 AM

I have a Tree using Class Patient_Tree.Its an AVL Tree which takes data and stores them into tress...

I want to store all nodes data into .dat using object I/O.Since read() and write only work with PoD Plain-old-Data, It does not work with pointers, or classes containing virtual functions, or classes like std::string.Nor does it magically traverse recursive data structures like a Tree, following your left/right pointers.

My problem is After saving taking and saving all nodes data into a Tree , whenever I want to save these nodes data by traversing through tree and storing them in Array of Object of 2nd class . Array of Object takes values in function but gives garbage value in main and thus cannnot be able to store in files in main...

This is Tree
//AVL Trees Grand Finale
#include<iostream>
#include<deque>
#include<algorithm>
#include<deque> //For breathfirst 
#include<string>
#include<fstream>
#include<conio.h>
#include<cstring>

using namespace std;

char cho;

const int l = 35;
const int N = 2;

class PatientsData
{
public:
	unsigned long long Patient_CNIC;
	int Patient_id;
	int  age;
	char Diseases[l];
};

class Patient_Tree
{
private:
	Patient_Tree* left;
	Patient_Tree* right;
	int Patient_id;
	int  age;
	char Diseasec[l];
	Patient_Tree* insert(Patient_Tree*, int);
	void StoreNodes(Patient_Tree*);
public:
	deque<Patient_Tree*> dq2;
	PatientsData Objarr[N];
	Patient_Tree* root = NULL;
	void Store()
	{
		StoreNodes(root);
	}
	void GetData(Patient_Tree* record)
	{
		cout << "\nEnter Patient's Age\n";
		cin >> record->age;
		cout << "\nEnter Patient's Disease\n";
		//getline(cin, record->Disease);
		cin.ignore();
		cin.get(record->Diseasec, l);
	}
	void insert1(int);
};
void Patient_Tree::insert1(int value)
{
	insert(root, value);
}

int main()
{
	Patient_Tree o;
	PatientsData Objarr[N];

	cout << "\nEnter " << N << " Records";
	for (unsigned short int i = 0; i < N; i++)
	{	
		int temp;
		cout << "\nEnter Patient Id Bro\n";
		cin >> temp;
		o.insert1(temp);
	}
	o.Store();
	//I have Stores values in ObjArr but still it is giving garbage values 
	cout << Objarr[0].age;
	cout << "\n";
	system("pause");
	return 0;
}
Patient_Tree* Patient_Tree::insert(Patient_Tree * tree, int value)
{
	if (root == NULL)
	{
		root = new Patient_Tree;
		root->Patient_id = value;
		GetData(root);
		root->left = NULL;
		root->right = NULL;
		return root;
	}
	else if (value < tree->Patient_id)
	{
		if (tree->left == NULL)
		{
			tree->left = new Patient_Tree;
			tree->left->Patient_id = value;
			GetData(tree->left);
		}
		else
			tree->left = insert(tree->left, value);
	}
	else if (value > tree->Patient_id)
	{
			if (tree->right == NULL)
			{
				tree->right = new Patient_Tree;
				tree->right->Patient_id = value;
				GetData(tree->right);
			}
			else
				tree->right = insert(tree->right, value);
	}
	else
		cout << "\nValue already exists\n";	root= tree;	return tree;
}
void Patient_Tree::StoreNodes(Patient_Tree *tree)
{
	deque<Patient_Tree*> dq, dq2;	dq.push_back(tree); dq2.push_back(tree);
	while (!dq.size() == 0)
	{
		if (dq.front()->left != NULL)
		{
			dq.push_back(dq.front()->left);		dq2.push_back(dq.back());
		}
		if (dq.front()->right != NULL)
		{
			dq.push_back(dq.front()->right);	dq2.push_back(dq.back());
		}
			cout << dq.front()->Patient_id << endl;
		dq.pop_front();
	}
	//All Tree data saved into Deque 2 
	//Now Saving All nodes data from Deque 2  to Objarr(Object Array)
	for (int i = 0; i < N; i++)
	{
		cout << "dq2.frony()->age = " << dq2.front()->age;
		Objarr[i].age = dq2.front()->age;
		cout << endl << Objarr[i].age;
		Objarr[i].Diseases[l] = dq2.front()->Diseasec[l];
		dq2.pop_front();
	}
}



Was This Post Helpful? 0
  • +
  • -

#5 jimblumberg   User is offline

  • member icon

Reputation: 5733
  • View blog
  • Posts: 17,568
  • Joined: 25-December 09

Re: I have error in reading .dat which is based on Trees

Posted 12 May 2019 - 10:45 AM

Perhaps you should think about increasing your compiler warning level, and fix all warnings and errors.

||=== Build: Debug in homework (compiler: gcc-8) ===|
/main.cpp||In member function ‘Patient_Tree* Patient_Tree::insert(Patient_Tree*, int)’:|
/main.cpp|115|warning: this ‘else’ clause does not guard... [-Wmisleading-indentation]|
/main.cpp|116|note: ...this statement, but the latter is misleadingly indented as if it were guarded by the ‘else’|
/main.cpp||In member function ‘void Patient_Tree::StoreNodes(Patient_Tree*)’:|
/main.cpp|120|warning: declaration of ‘dq2’ shadows a member of ‘Patient_Tree’ [-Wshadow]|
/main.cpp|38|note: shadowed declaration is here|
/main.cpp||In function ‘int main()’:|
/main.cpp|76|warning: ‘Objarr[0].PatientsData::age’ may be used uninitialized in this function [-Wmaybe-uninitialized]|
||=== Build finished: 0 error(s), 3 warning(s) (0 minute(s), 1 second(s)) ===|




Jim
Was This Post Helpful? 0
  • +
  • -

#6 Salem_c   User is offline

  • void main'ers are DOOMED
  • member icon

Reputation: 2355
  • View blog
  • Posts: 4,474
  • Joined: 30-May 10

Re: I have error in reading .dat which is based on Trees

Posted 12 May 2019 - 01:24 PM

032 int Patient_id;
033 int age;
034 char Diseasec[l];
Why aren't these just an instance of PatientsData?

039 PatientsData Objarr[N];
You have exactly one PatientsData in each node.

040 Patient_Tree* root = NULL;
What's this for?

Writing should be something like
void writeTree(Patient_Tree *t) {
  if ( !t ) return;
  write(t->data);  // your favourite file API
  writeTree(t->left);
  writeTree(t->right);
}


Was This Post Helpful? 0
  • +
  • -

#7 Skydiver   User is offline

  • Code herder
  • member icon

Reputation: 6968
  • View blog
  • Posts: 23,678
  • Joined: 05-May 12

Re: I have error in reading .dat which is based on Trees

Posted 12 May 2019 - 06:34 PM

You already have a thread about this same topic/code base. Merging threads...
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1