Class 平衡基于字符串的二进制搜索树(用于拼写检查)

Class 平衡基于字符串的二进制搜索树(用于拼写检查),class,data-structures,spell-checking,binary-search-tree,tree-balancing,Class,Data Structures,Spell Checking,Binary Search Tree,Tree Balancing,更新:我无法让“平衡”工作,因为我无法让“doAVLBalance”识别成员函数“isBalanced()”、“isRightHeavy()”、“isLeftHeavy”。我不知道为什么!我尝试了Sash的例子(第三个答案),但我得到了“减速不兼容”,我无法解决这个问题…所以我尝试了我的方式…它告诉我这些成员函数不存在,当它们明显存在时 错误:类“IntBinaryTree:TreeNode”没有成员“isRightHeavy”。 在过去4个小时的尝试后,我被卡住了:(.下面更新了代码,非常感谢

更新:我无法让“平衡”工作,因为我无法让“doAVLBalance”识别成员函数“isBalanced()”、“isRightHeavy()”、“isLeftHeavy”。我不知道为什么!我尝试了Sash的例子(第三个答案),但我得到了“减速不兼容”,我无法解决这个问题…所以我尝试了我的方式…它告诉我这些成员函数不存在,当它们明显存在时

错误:类“IntBinaryTree:TreeNode”没有成员“isRightHeavy”。 在过去4个小时的尝试后,我被卡住了:(.下面更新了代码,非常感谢您的帮助

我正在创建一个基于字符串的二叉搜索树,需要使其成为一个“平衡”树。我该怎么做?*请帮助!!提前感谢

BinarySearchTree.cpp:

    bool IntBinaryTree::leftRotation(TreeNode *root)
    {
        //TreeNode *nodePtr = root;  // Can use nodePtr instead of root, better?
        // root, nodePtr, this->?

        if(NULL == root)
        {return NULL;}

        TreeNode *rightOfTheRoot = root->right;
        root->right = rightOfTheRoot->left;
        rightOfTheRoot->left = root;

        return rightOfTheRoot;
    }

    bool IntBinaryTree::rightRotation(TreeNode *root)
    {
        if(NULL == root)
        {return NULL;}
        TreeNode *leftOfTheRoot = root->left;
        root->left = leftOfTheRoot->right;
        leftOfTheRoot->right = root;

        return leftOfTheRoot;
    }

    bool IntBinaryTree::doAVLBalance(TreeNode *root)
    {


        if(NULL==root)
            {return NULL;}
        else if(root->isBalanced()) // Don't have "isBalanced"
            {return root;}

        root->left = doAVLBalance(root->left);
        root->right = doAVLBalance(root->right);

        getDepth(root); //Don't have this function yet

        if(root->isRightHeavy()) // Don't have "isRightHeavey"
        {
            if(root->right->isLeftheavey())
            {
                root->right = rightRotation(root->right);
            }
            root = leftRotation(root);
        }
        else if(root->isLeftheavey()) // Don't have "isLeftHeavey"
        {
            if(root->left->isRightHeavey())
            {
                root->left = leftRotation(root->left);
            }
            root = rightRotation(root);
        }
        return root;
    }

    void IntBinaryTree::insert(TreeNode *&nodePtr, TreeNode *&newNode)
    {
        if(nodePtr == NULL)
            nodePtr = newNode;                  //Insert node
        else if(newNode->value < nodePtr->value)
            insert(nodePtr->left, newNode);     //Search left branch
        else
            insert(nodePtr->right, newNode);    //search right branch
    }

//
// Displays the number of nodes in the Tree


int IntBinaryTree::numberNodes(TreeNode *root)
{
    TreeNode *nodePtr = root;

    if(root == NULL)
        return 0;

    int count = 1; // our actual node
    if(nodePtr->left !=NULL)
    { count += numberNodes(nodePtr->left);
    }
    if(nodePtr->right != NULL)
    {
        count += numberNodes(nodePtr->right);
    }
    return count;
} 

    // Insert member function

    void IntBinaryTree::insertNode(string num)
    {
        TreeNode *newNode; // Poitner to a new node.

        // Create a new node and store num in it.
        newNode = new TreeNode;
        newNode->value = num;
        newNode->left = newNode->right = NULL;

        //Insert the node.
        insert(root, newNode);
    }

    // More member functions, etc.
class IntBinaryTree
{
private:
    struct TreeNode
    {
        string value; // Value in the node
        TreeNode *left; // Pointer to left child node
        TreeNode *right; // Pointer to right child node
    };

    //Private Members Functions
    // Removed for shortness
    void displayInOrder(TreeNode *) const;


public:
    TreeNode *root;
    //Constructor
    IntBinaryTree()
        { root = NULL; }
    //Destructor
    ~IntBinaryTree()
        { destroySubTree(root); }

    // Binary tree Operations
    void insertNode(string);
    // Removed for shortness

    int numberNodes(TreeNode *root);
    //int balancedTree(string, int, int); // TreeBalanced

    bool leftRotation(TreeNode *root);
    bool rightRotation(TreeNode *root);
    bool doAVLBalance(TreeNode *root); // void doAVLBalance();
    bool isAVLBalanced();

    int calculateAndGetAVLBalanceFactor(TreeNode *root);

    int getAVLBalanceFactor()
    {
        TreeNode *nodePtr = root; // Okay to do this? instead of just
        // left->mDepth
        // right->mDepth

        int leftTreeDepth = (left !=NULL) ? nodePtr->left->Depth : -1;
        int rightTreeDepth = (right != NULL) ? nodePtr->right->Depth : -1;
        return(leftTreeDepth - rightTreeDepth);
    }

    bool isRightheavey() { return (getAVLBalanceFactor() <= -2); }

    bool isLeftheavey() { return (getAVLBalanceFactor() >= 2); }


    bool isBalanced()
    {
        int balanceFactor = getAVLBalanceFactor();
        return (balanceFactor >= -1 && balanceFactor <= 1);
    }


    int getDepth(TreeNode *root); // getDepth

    void displayInOrder() const
        { displayInOrder(root); }
    // Removed for shortness
};
bool IntBinaryTree::leftRotation(TreeNode*root)
{
//TreeNode*nodePtr=root;//可以使用nodePtr代替root,更好吗?
//root,nodePtr,这个->?
if(NULL==根)
{返回NULL;}
TreeNode*RightofRoot=根->右;
根->右=根的右->左;
右根->左=根;
归根权;
}
bool IntBinaryTree::rightRotation(TreeNode*根)
{
if(NULL==根)
{返回NULL;}
TreeNode*LeftOfRoot=根->左;
根->左=根的左->右;
左根->右=根;
返回根的左边;
}
bool IntBinaryTree::doAVLBalance(TreeNode*根)
{
if(NULL==根)
{返回NULL;}
else if(root->isBalanced())//没有“isBalanced”
{返回根;}
根->左=DOAVALBANCE(根->左);
根->右=DOAVALBANCE(根->右);
getDepth(root);//还没有此函数
if(root->isRightHeavy())//没有“isRightHeavy”
{
如果(根->右->IsLeftheavy())
{
根->右=右旋转(根->右);
}
根=左旋转(根);
}
else if(root->isleftheavy())//没有“isleftheavy”
{
如果(根->左->isRightHeavey())
{
根->左=左旋转(根->左);
}
根=右旋转(根);
}
返回根;
}
void IntBinaryTree::insert(TreeNode*&nodePtr,TreeNode*&newNode)
{
if(nodePtr==NULL)
nodePtr=newNode;//插入节点
else if(newNode->valuevalue)
插入(nodePtr->left,newNode);//搜索左分支
其他的
插入(nodePtr->right,newNode);//搜索右分支
}
//
//显示树中的节点数
int IntBinaryTree::numberNodes(树节点*根)
{
TreeNode*nodePtr=根;
if(root==NULL)
返回0;
int count=1;//我们的实际节点
if(nodePtr->left!=NULL)
{count+=numberNodes(nodePtr->left);
}
if(nodePtr->right!=NULL)
{
计数+=numberNodes(nodePtr->right);
}
返回计数;
} 
//插入成员函数
void IntBinaryTree::insertNode(字符串num)
{
TreeNode*newNode;//指向新节点。
//创建一个新节点并在其中存储num。
新节点=新树节点;
newNode->value=num;
newNode->left=newNode->right=NULL;
//插入节点。
插入(根,新节点);
}
//更多的成员功能等。
BinarySearchTree.h:

    bool IntBinaryTree::leftRotation(TreeNode *root)
    {
        //TreeNode *nodePtr = root;  // Can use nodePtr instead of root, better?
        // root, nodePtr, this->?

        if(NULL == root)
        {return NULL;}

        TreeNode *rightOfTheRoot = root->right;
        root->right = rightOfTheRoot->left;
        rightOfTheRoot->left = root;

        return rightOfTheRoot;
    }

    bool IntBinaryTree::rightRotation(TreeNode *root)
    {
        if(NULL == root)
        {return NULL;}
        TreeNode *leftOfTheRoot = root->left;
        root->left = leftOfTheRoot->right;
        leftOfTheRoot->right = root;

        return leftOfTheRoot;
    }

    bool IntBinaryTree::doAVLBalance(TreeNode *root)
    {


        if(NULL==root)
            {return NULL;}
        else if(root->isBalanced()) // Don't have "isBalanced"
            {return root;}

        root->left = doAVLBalance(root->left);
        root->right = doAVLBalance(root->right);

        getDepth(root); //Don't have this function yet

        if(root->isRightHeavy()) // Don't have "isRightHeavey"
        {
            if(root->right->isLeftheavey())
            {
                root->right = rightRotation(root->right);
            }
            root = leftRotation(root);
        }
        else if(root->isLeftheavey()) // Don't have "isLeftHeavey"
        {
            if(root->left->isRightHeavey())
            {
                root->left = leftRotation(root->left);
            }
            root = rightRotation(root);
        }
        return root;
    }

    void IntBinaryTree::insert(TreeNode *&nodePtr, TreeNode *&newNode)
    {
        if(nodePtr == NULL)
            nodePtr = newNode;                  //Insert node
        else if(newNode->value < nodePtr->value)
            insert(nodePtr->left, newNode);     //Search left branch
        else
            insert(nodePtr->right, newNode);    //search right branch
    }

//
// Displays the number of nodes in the Tree


int IntBinaryTree::numberNodes(TreeNode *root)
{
    TreeNode *nodePtr = root;

    if(root == NULL)
        return 0;

    int count = 1; // our actual node
    if(nodePtr->left !=NULL)
    { count += numberNodes(nodePtr->left);
    }
    if(nodePtr->right != NULL)
    {
        count += numberNodes(nodePtr->right);
    }
    return count;
} 

    // Insert member function

    void IntBinaryTree::insertNode(string num)
    {
        TreeNode *newNode; // Poitner to a new node.

        // Create a new node and store num in it.
        newNode = new TreeNode;
        newNode->value = num;
        newNode->left = newNode->right = NULL;

        //Insert the node.
        insert(root, newNode);
    }

    // More member functions, etc.
class IntBinaryTree
{
private:
    struct TreeNode
    {
        string value; // Value in the node
        TreeNode *left; // Pointer to left child node
        TreeNode *right; // Pointer to right child node
    };

    //Private Members Functions
    // Removed for shortness
    void displayInOrder(TreeNode *) const;


public:
    TreeNode *root;
    //Constructor
    IntBinaryTree()
        { root = NULL; }
    //Destructor
    ~IntBinaryTree()
        { destroySubTree(root); }

    // Binary tree Operations
    void insertNode(string);
    // Removed for shortness

    int numberNodes(TreeNode *root);
    //int balancedTree(string, int, int); // TreeBalanced

    bool leftRotation(TreeNode *root);
    bool rightRotation(TreeNode *root);
    bool doAVLBalance(TreeNode *root); // void doAVLBalance();
    bool isAVLBalanced();

    int calculateAndGetAVLBalanceFactor(TreeNode *root);

    int getAVLBalanceFactor()
    {
        TreeNode *nodePtr = root; // Okay to do this? instead of just
        // left->mDepth
        // right->mDepth

        int leftTreeDepth = (left !=NULL) ? nodePtr->left->Depth : -1;
        int rightTreeDepth = (right != NULL) ? nodePtr->right->Depth : -1;
        return(leftTreeDepth - rightTreeDepth);
    }

    bool isRightheavey() { return (getAVLBalanceFactor() <= -2); }

    bool isLeftheavey() { return (getAVLBalanceFactor() >= 2); }


    bool isBalanced()
    {
        int balanceFactor = getAVLBalanceFactor();
        return (balanceFactor >= -1 && balanceFactor <= 1);
    }


    int getDepth(TreeNode *root); // getDepth

    void displayInOrder() const
        { displayInOrder(root); }
    // Removed for shortness
};
类IntBinaryTree
{
私人:
树状结构
{
字符串值;//节点中的值
TreeNode*left;//指向左子节点的指针
TreeNode*right;//指向右子节点的指针
};
//私人成员的职能
//因短而删除
无效显示顺序(TreeNode*)常数;
公众:
树根;
//建造师
IntBinaryTree()
{root=NULL;}
//析构函数
~IntBinaryTree()
{子树(根);}
//二叉树运算
void insertNode(字符串);
//因短而删除
整数节点(树节点*根);
//int平衡树(字符串,int,int);//树平衡
布尔左旋转(树节点*根);
bool右旋转(树节点*根);
bool-doAVLBalance(TreeNode*root);//void-doAVLBalance();
bool isavl();
int CALCULATEADGETAVLBALANCEFactor(树节点*根);
int getAVLBalanceFactor()
{
TreeNode*nodePtr=root;//可以这样做吗?而不仅仅是
//左->深度
//右->深度
int leftTreeDepth=(left!=NULL)?nodePtr->left->Depth:-1;
int rightreedepth=(right!=NULL)?nodePtr->right->Depth:-1;
返回(leftTreeDepth-rightTreeDepth);
}
bool isRightheavey(){return(getAVLBalanceFactor()=2);}
布尔是平衡的
{
int balanceFactor=getAVLBalanceFactor();

return(balanceFactor>=-1&&balanceFactor有很多方法可以做到这一点,但我建议您实际上不要这样做。如果您想存储字符串的BST,有更好的选择:

>P>使用预写的二叉搜索树类。C++ STD::SET类提供平衡的二叉搜索树的相同时间保证,并且经常这样实现。它比滚动BST. < < /P> < /LIE更容易使用。
  • 使用trie。trie数据结构比字符串的BST更简单、更高效,完全不需要平衡,并且比BST更快

  • 如果你真的必须编写自己的平衡BST,你有很多选择。大多数使用平衡的BST实现都非常复杂,不适合胆小的人。我建议实现treap或splay tree,这两种平衡的BST结构都非常容易实现。它们都比cod更复杂你在上面有什么
    class BinarySearchTree
    {
    public:
        // .. lots of methods 
        Node *getRoot();
        int getDepth(Node *root);
    
        bool isAVLBalanced();
        int calculateAndGetAVLBalanceFactor(Node *root);
        void doAVLBalance();
    
    private:
         Node *mRoot;
    };
    
    class Node
    {
    public:
        int  mData;
        Node *mLeft;
        Node *mRight;
        bool mHasVisited;
        int mDepth;
    public:
    
        Node(int data)
        : mData(data),
          mLeft(NULL),
          mRight(NULL),
          mHasVisited(false),
          mDepth(0)
        {
        }
    
        int getData()              { return mData; }
    
        void setData(int data)     { mData = data;  }
    
        void setRight(Node *right) { mRight = right;}
    
        void setLeft(Node *left)   { mLeft = left; }
    
        Node * getRight()          { return mRight; }
    
        Node * getLeft()           { return mLeft; }
    
        bool hasLeft()             { return (mLeft != NULL);  }
    
        bool hasRight()            { return (mRight != NULL); }
    
        bool isVisited()           { return (mHasVisited == true); }
    
        int getAVLBalanceFactor()
        {
            int leftTreeDepth = (mLeft != NULL) ? mLeft->mDepth : -1;
            int rightTreeDepth = (mRight != NULL) ? mRight->mDepth : -1;
            return(leftTreeDepth - rightTreeDepth);
        }
    
        bool isRightHeavy() { return (getAVLBalanceFactor() <= -2); }
    
        bool isLeftHeavy()  { return (getAVLBalanceFactor() >= 2);  }
    
        bool isBalanced()
        {
            int balanceFactor = getAVLBalanceFactor();
            return (balanceFactor >= -1 && balanceFactor <= 1);
        }
    };