-
Notifications
You must be signed in to change notification settings - Fork 28
/
treap.c
116 lines (101 loc) · 2.38 KB
/
treap.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
/*
* This file is part of the tknet project.
* which be used under the terms of the GNU General Public
* License version 3.0 as published by the Free Software
* Foundation and appearing in the file LICENSE.GPL included
* in the packaging of this file. Please review the following
* information to ensure the GNU General Public License
* version 3.0 requirements will be met:
* http://www.gnu.org/copyleft/gpl.html
*
* Copyright (C) 2012 Zhong Wei <[email protected]> .
*/
#include "tknet.h"
DEF_STRUCT_CONSTRUCTOR( Treap ,
out_cons->RanPriority = tkGetRandom();
BinTreeNodeCons(&out_cons->btnd);
)
void
TreapRoateUp(struct Treap* pa_pTp)
{
struct BinTreeNode *pFather,*pGrandpa,*pSave = NULL;
BOOL Pos,FatherPos;
if( pa_pTp->btnd.tnd.pFather == NULL )
{
return ;
}
pFather = GET_STRUCT_ADDR( pa_pTp->btnd.tnd.pFather , struct BinTreeNode , tnd );
Pos = ( pFather->RightChild == &pa_pTp->btnd );
BinDisattach(&pa_pTp->btnd);
if( pFather->tnd.pFather )
{
pGrandpa = GET_STRUCT_ADDR( pFather->tnd.pFather , struct BinTreeNode , tnd );
FatherPos = ( pGrandpa->RightChild == pFather );
BinDisattach(pFather);
BinAttachTo( &pa_pTp->btnd , pGrandpa , FatherPos );
}
if( Pos )
{
pSave = pa_pTp->btnd.LeftChild;
}
else
{
pSave = pa_pTp->btnd.RightChild;
}
if( pSave )
{
BinDisattach( pSave );
BinAttachTo( pSave , pFather , Pos );
}
BinAttachTo( pFather , &pa_pTp->btnd , !Pos );
}
void
TreapInsert(struct Treap* pa_pFrom , struct Treap* pa_pTo , BinTreeCompareCallback pa_cmp , void* pa_else)
{
struct TreeNode *pFather;
struct Treap *pFatherTp;
BinTreeInsert( &pa_pFrom->btnd , &pa_pTo->btnd , pa_cmp , pa_else );
while(1)
{
pFather = pa_pFrom->btnd.tnd.pFather;
if( pFather )
{
pFatherTp = GET_STRUCT_ADDR( pFather , struct Treap , btnd.tnd );
if( pa_pFrom->RanPriority < pFatherTp->RanPriority )
{
TreapRoateUp( pa_pFrom );
}
else
{
break;
}
}
else
{
break;
}
}
}
void
TreapDragOut( struct Treap* pa_pTp )
{
struct Treap *pTp;
while(1)
{
if( pa_pTp->btnd.RightChild )
{
pTp = GET_STRUCT_ADDR( pa_pTp->btnd.RightChild , struct Treap , btnd );
TreapRoateUp( pTp );
}
else if( pa_pTp->btnd.LeftChild )
{
pTp = GET_STRUCT_ADDR( pa_pTp->btnd.LeftChild , struct Treap , btnd );
TreapRoateUp( pTp );
}
else
{
break;
}
}
BinDisattach( &pa_pTp->btnd );
}