gudali 0 Newbie Poster

I have a doubt regarding avl trees.i got a part of this code from the internet.But this code was mde to use with integer input.But what i need to do is to insert strings which are alphanumeric.I am using strcmp() for insertion..but the output differs from the actual one.. Can anyone help me with that and any ideas please.....?

#include <cstdlib>
#include <iostream>
#include <fstream>
#include <string>
#include <algorithm>
 
using namespace std;
 
struct AVLNode
{    char Name[100];
    //string Name;
    int Balance;
    AVLNode *LeftChild;
    AVLNode *RightChild;
};
 

AVLNode *InputData(ifstream &);
AVLNode *InsertData(AVLNode *, AVLNode *);
int FindBalance(AVLNode *);
void InorderTraversal(AVLNode *);
void PreorderTraversal(AVLNode *);
AVLNode *LeftLeft(AVLNode *);
AVLNode *RightRight(AVLNode *);
AVLNode *LeftRight(AVLNode *);
AVLNode *RightLeft(AVLNode *);
 
int main(int argc,char *argv[])
{
    ifstream CheckFile; //ifstream checks for file existence
    CheckFile.open(argv[1]); //attempts to open read file, and tests for existence
    if(CheckFile.is_open())
        cout << "Input file 'a6' exists." << endl << endl;
    else
        cout << "Input file 'a6' does not exist." << endl << endl;
 
    AVLNode *head = InputData(CheckFile);
    cout << "Inorder:\n\n";
    InorderTraversal(head);
    cout << "\n\nPreorder:\n\n";
    PreorderTraversal(head);
    cout << endl;
 
    system("PAUSE");
    return EXIT_SUCCESS;
}
 
AVLNode *InputData(ifstream &InFile)
{
    AVLNode *Root = NULL;
    AVLNode *Leaf;
    while(!InFile.eof())
    {
        Leaf = new AVLNode;InFile>>Leaf->Name;
      //  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(strcmp(Leaf->Name , Root->Name)<0)
    {
        Root->LeftChild = InsertData(Leaf, Root->LeftChild);
        if(FindBalance(Root->LeftChild) - FindBalance(Root->RightChild) == 2)
        {
            if(strcmp(Leaf->Name , Root->LeftChild->Name)<0)
                Root = LeftLeft(Root);
            else
                Root = LeftRight(Root);
        }
    }
    else if(strcmp(Leaf->Name , Root->Name)>0)
    {
        Root->RightChild = InsertData(Leaf, Root->RightChild);
        if(FindBalance(Root->RightChild) - FindBalance(Root->LeftChild) == 2)
        {
            if(strcmp(Leaf->Name , Root->RightChild->Name)>0)
                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 InorderTraversal(AVLNode *Root)
{
    AVLNode *temp;
    if(Root != NULL)
    {
        InorderTraversal(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;
        InorderTraversal(Root->RightChild); // print right subtree
    }
    return;
}
 
void PreorderTraversal(AVLNode *Root)
{
    AVLNode *temp;
    if(Root != NULL)
    {
        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;
        PreorderTraversal(Root->LeftChild);  // print left subtree
        PreorderTraversal(Root->RightChild); // print right subtree
    }
    return;
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.