2016-05-22 5 views
0

私はAVLツリーの右回転を書いています。現在、私はAVLツリーの高さ部分を無視しています。私は後でそれを扱います。ただ5で右回転を適用して間違った値を与えています。 l->data,ll->data,lll->dataはRight Rotateを実行した後でも古い値(それぞれ5,3,2)を与えています。私が使用しているAVLツリーは次のとおりです。AVLの右回転機能はまだ古い値を与えていますか?

        9(Root) 

       5(Left Child)       13(Right Child) 

     3      7     11      17 
    2 
1 

Cコード:彼らはmain()で定義されたローカル変数であり、彼らはあなたの後に新しい値を得ることはありませんので、

#include <stdio.h> 
#include <stdlib.h> 
#include <malloc.h> 


struct AVLTree 
{ 
    int data; 
    struct AVLTree *left; 
    struct AVLTree *right; 
}; 




struct AVL *RightRotate1(struct AVLTree *rootop,struct AVLTree *root) 
{ 
    if(root == NULL) 
    { 
     return NULL; 
    } 
    if(rootop->data > root->data) 
    { 
     root->right = RightRotate1(rootop,root->right); 
    } 
    else if(rootop->data < root->data) 
    { 
     root->left = RightRotate1(rootop,root->left); 
    } 
    else 
    { 
     struct AVLTree *A = rootop->left; 
     rootop->left = A->right; 
     A->right = rootop; 

     return A; 
    } 
    return root; 

} 




int main() 
{ 
    struct AVLTree * root = (struct AVLTree *)malloc(sizeof(struct AVLTree)); 
    root-> data = 9; 
    struct AVLTree * l = (struct AVLTree *)malloc(sizeof(struct AVLTree)); 
    l -> data = 5; 
    struct AVLTree * ll = (struct AVLTree *)malloc(sizeof(struct AVLTree)); 
    ll -> data = 3; 
    ll -> left = ll -> right = NULL; 
    struct AVLTree * lll = (struct AVLTree *)malloc(sizeof(struct AVLTree)); 
    lll -> data = 2; 
    lll -> left = lll -> right = NULL; 
    ll->left = lll; 
    struct AVLTree * llll = (struct AVLTree *)malloc(sizeof(struct AVLTree)); 
    llll -> data = 1; 
    llll -> left = llll -> right = NULL; 
    lll->left = llll; 
    struct AVLTree * lr = (struct AVLTree *)malloc(sizeof(struct AVLTree)); 
    lr -> data = 7; 
    lr -> left = lr -> right = NULL; 
    l -> left = ll; 
    l -> right = lr; 
    struct AVLTree * r = (struct AVLTree *)malloc(sizeof(struct AVLTree)); 
    r -> data = 13; 
    struct AVLTree * rl = (struct AVLTree *)malloc(sizeof(struct AVLTree)); 
    rl -> data = 11; 
    rl -> left = rl -> right = NULL; 
    struct AVLTree * rr = (struct AVLTree *)malloc(sizeof(struct AVLTree)); 
    rr -> data = 17; 
    rr -> left = rr -> right = NULL; 
    r -> left = rl; 
    r -> right = rr; 
    root -> left = l; 
    root -> right = r; 



    root = RightRotate1(l,root); 

    printf("Root is %d\n",root->data); 
    printf("Root Left is %d\n",root->left->data); 
    printf("Root Left Left is %d\n",root->left->left->data); 
    printf("Root Left Right is %d\n",root->left->right->data); 

    printf("Left is %d\n",l->data); 
    printf("Left left is %d\n",ll->data); 
    printf("Left right is %d\n",lll->data); 


    return 0; 
} 

答えて

1

あなたはまだl, ll, and lllの古い値を取得していますRightRotate1()からの返信。 main()rootポインタのみがRightRotate1()に変更されます。 l, ll, and lllを使用する場合は、ツリーを回転してから新しい場所を指すように再割り当てする必要があります。

root = RightRotate1(l,root); 
l = root->left; 
ll = root->left->left; 
lll = root->left->left->left; 
関連する問題