Family Tree Application - A multi-node tree

Printer-friendly versionPDF version

Here is another interesting application. I don't have Visual Studio installed to run and take a screenshot. The exe file I probably deleted back than. Should have kept it.

Anyways it was written with Visual Studio 5 if I remember correctly back in the good old University days. I never got the hang of mfc based applications, but somehow I wrapped my head around Dialog based applications, so this is written as a Dialog based app. The application was simply a multi-node tree, you could save it to disk and load it back from disk ( you don't really want to enter your family tree each time), and from what else I see, you could search the tree, create children nodes and sibling nodes. This was definitely semester 4. The main crux of business code is ofcourse the Tree.cpp. I hand the whole project as a zip file towards the end of this article. The tree.h file can be found in the zip file, the Tree.cpp I publish for young budding programmers. Feel free to use whatever you want to with permission or without permission, with attribution or without attribution. If you learn something thats good enough.

// Tree.cpp: implementation of the Tree class.
//
//////////////////////////////////////////////////////////////////////
 
#include "stdafx.h"
#include "Family tree.h"
#include "Tree.h"
#include <fstream.h>
 
#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif
 
//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////
 
Tree::Tree()
{
	root = 0 ;
}
 
Tree::~Tree()
{
 
}
 
 
Tree::Tree(CString myName,CString fname,CString mname)
{
	root-&gt;name = myName;
	root-&gt;mothersName = mname;
	root-&gt;fathersName = fname;
}
 
 
 
void Tree::saveTree(Tree::TreeNodePtr root)
{
	ofstream outTreeFile("tree.txt",ios::out);
	//write error msg
	if(!outTreeFile)
	{
 
	}
 
	for(int i=0;i<root->noOfChildrens;i++)
	{
		writeNode(root,outTreeFile);
		saveTree(root-&gt;childArray[i]);
	}
	outTreeFile.close();
}
 
 
 
void Tree::writeNode(Tree::TreeNodePtr root, ofstream &amp; outTreeFile)
{
	outTreeFile &lt;&lt; root-&gt;noOfChildrens &lt;&lt; ' ' ;		
	outTreeFile.write(root-&gt;name,(root-&gt;name).GetLength());
	outTreeFile.put('\n');
	outTreeFile.write(root-&gt;fathersName,(root-&gt;fathersName).GetLength());
	outTreeFile.put('\n');
	outTreeFile.write(root-&gt;mothersName,(root-&gt;mothersName).GetLength());
	outTreeFile.put('\n');
	outTreeFile.write(root-&gt;DOB,(root-&gt;DOB).GetLength());
	outTreeFile.put('\n');
	outTreeFile.write(root-&gt;gender,(root-&gt;gender).GetLength());
	outTreeFile.put('\n');
}
 
 
 
 
void Tree::readTree(Tree::TreeNodePtr root)
{
	ifstream inTreeFile("tree.txt",ios::in);
	//write error msg
	if(!inTreeFile)
	{
 
	}
	for(int i=0; inTreeFile.eof() || i<root->noOfChildrens; i++)
	{
		readNode(root,inTreeFile);
		readTree(root-&gt;childArray[i]);
	}
 
}
 
 
void Tree::readNode(Tree::TreeNodePtr root, ifstream &amp; inTreeFile)
{
	char * myname,
		 * fname,
		 * mname,
		 * dob,
		 * sex;
	int childrens;
	fname = dob ;
	inTreeFile &gt;&gt; childrens;
	inTreeFile.getline(myname,100);
	inTreeFile.getline(fname,100);
	inTreeFile.getline(mname,100);
	inTreeFile.getline(dob,100);
	inTreeFile.getline(sex,100);
	root = new Tree::TreeNode(myname,fname,mname,dob,sex,childrens);
 
}
 
Tree::TreeNodePtr Tree::search(Tree::TreeNode* parent,const CString &amp; key)
{	
	return searchAux(parent,root,key);
}
 
void Tree::editNode(Tree::TreeNodePtr currentptr,CString myname,CString fname,CString mname,CString DOB)
{
 
}
 
Tree::TreeNodePtr Tree::displayParent(const CString key)
{
	Tree::TreeNodePtr parent;
	search(parent,key);
	return parent;
}
 
void Tree::displaySpouse()
{
 
}
 
 
void Tree::displayChildren(const Tree::TreeNode parent)
{
	for(int i = 0; i &lt;= parent.noOfChildrens ; i++)
	{
		//process nodes.
	}
}
 
 
void Tree::createChild(CString myName,CString fname, CString mname, CString dob, CString sex)
{
	Tree::TreeNodePtr temp1;
	Tree::TreeNodePtr temp2;
	Tree::TreeNodePtr parent;
 
	temp1 = new Tree::TreeNode(currentptr,((currentptr-&gt;noOfChildrens)+1));
 
	int i=0;
	for(;i<currentptr->noOfChildrens;i++)
	{
		currentptr-&gt;childArray[i] = currentptr-&gt;childArray[i];
		currentptr-&gt;childArray[i] = NULL;
	}
				//Change
 
	temp1-&gt;childArray[i] = new Tree::TreeNode(myName,fname,mname,dob,sex);
 
			//Here values of text boxes to be entered 
	search(parent,currentptr-&gt;name);
	temp2 = parent;
	parent = temp1;
	delete temp2;
}
 
void Tree::createSibling(CString myName,CString fname,CString mname,CString dob,CString sex)
{
	Tree::TreeNodePtr parent,
					  temp, 
					  current;
	search(parent,currentptr-&gt;name);
	temp = current;
	current = parent;
	createChild(myName,fname,mname,dob,sex);
	current = temp;
}
 
void Tree::createParent(CString myName,CString fname,CString mname,CString dob,CString sex)
{
	createSibling(myName,fname,mname,dob,sex);
}
 
 
 
 
 
 
Tree::TreeNodePtr Tree::searchAux(Tree::TreeNodePtr parent,Tree::TreeNodePtr locptr, const CString key)
{	
	if(locptr == 0)
	{
		return NULL;
	}
 
	else 
	{
		parent = locptr;
		for(int i = 0 ; i&lt;= locptr-&gt;noOfChildrens ; i++)
		{
			if(locptr-&gt;name == key)
				break;
			searchAux(parent,locptr-&gt;childArray[i],key);
		}
		return locptr;
	}
 
}
 
</currentptr-></root-></root-></fstream.h>

 

Family tree(original).zip — Downloaded 19 times

Tags:

Top level category:

Add new comment