-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRedBlackTree.cpp
81 lines (72 loc) · 1.3 KB
/
RedBlackTree.cpp
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
#include "RedBlackTree.h"
#include <malloc.h>
struct RedBlackNode
{
ElementType Element;
PtrToRedBlackNode Left;
PtrToRedBlackNode Right;
ColorType Color;
};
RedBlackTree Initialize(void)
{
RedBlackTree T = (RedBlackTree)malloc(sizeof(struct RedBlackNode));
if (T == NULL)
return NULL;
T->Left = NULL;
T->Right = NULL;
T->Color = Black;
T->Element = 0;
return T;
}
Position SingleRotateWithLeft(Position P)
{
Position L = P->Right;
if (L == NULL)
return NULL;
P->Right = L->Left;
L->Left = P;
return L;
}
Position SingleRotateWithRight(Position P)
{
Position R = P->Left;
if (R == NULL)
return NULL;
P->Left = R->Right;
R->Right = P;
return R;
}
RedBlackTree Insert(ElementType Item, RedBlackTree T)
{
Position X, P, GP, GGP;
X = P = GP = T;
Position NewNode = (Position)malloc(sizeof(struct RedBlackNode));
while (X->Element != Item)
{
GGP = GP;
GP = P;
P = X;
if (Item < X->Element)
X = X->Left;
else
X = X->Right;
if (X->Left->Color == Red && X->Right->Color == Red)
;
}
if (X != NULL)
return NewNode;
X = (Position)malloc(sizeof(struct RedBlackNode));
if (X == NULL)
return NULL;
X->Element = Item;
X->Left = X->Right = NULL;
if (Item < P->Element)
P->Left = X;
else
P->Right = X;
}
}
void HandleReorient(ElementType Item, RedBlackTree T)
{
X->
}