-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtree.h
144 lines (138 loc) · 4.37 KB
/
tree.h
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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
#define TreeNode struct TreeNode
TreeNode
{
int size, start, takenId, cursize;
TreeNode *left;
TreeNode *right;
};
void CreateNode(TreeNode **root, int lastSize, int start)
{
if (*root == NULL)
{
*root = (TreeNode *)malloc(sizeof(TreeNode));
(*root)->size = (*root)->cursize = lastSize;
(*root)->start = start;
(*root)->left = NULL;
(*root)->right = NULL;
(*root)->takenId = -1;
}
}
int insert(TreeNode **root, int value, int PId, int lastSize, int start)
{
// if the root is empty, create a new node to hold the value and make it the root
if (value > (*root)->cursize)
return 0; // can't insert under this node
if (value > (*root)->size / 2 && value <= (*root)->cursize && (*root)->takenId == -1) // check the place and check that it can be taken
{
printf("id: %d start: %d\n",PId,start);
(*root)->takenId = PId;
(*root)->cursize = 0;
return (*root)->size;
}
else
{
//(*root)->takenId = -2;
if ((*root)->left == NULL)
{
CreateNode(&((*root)->left), lastSize / 2, start);
CreateNode(&((*root)->right), lastSize / 2, start + lastSize / 2);
}
int ret;
ret = insert(&((*root)->left), value, PId, lastSize / 2, start);
if (ret > 0)
{
(*root)->cursize -= ret;
return ret;
}
ret = insert(&((*root)->right), value, PId, lastSize / 2, start + (lastSize / 2));
if (ret > 0)
{
(*root)->cursize -= ret;
}
return ret;
}
}
int Check(TreeNode **root, int value, int lastSize, int start)
{
// if the root is empty, create a new node to hold the value and make it the root
if (value > (*root)->cursize)
return 0; // can't insert under this node
if (value > (*root)->size / 2 && value <= (*root)->cursize && (*root)->takenId == -1) // check the place and check that it can be taken
{
return 1;
}
else
{
//(*root)->takenId = -2;
if ((*root)->left == NULL)
{
CreateNode(&((*root)->left), lastSize / 2, start);
CreateNode(&((*root)->right), lastSize / 2, start + lastSize / 2);
}
int ret;
ret = Check(&((*root)->left), value, lastSize / 2, start);
if (ret > 0)
{
return ret;
}
ret = Check(&((*root)->right), value, lastSize / 2, start + (lastSize / 2));
return ret;
}
}
int deleteNode(TreeNode **root, int PId, int *start)
{
if ((*root) == NULL)
return 0;
if ((*root)->takenId == PId)
{
(*root)->takenId = -1;
(*root)->cursize = (*root)->size;
(*start) = (*root)->start;
return (*root)->size;
}
int ret = 0;
ret = deleteNode(&((*root)->left), PId, start);
if (ret != 0)
{
// check that the right are empty or not to delete them both
if ((*root)->right->cursize == (*root)->right->size&&(*root)->left->cursize == (*root)->left->size)
{
free((*root)->left);
free((*root)->right);
(*root)->left = (*root)->right = NULL;
}
(*root)->cursize += ret;
return ret;
}
ret = deleteNode(&((*root)->right), PId, start);
if (ret != 0)
{
// check that the left are empty or not
if ((*root)->right->cursize == (*root)->right->size&&(*root)->left->cursize == (*root)->left->size)
{
free((*root)->left);
free((*root)->right);
(*root)->left = (*root)->right = NULL;
}
(*root)->cursize += ret;
return ret;
}
return ret;
}
TreeNode* findNode(TreeNode **root, int PId){
if((*root) == NULL) return 0;
if((*root)->takenId == PId){
return (*root);
}
TreeNode *left_pointer = findNode(&((*root)->left), PId);
if(left_pointer != NULL) return left_pointer;
TreeNode *right_pointer = findNode(&((*root)->right), PId);
return right_pointer;
}
/*
(-2 , 1024)
/ \
(5 , 512) (-2 , 512)
/ \ / \
(5 , 256) (5 , 256) (-2, 265) (-1 , 256)
*/