AvlTree
Insert( ElementType X, AvlTree T)
{
1 if( T == NULL)
2 {
3 /* Create and return a one-onde tree */
4 T = malloc( sizeof( struct AvlNode ) );
5 if( T == NULL )
6 FatalError( "Out of space!!!");
7 else
8 {
9 T->Element = X; T->Height = 0;
10 T->Left = T->Right = NULL;
11 }
12 }
13 else
14 if( X < T->Element )
15 {
16 T->left = Insert(x, T->left );
17 if( Height( T->left ) - Height( T->Right ) == 2 )
18 if( X < T->Left->Element )
19 T = SingleRotatewithLeft( T );
20 else
21 T = DoubleRotateWithLeft( T );
22 }
23 else
24 if( X > T->Element )
25 {
26 T->Right = Insert( X, T->Right );
27 if( Height( T->Right ) - Height( T->Left ) == 2 )
28 if( X > T->Right->Element )
29 T = SingleRotateWithRight( T );
30 else
31 T = DoubleRotateWithRight( T );
32 }
33 /* Else X is in the tree already; we'll do nothing */
34 T->Height = Max( Height( T->Left ), Height( T->Right ) ) + 1;
35 return T;
}
为什么X不等于T->left->Element
,
以及X如何与T->left->Element
进行比较
X应该分配给T->left-Element
(当T->left = NULL
)
答案 0 :(得分:0)
在第18行之后,X
已插入T->Left
,但它可能(必然)不在该树的顶部。