HW3A - finish setting links K1>Height=Max Height(K1>Left Height(K1>Right 1 K3>Height=Max Height(K3>Left Height(K3>Right 1

Info iconThis preview shows pages 1–2. Sign up to view the full content.

View Full Document Right Arrow Icon
p.141 4.16 Show the result of inserting 2, 1, 4, 5, 9, 3, 6, 7 into an initially empty AVL tree. 4 6 1 2 7 5 9 3 p.141 4.22 Write the functions to perform the double rotation without the inefficiency of doing two single rotations. #ifndef _AvlTree_H #define _AvlTree_H struct AvlNode; typedef struct AvlNode *Position; typedef struct AvlNode *AvlTree; /* function declarations are omitted */ #endif /* _AvlTree_H */ struct AvlNode { ElementType Element; AvlTree Left, Right; int Height; } static Position DoubleRotateWithLeft ( Position K3 ) { /* Do the left—right double rotation. K3 is the trouble finder. */ Position K1, K2; K1=K3->Left; /* mark parent */ K2=K1->Right; /* mark trouble maker */ K1->Right=K2->Left; K3->Left=K2->Right; K2->Left=K1; K2->Right=K3;
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Background image of page 2
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: /* finish setting links */ K1->Height=Max( Height(K1->Left), Height(K1->Right) ) + 1; K3->Height=Max( Height(K3->Left), Height(K3->Right) ) + 1; K2->Height=Max( K1->Height, K3->Height ) + 1; /* finish setting heights */ return K2; /* K2 is the new root */ } static Position DoubleRotateWithRight ( Position K1 ) { /* Do the right--left double rotation. K1 is the trouble finder. */ Position K2, K3; /* Similar to the above function */ K3=K1->Right; K2=K3->Left; K1->Right=K2->Left; K3->Left=K2->Right; K2->Left=K1; K2->Right=K3; K1->Height=Max( Height(K1->Left), Height(K1->Right) ) + 1; K3->Height=Max( Height(K3->Left), Height(K3->Right) ) + 1; K2->Height=Max( K1->Height, K3->Height ) + 1; return K2; }...
View Full Document

This note was uploaded on 02/16/2011 for the course CS 136 taught by Professor Yuechen during the Winter '08 term at Zhejiang University.

Page1 / 2

HW3A - finish setting links K1>Height=Max Height(K1>Left Height(K1>Right 1 K3>Height=Max Height(K3>Left Height(K3>Right 1

This preview shows document pages 1 - 2. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online