"write a program that reads a list of names and telephone numbers from a text file and inserts them into an AVL tree showing in order and pre order traversal..once the tree has been built,present the user with a menu that allows huim or her to search the list for a specified name,insert a new name,delete an exixting name or prInt the entire phone list.At the end of the job write the data in the list back to the file.Test your program with atleast 10 names."
this is a college project i've been working on..i have managed to work on the 1st half of the program ,where in a text file acts as an input to create an AVL tree.The code for this is attached.The problem is with building the menu for the user.I am really finding it very difficult to perform any update (delete,search etc) on the file content.(i know veryyyy little about file handling in c++ :( )
PLEASE if any one could help me with the rest of the code i'll be so thankful!!
#include <cstdlib>
#include <iostream>
#include <fstream>
#include <string>
#include <algorithm>
using namespace std;
struct AVLNode
{
string Name;
int Balance;
AVLNode *LeftChild;
AVLNode *RightChild;
};
AVLNode *InputData(ifstream &);
AVLNode *InsertData(AVLNode *, AVLNode *);
int FindBalance(AVLNode *);
void Traversal(AVLNode *);
AVLNode *LeftLeft(AVLNode *);
AVLNode *RightRight(AVLNode *);
AVLNode *LeftRight(AVLNode *);
AVLNode *RightLeft(AVLNode *);
int main()
{
ifstream CheckFile; //ifstream checks for file existence
char inputFileName[]="file.txt";
CheckFile.open(inputFileName); //attempts to open read file, and tests for existence
if(!CheckFile)
cout << "Input file does not exist." << endl << endl;
else
cout <<"file opend"<<endl;
AVLNode *head = InputData(CheckFile);
cout << "taversal:\n\n";
Traversal(head);
//cout << endl;
system("PAUSE");
return EXIT_SUCCESS;
}
AVLNode *InputData(ifstream &InFile)
{
AVLNode *Root = NULL;
AVLNode *Leaf;
while(!InFile.eof())
{
Leaf = new AVLNode;
getline(InFile, Leaf->Name);
Leaf->Balance = 0;
Leaf->RightChild = Leaf->LeftChild = NULL;
if(Root == NULL)
Root = Leaf;
Root = InsertData(Leaf, Root);
}
return Root;
}
AVLNode *InsertData(AVLNode *Leaf, AVLNode *Root)
{
if(Root == NULL)
return Leaf;
else if(Leaf->Name < Root->Name)
{
Root->LeftChild = InsertData(Leaf, Root->LeftChild);
if(FindBalance(Root->LeftChild) - FindBalance(Root->RightChild) == 2)
{
if(Leaf->Name < Root->LeftChild->Name)
Root = LeftLeft(Root);
else
Root = LeftRight(Root);
}
}
else if(Leaf->Name > Root->Name)
{
Root->RightChild = InsertData(Leaf, Root->RightChild);
if(FindBalance(Root->RightChild) - FindBalance(Root->LeftChild) == 2)
{
if(Leaf->Name > Root->RightChild->Name)
Root = RightRight(Root);
else
Root = RightLeft(Root);
}
}
Root->Balance = max(FindBalance(Root->LeftChild), FindBalance(Root->RightChild)) + 1;
return Root;
}
int FindBalance(AVLNode *Root)
{
if(Root == NULL)
return -1;
else
return Root->Balance;
}
AVLNode *LeftLeft(AVLNode *Rotate)
{
AVLNode *Pivot = Rotate->LeftChild;
Rotate->LeftChild = Pivot->RightChild;
Pivot->RightChild = Rotate;
Rotate->Balance = max(FindBalance(Rotate->LeftChild), FindBalance(Rotate->RightChild)) + 1;
Pivot->Balance = max(FindBalance(Pivot->LeftChild), FindBalance(Rotate->RightChild)) + 1;
return Pivot;
}
AVLNode *RightRight(AVLNode *Rotate)
{
AVLNode *Pivot = Rotate->RightChild;
Rotate->RightChild = Pivot->LeftChild;
Pivot->LeftChild = Rotate;
Rotate->Balance = max(FindBalance(Rotate->LeftChild), FindBalance(Rotate->RightChild)) + 1;
Pivot->Balance = max(FindBalance(Pivot->RightChild), FindBalance(Rotate->LeftChild)) + 1;
return Pivot;
}
AVLNode *LeftRight(AVLNode *RotateTop)
{
RotateTop->LeftChild = RightRight(RotateTop->LeftChild);
return LeftLeft(RotateTop);
}
AVLNode *RightLeft(AVLNode *RotateTop)
{
RotateTop->RightChild = LeftLeft(RotateTop->RightChild);
return RightRight(RotateTop);
}
void Traversal(AVLNode *Root)
{
AVLNode *temp;
if(Root != NULL)
{
Traversal(Root->LeftChild); // print left subtree
cout << "(" << Root->Name << ", "; // print this node
if(Root->LeftChild == NULL)
cout << "NULL, ";
else
{
temp = Root->LeftChild;
cout << temp->Name << ", ";
}
if(Root->RightChild == NULL)
cout << "NULL, ";
else
{
temp = Root->RightChild;
cout << temp->Name << ", ";
}
int temp1 = (FindBalance(Root->RightChild) - FindBalance(Root->LeftChild));
cout << temp1 << ")" << endl;
Traversal(Root->RightChild); // print right subtree
}
return;
}