[英]AVL tree insert function
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;
}
why X isn't equal to T->left->Element
, and how can X compare with T->left->Element
the X should be assign to the T->left-Element
(when the T->left = NULL
) 为什么X不等于T->left->Element
,以及X如何与T->left->Element
比较,X应该分配给T->left-Element
(当T->left = NULL
)
在第18行之后,将X
插入到T->Left
,但是(不一定)位于该树的顶部。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.