C++ c++;AVL二叉树旋转和删除树的问题

C++ c++;AVL二叉树旋转和删除树的问题,c++,binary-tree,avl-tree,C++,Binary Tree,Avl Tree,我正在研究一个AVL二叉树实现,除了旋转和删除函数外,我的大部分代码都在工作。我尝试了不同的实现方法,但我仍然无法找出我做错了什么。如果有人能帮我解决问题,我将不胜感激。如果我注释掉平衡函数,代码将根据需要工作,并插入导入的文本文件中的所有单词,但当我尝试平衡节点时,代码就会崩溃。出于某种原因,我的: 删除节点 但我不确定为什么这会成为一个问题。通过树的递归没有问题,但是一旦它到达删除节点部分,它就有问题了,这对我来说没有意义 标题: #ifndef AVLBINARYTREE_H #defin

我正在研究一个AVL二叉树实现,除了旋转和删除函数外,我的大部分代码都在工作。我尝试了不同的实现方法,但我仍然无法找出我做错了什么。如果有人能帮我解决问题,我将不胜感激。如果我注释掉平衡函数,代码将根据需要工作,并插入导入的文本文件中的所有单词,但当我尝试平衡节点时,代码就会崩溃。出于某种原因,我的:

删除节点

但我不确定为什么这会成为一个问题。通过树的递归没有问题,但是一旦它到达删除节点部分,它就有问题了,这对我来说没有意义

标题:

#ifndef AVLBINARYTREE_H
#define AVLBINARYTREE_H
#include <iostream>
#include <string>
using namespace std;

class LinkedBinaryTree {
private:
    struct Node {
        string word;
        Node* left;
        Node* right;
        Node* parent;
        int wordCount;
        int height;
        Node() : word(), left(NULL), right(NULL), parent(NULL), wordCount(1), height(0) {}
        Node(string s, Node* l, Node* r, Node* p) {
            word = s;
            left = NULL;
            right = NULL;
            parent = p;
            wordCount = 1;
            height = 0;
        }
    };

    Node* _root;

public:
    LinkedBinaryTree();
    ~LinkedBinaryTree();
    void destroyTree();
    void destroyTree(Node* node);
    void insert(string word);
    void display(Node* ptr, int level);
    Node* root();
    void inOrder(Node* node);
    int avlNum(Node* node);
    int getNumWords();

    void insertNode(Node* node, string word);
    int height(Node* node);
    int bfactor(Node* node);
    void fixHeight(Node* node);
    void balance(Node* node);
    void rightRotate(Node* node);
    void leftRotate(Node* node);
    void rightLeftRotate(Node* node);
    void leftRightRotate(Node* node);

    int n;
};
#endif
\ifndef AVLBINARYTREE\u H
#定义AVLBINARYTREE\u H
#包括
#包括
使用名称空间std;
类LinkedBinaryTree{
私人:
结构节点{
字符串字;
节点*左;
节点*右;
节点*父节点;
int字数;
内部高度;
Node():word()、左(NULL)、右(NULL)、父(NULL)、字数(1)、高度(0){}
节点(字符串s、节点*l、节点*r、节点*p){
word=s;
左=空;
右=空;
父母=p;
字数=1;
高度=0;
}
};
节点*_根;
公众:
LinkedBinaryTree();
~LinkedBinaryTree();
void树();
void破坏树(Node*Node);
空白插入(字符串字);
无效显示(节点*ptr,整数级);
Node*root();
按顺序作废(节点*节点);
int avlNum(节点*节点);
int getNumWords();
void insertNode(节点*节点,字符串字);
整数高度(节点*节点);
int bfactor(Node*Node);
空隙固定高度(节点*节点);
空余额(节点*节点);
void rightRotate(节点*节点);
void leftRotate(节点*节点);
void right leftrotate(节点*节点);
void leftRightRotate(节点*节点);
int n;
};
#恩迪夫
.cpp:

#包括“AVLBinaryTree.h”
#包括
void LinkedBinaryTree::索引(节点*节点){
if(node==NULL)
回来
顺序(节点->左);
无法计算剩余的字数;
节点->左=临时->右;
//节点->左->父节点=节点;
临时->父节点->父节点;
temp->right=节点;
节点->父节点=临时节点;
节点=温度;
如果(临时->父项==NULL){
_根=节点;
}
固定高度(节点);
固定高度(节点->右侧);
固定高度(节点->左侧);
}
void LinkedBinaryTree::leftRotate(节点*节点){
节点*温度;
temp=节点->右侧;
节点->右=临时->左;
临时->父节点->父节点;
temp->left=节点;
节点->父节点=临时节点;
节点=温度;
如果(临时->父项==NULL){
_根=节点;
}
固定高度(节点);
固定高度(节点->右侧);
固定高度(节点->左侧);
}
void LinkedBinaryTree::rightLeftRotate(节点*节点){
右旋转(节点->左);
左旋转(节点);
}
void LinkedBinaryTree::leftRightRotate(节点*节点){
左旋转(节点->右);
右旋转(节点);
}
int LinkedBinaryTree::高度(节点*节点){
int h=0;
如果(节点!=NULL){
h=节点->高度;
}
返回h;
}
int LinkedBinaryTree::bfactor(节点*节点){
返回高度(节点->右)-高度(节点->左);
}
void LinkedBinaryTree::固定高度(节点*节点){
int hl=高度(节点->左侧);
int hr=高度(节点->右侧);
节点->高度=(hl>hr?hl:hr)+1;
}
int LinkedBinaryTree::avlNum(节点*节点){
int leftH=高度(节点->左侧);
int rightH=高度(节点->右侧);
int avlNum=rightH-leftH;
返回平均值;
}
LinkedBinaryTree::LinkedBinaryTree(){
_root=NULL;
}
LinkedBinaryTree::~LinkedBinaryTree(){
破坏树();
}
void LinkedBinaryTree::destroyTree(){
破坏树(_根);
}
//**********************************************************
//destroyTree由析构函数调用。它删除
//树中的所有节点。
//**********************************************************
void LinkedBinaryTree::destroyTree(节点*节点){
如果(节点!=NULL){
如果(节点->左!=NULL)
破坏树(节点->左侧);
如果(节点->右侧!=NULL)
破坏树(节点->右侧);
删除节点;
}
}
void LinkedBinaryTree::insertNode(节点*节点,字符串字){
if(wordword){
如果(节点->左!=NULL)
insertNode(节点->左侧,word);
否则{
节点->左=新节点(字,空,空,节点);
n++;
固定高度(节点->左侧);
}
}
else if(word>节点->word){
如果(节点->右侧!=NULL)
insertNode(节点->右侧,word);
否则{
node->right=新节点(word,NULL,NULL,node);
n++;
固定高度(节点->右侧);
}
}
else if(word==节点->word){
node->wordCount++;
}
平衡(节点);
}
void LinkedBinaryTree::insert(字符串字){
如果(_root==NULL){
_root=新节点(word,NULL,NULL,NULL);
n++;
}
否则{
插入节点(_根,字);
}
}
void LinkedBinaryTree::display(节点*ptr,int级别){
int i;
如果(ptr!=NULL)
{
显示(ptr->右侧,级别+1);
printf(“\n”);
如果(ptr==\u根)
cout(右侧)<0)
右旋转(节点->右);
其他的
左旋转(节点);
}
if(bfactor(node)=-2){
如果(b因素(节点->左)>0)
左旋转(节点->左);
其他的
右旋转(节点);
}
}
int LinkedBinaryTree::getNumWords(){
返回n;
}
主要内容:

#包括“AVLBinaryTree.h”
#包括
#包括
#包括
#包括
#包括
#包括
int main(int argv,char*argc[]){
连接二叉树t;
字符串字(“测试”)、最后一个字(“”);
对于(int i=0;i#include "AVLBinaryTree.h"
#include <algorithm>

void LinkedBinaryTree::inOrder(Node* node) {

    if (node == NULL)
        return;
    inOrder(node->left);
    cout << node->wordCount << " " << node->word << endl;
    inOrder(node->right);
}

void LinkedBinaryTree::rightRotate(Node* node) {

    Node* temp;
    temp = node->left;
    node->left = temp->right;
    //node->left->parent = node;
    temp->parent = node->parent;
    temp->right = node;
    node->parent = temp;
    node = temp;
    if (temp->parent == NULL) {
        _root = node;
    }
    fixHeight(node);
    fixHeight(node->right);
    fixHeight(node->left);
}

void LinkedBinaryTree::leftRotate(Node* node) {

    Node* temp;
    temp = node->right;
    node->right = temp->left;
    temp->parent = node->parent;
    temp->left = node;
    node->parent = temp;
    node = temp;
    if (temp->parent == NULL) {
        _root = node;
    }
    fixHeight(node);
    fixHeight(node->right);
    fixHeight(node->left);
}

void LinkedBinaryTree::rightLeftRotate(Node* node) {

    rightRotate(node->left);
    leftRotate(node);
}

void LinkedBinaryTree::leftRightRotate(Node* node) {

    leftRotate(node->right);
    rightRotate(node);
}

int LinkedBinaryTree::height(Node* node) {

    int h = 0;

    if (node != NULL) {
        h = node->height;
    }
    return h;
}

int LinkedBinaryTree::bfactor(Node* node) {

    return height(node->right) - height(node->left);
}

void LinkedBinaryTree::fixHeight(Node* node) {

    int hl = height(node->left);
    int hr = height(node->right);
    node->height = (hl > hr ? hl : hr) + 1;
}

int LinkedBinaryTree::avlNum(Node* node) {

    int leftH = height(node->left);
    int rightH = height(node->right);
    int avlNum = rightH - leftH;
    return avlNum;
}

LinkedBinaryTree::LinkedBinaryTree() {

    _root = NULL;
}

LinkedBinaryTree::~LinkedBinaryTree() {

    destroyTree();
}
void LinkedBinaryTree::destroyTree() {

    destroyTree(_root);
}
//**********************************************************
// destroyTree is called by the destructor. It deletes  
// all nodes in the tree.                                  
//**********************************************************
void LinkedBinaryTree::destroyTree(Node* node) {

    if (node != NULL) {
        if (node->left != NULL)
            destroyTree(node->left);
        if (node->right != NULL)
            destroyTree(node->right);
        delete node;
    }
}

void LinkedBinaryTree::insertNode(Node* node, string word) {

    if (word < node->word) {
        if (node->left != NULL)
            insertNode(node->left, word);
        else {
            node->left = new Node(word, NULL, NULL, node);
            n++;
            fixHeight(node->left);
        }
    }
    else if (word > node->word) {

        if (node->right != NULL)
            insertNode(node->right, word);
        else {
            node->right = new Node(word, NULL, NULL, node);
            n++;
            fixHeight(node->right);
        }
    }
    else if (word == node->word) {
        node->wordCount++;
    }
    balance(node);
}

void LinkedBinaryTree::insert(string word) {

    if (_root == NULL) {
        _root = new Node(word, NULL, NULL, NULL);
        n++;
    }
    else {
        insertNode(_root, word);
    }
}
void LinkedBinaryTree::display(Node* ptr, int level) {

    int i;
    if (ptr != NULL)
    {
        display(ptr->right, level + 1);
        printf("\n");
        if (ptr == _root)
            cout << "Root -> ";
        for (i = 0; i < level && ptr != _root; i++)
            cout << "        ";
        cout << ptr->word;
        display(ptr->left, level + 1);
    }
}

LinkedBinaryTree::Node * LinkedBinaryTree::root() {

    return _root;
}

void LinkedBinaryTree::balance(Node* node) {

    fixHeight(node);
    if (bfactor(node) == 2) {
        if (bfactor(node->right) < 0)
            rightRotate(node->right);
        else
            leftRotate(node);
    }
    if (bfactor(node) == -2) {
        if (bfactor(node->left) > 0)
            leftRotate(node->left);
        else
            rightRotate(node);
    }
}

int LinkedBinaryTree::getNumWords() {

    return n;
}
#include "AVLBinaryTree.h"
#include <iostream>
#include <fstream>
#include <string>
#include <queue>
#include <vector>
#include <functional>

int main(int argv, char *argc[]) {

    LinkedBinaryTree t;
    string word("Test."), lastword("");

    for (int i = 0; i < argv; i++)
        cout << argc[i] << endl;

    if (argv < 2) {
        cerr << "No input file specified" << endl;
        system("pause");
        exit(1);
    }
    for (int count = 1; count < argv; count++)
    {
        ifstream input(argc[count]);
        if (!input) {
            cerr << "Cannot open input file" << argc[count] << endl;
            system("pause");
            exit(1);
        }
        while (input >> word)
        {
            transform(word.begin(), word.end(), word.begin(), ::tolower);

            word.erase(remove_if(word.begin(), word.end(), ispunct));

            t.insert(word);
        }
    }

    t.inOrder(t.root());
    cout << endl;

    cout << "--------" << endl;
    cout << t.getNumWords() << "  " << "Total number of different words";
    cout << endl;


    /*t.insert("Yes");
    t.insert("No");
    t.insert("Maybe");
    t.insert("Hopefully");
    t.insert("Absolutely");

    t.display(t.root(), 1);

    cout << endl;
    cout << endl;

    t.inOrder(t.root());
    */

    system("PAUSE");

    t.~LinkedBinaryTree();

    return EXIT_SUCCESS;
}