具有AVL的二叉树

时间:2019-05-29 09:24:24

标签: c++

我相信,除了checkBalances函数之外,所有代码都是正确的。它可以简单地固定break语句在while循环中的位置。但这不会显示预期的输出

我尝试过运气的break语句。

void BSTY::checkBalances(NodeT *n){
    NodeT *temp = n->parent;
    int balance = findBalance(temp);
    int balanceL = 0;
    int balanceR = 0;

    if (temp->left != NULL){
        balanceL = findBalance(temp->left);
    } 
    else if (temp->right != NULL){
        balanceR = findBalance(temp->right);
    }

    while (temp != NULL){
        if (balance < -1 && balanceR > 1){
            cout << temp->data << " must rotate left " << " (" << balance << ")" << endl;
            temp->right = rotateRight(temp->right);
            rotateLeft(temp);
            temp = temp->parent;
        }
        else if (balance > 1 && balanceL < -1){
            cout << temp->data << " must rotate right" << " (" << balance << ")" << endl;
            temp->left = rotateLeft(temp->left);
            rotateRight(temp);
            temp = temp->parent;
        }
        else if (balance >= 2){ /* Crashes here */
            cout << temp->data << " must rotate right" << " (" << balance << ")" << endl;
            rotateRight(temp);
            temp = temp->parent;

        }
        else if (balance <= -2){ /* Crashes here */
            cout << temp->data << " must rotate left" << " (" << balance << ")" << endl;
            rotateLeft(temp);
            temp = temp->parent;
            break;
        }
        else if (temp->parent != NULL){
            temp = temp->parent;

        }else{
            break;
        }

        balance = findBalance(temp);
        balanceL = 0;
        balanceR = 0;

        if (temp->left != NULL){
            balanceL = findBalance(temp->left);
        }
        else if (temp->right != NULL){
            balanceR = findBalance(temp->right);
        }
    }
}

输出:

inserting run
inserting tuxedo
inserting ocelot
inserting vast
inserting bark
inserting punctilios
inserting hello
inserting is
bark must rotate left (-2) 
inserting sibylic
inserting go
ocelot must rotate right (2) 
Mine 
3.141592653

*******************************
PREORDER 
|run(4): 
|hello(3): 
|bark(2): 
|go(1): 
|ocelot(2): 
|is(1): 
|punctilios(1): 
|tuxedo(2): 
|sibylic(1): 
|vast(1): 
*******************************
INORDER 
|bark(2): 
|go(1): 
|hello(3): 
|is(1): 
|ocelot(2): 
|punctilios(1): 
|run(4): 
|sibylic(1): 
|tuxedo(2): 
|vast(1): 
*******************************
POSTORDER 
|go(1): 
|bark(2): 
|is(1): 
|punctilios(1): 
|ocelot(2): 
|hello(3): 
|sibylic(1): 
|vast(1): 
|tuxedo(2): 
|run(4): 



PART TWO 
3: |ocelot(2): 
perforated not found 
3: |sibylic(1): 
4: |go(1): 

A LOT OF LEFT ROTATIONS 
inserting a
inserting b
inserting c
a must rotate left (-2) 
inserting d
inserting e
c must rotate left (-2) 
inserting f
b must rotate left (-2) 
inserting g
e must rotate left (-2) 
inserting h
inserting i
g must rotate left (-2) 
inserting j
f must rotate left (-2) 

*******************************
PREORDER 
|d(4): 
|b(2): 
|a(1): 
|c(1): 
|h(3): 
|f(2): 
|e(1): 
|g(1): 
|i(2): 
|j(1): 
*******************************
INORDER 
|a(1): 
|b(2): 
|c(1): 
|d(4): 
|e(1): 
|f(2): 
|g(1): 
|h(3): 
|i(2): 
|j(1): 
*******************************
POSTORDER 
|a(1): 
|c(1): 
|b(2): 
|e(1): 
|g(1): 
|f(2): 
|j(1): 
|i(2): 
|h(3): 
|d(4): 




A LOT OF RIGHT ROTATIONS 
inserting j
inserting i
inserting h
j must rotate right (2) 
inserting g
inserting f
h must rotate right (2) 
inserting e
i must rotate right (2) 
inserting d
f must rotate right (2) 
inserting c
inserting b
d must rotate right (2) 
inserting a
e must rotate right (2) 

*******************************
PREORDER 
|g(4): 
|c(3): 
|b(2): 
|a(1): 
|e(2): 
|d(1): 
|f(1): 
|i(2): 
|h(1): 
|j(1): 
*******************************
INORDER 
|a(1): 
|b(2): 
|c(3): 
|d(1): 
|e(2): 
|f(1): 
|g(4): 
|h(1): 
|i(2): 
|j(1): 
*******************************
POSTORDER 
|a(1): 
|b(2): 
|d(1): 
|f(1): 
|e(2): 
|c(3): 
|h(1): 
|j(1): 
|i(2): 
|g(4): 




A LOT OF RIGHT-LEFT ROTATIONS 
inserting g
inserting j
inserting i
g must rotate left (-2) 
j child, rotating right 
inserting c
inserting f
g must rotate right (2) 
c must rotate left (child) 
inserting h
i must rotate right (2) 
f must rotate left (child) 
inserting e
f must rotate right (2) 
c must rotate left (child) 
inserting d
inserting a
inserting b
e must rotate right (2) 

*******************************
PREORDER 
|g(4): 
|c(3): 
|a(2): 
|b(1): 
|e(2): 
|d(1): 
|f(1): 
|i(2): 
|h(1): 
|j(1): 
*******************************
INORDER 
|a(2): 
|b(1): 
|c(3): 
|d(1): 
|e(2): 
|f(1): 
|g(4): 
|h(1): 
|i(2): 
|j(1): 
*******************************
POSTORDER 
|b(1): 
|a(2): 
|d(1): 
|f(1): 
|e(2): 
|c(3): 
|h(1): 
|j(1): 
|i(2): 
|g(4): 




A LOT OF LEFT-RIGHT ROTATIONS 
inserting a
inserting p
inserting d
a must rotate left (-2) 
p child, rotating right 
inserting g
inserting f
p must rotate right (2) 
inserting e
d must rotate left (-2) 
g child, rotating right 
inserting k
g must rotate left (-2) 
p child, rotating right 
inserting j
inserting v
inserting b

*******************************
PREORDER 
|f(4): 
|d(3): 
|a(2): 
|b(1): 
|e(1): 
|k(3): 
|g(2): 
|j(1): 
|p(2): 
|v(1): 
*******************************
INORDER 
|a(2): 
|b(1): 
|d(3): 
|e(1): 
|f(4): 
|g(2): 
|j(1): 
|k(3): 
|p(2): 
|v(1): 
*******************************
POSTORDER 
|b(1): 
|a(2): 
|e(1): 
|d(3): 
|j(1): 
|g(2): 
|v(1): 
|p(2): 
|k(3): 
|f(4):

0 个答案:

没有答案