write a program that read a list of names and telephone numbers from a text file
ID: 3636393 • Letter: W
Question
write a program that read a list of names and telephone numbers from a text file and inserts them into an AVL tree. Once the tree has built, present the user with a menu that allows him/her to search the list for a specified name, insert a new node, delete an existing name, or to print the entire phone list. At the end of the job, write the data in the list back into the file.If been doing this for weeks but still couldnt complete it. please help me~
Explanation / Answer
#include #include #include #include #include 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 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 coutRelated Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.