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 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303
|
#include <stdio.h> #include <stdlib.h> #include <string.h> #include <unistd.h>
#define max(x, y) (x) > (y)? (x) : (y) #define treenode(T) struct treenode_##T #define Queue(T) typedef struct queuetreenode_##T{\ void *start;\ void *end;\ struct treenode_##T **ptr;\ int size;\ void (*push)(struct queuetreenode_##T *queue, treenode_##T *val);\ void (*pop)(struct queuetreenode_##T *queue);\ treenode_##T *(*top)(struct queuetreenode_##T *queue);\ }queuetreenode_##T; #define queue(T) queuetreenode_##T #define Push(T) void pushtreenode_##T(queuetreenode_##T *queue, struct treenode_##T *val){\ if((void *) queue->ptr == queue->end){\ int size = queue->end - queue->start;\ void *newstart = malloc(2 * size);\ memcpy(newstart, queue->start, size);\ free(queue->start);\ queue->start = newstart;\ queue->end = queue->start + 2 * size;\ queue->ptr = queue->start + size;\ }\ memcpy(queue->ptr, &val, sizeof(struct treenode_##T *));\ queue->ptr += 1;\ queue->size++;\ } #define Pop(T) void poptreenode_##T(queuetreenode_##T *queue){\ if((void *) queue->ptr == queue->start) return;\ memmove(queue->start, queue->start + sizeof(treenode_##T *), queue->end - queue->start - sizeof(treenode_##T *));\ queue->ptr -= 1;\ queue->size--;\ } #define Top(T) treenode_##T *toptreenode_##T(queuetreenode_##T *queue){\ treenode_##T *treenode = *((treenode_##T **) queue->start);\ return treenode;\ } #define NewQueue(T) queuetreenode_##T *newqueuetreenode_##T(int size){\ queuetreenode_##T *queue = (queuetreenode_##T *) malloc(sizeof(queuetreenode_##T));\ queue->start = malloc(sizeof(treenode_##T *) * size);\ queue->end = queue->start + sizeof(treenode_##T *) * size;\ queue->ptr = queue->start;\ queue->push = pushtreenode_##T;\ queue->pop = poptreenode_##T;\ queue->top = toptreenode_##T;\ } #define newqueue(T) newqueuetreenode_##T #define DeleteQueue(T) void deletequeuetreenode_##T(queuetreenode_##T *queue){\ free(queue->start);\ free(queue);\ } #define deletequeue(T) deletequeuetreenode_##T
#define TreeNode(T) typedef struct treenode_##T{\ T val;\ struct treenode_##T *parent;\ struct treenode_##T *left;\ struct treenode_##T *right;\ int height;\ void (*nodeDFS)(treenode_##T *this, void (*func)(T val));\ void (*nodeBFS)(treenode_##T *this, void (*func)(T val));\ void (*insertl)(struct treenode_##T *this, struct treenode_##T *node);\ void (*insertr)(struct treenode_##T *this, struct treenode_##T *node);\ int (*isleaf)(struct treenode_##T *this);\ int (*isroot)(struct treenode_##T *this);\ }treenode_##T; #define NodeDFS(T) void nodeDFS_##T(treenode_##T *this, void (*func)(T val)){\ if(!this)return;\ func(this->val);\ nodeDFS_##T(this->left, func);\ nodeDFS_##T(this->right, func);\ } #define NodeBFS(T) void nodeBFS_##T(treenode_##T *this, void (*func)(T val)){\ queue(T) *nodequeue = newqueue(T)(32);\ nodequeue->push(nodequeue, this);\ while(nodequeue->size){\ treenode_##T *node = nodequeue->top(nodequeue);\ nodequeue->pop(nodequeue);\ if(!node)continue;\ func(node->val);\ nodequeue->push(nodequeue, node->left);\ nodequeue->push(nodequeue, node->right);\ }\ } #define InsertL(T) void insertl_##T(treenode_##T *this, treenode_##T *node){\ this->left = node;\ this->height = (1 + node->height > this->height? 1 + node->height : this->height);\ node->parent = this;\ } #define InsertR(T) void insertr_##T(treenode_##T *this, treenode_##T *node){\ this->right = node;\ this->height = (1 + node->height > this->height? 1 + node->height : this->height);\ node->parent = this;\ } #define Isleaf(T) int isleaf_##T(treenode_##T *this){\ return !this->right && !this->left;\ } #define Isroot(T) int isroot_##T(treenode_##T *this){\ return !this->parent;\ } #define NewTreeNode(T) treenode_##T *newtreenode_##T(T val){\ treenode_##T *node = (treenode_##T *) malloc(sizeof(treenode_##T));\ node->val = val;\ node->left = NULL;\ node->right = NULL;\ node->parent = NULL;\ node->height = 0;\ node->nodeDFS = nodeDFS_##T;\ node->nodeBFS = nodeBFS_##T;\ node->insertl = insertl_##T;\ node->insertr = insertr_##T;\ node->isleaf = isleaf_##T;\ node->isroot = isroot_##T;\ return node;\ } #define DeleteTreeNode(T) void deletetreenode_##T(treenode_##T *node){\ if(!node)return;\ deletetreenode_##T(node->left);\ deletetreenode_##T(node->right);\ free(node);\ } #define newtreenode newtreenode_##T #define deletetreenode(T) deletetreenode_##T #define Tree(T) typedef struct tree_##T{\ treenode_##T *root;\ int size;\ int (*compare)(const T a, const T b);\ int (*isempty)(struct tree_##T *this);\ treenode_##T *(*getroot)(struct tree_##T *tree);\ void (*insert)(struct tree_##T *this, T val);\ int (*heightBFS)(treenode_##T *node);\ void (*updateheight)(struct tree_##T *this);\ void (*balanceBFS)(queue(T) *queue, treenode_##T *node);\ void (*linkBFS)(queue(T) *queue, treenode_##T *node, int left, int right, int mid);\ void (*balance)(struct tree_##T *this);\ }tree_##T; #define tree(T) tree_##T #define Isempty(T) int isempty_##T(tree_##T *this){\ return this->size == 0;\ } #define Getroot(T) treenode_##T *getroot_##T(tree_##T *this){\ return this->root;\ } #define HeightBFS(T) int heightBFS_##T(treenode_##T *node){\ if(node == NULL) return 0;\ int heightleft = heightBFS_##T(node->left);\ int heightright = heightBFS_##T(node->right);\ node->height = max(heightleft, heightright) + 1;\ return node->height;\ } #define Updateheight(T) void updateheight_##T(tree_##T *this){\ this->heightBFS(this->root);\ } #define Insert(T) void insert_##T(tree_##T *this, T val){\ treenode_##T *node = newtreenode_##T(val);\ if(!this->root){\ this->root = node;\ this->size++;\ return;\ }\ treenode_##T *iterator = this->root;\ while(iterator){\ if(iterator->val == val){\ printf("this tree has a same val, so can't insert\n");\ return;\ }\ if(val < iterator->val){\ if(!iterator->left){\ iterator->left = node;\ break;\ }\ iterator = iterator->left;\ }\ if(val > iterator->val){\ if(!iterator->right){\ iterator->right = node;\ break;\ }\ iterator = iterator->right;\ }\ }\ this->balance(this);\ this->updateheight(this);\ this->size++;\ } #define BalanceBFS(T) void balanceBFS_##T(queue(T) *queue, treenode_##T *node){\ if(!node)return;\ balanceBFS_##T(queue, node->left);\ queue->push(queue, node);\ balanceBFS_##T(queue, node->right);\ node->parent = NULL;\ node->left = NULL;\ node->right = NULL;\ } #define LinkBFS(T) void linkBFS_##T(queue(T) *queue, treenode_##T *node, int left, int right, int mid){\ if(left >= right) return;\ if(left == mid - 1){\ node->left = *((treenode_##T **) queue->start + left);\ node->left->parent = node;\ }\ else if(left < mid - 1){\ int leftmid = (left + mid) >> 1;\ node->left = *((treenode_##T **) queue->start + leftmid);\ node->left->parent = node;\ linkBFS_##T(queue, node->left, left, mid - 1, leftmid);\ }\ if(right == mid + 1){\ node->right = *((treenode_##T **) queue->start + right);\ node->right->parent = node;\ }\ else if(right > mid + 1){\ int rightmid = (right + mid) >> 1;\ node->right = *((treenode_##T **) queue->start + rightmid);\ node->right->parent = node;\ linkBFS_##T(queue, node->right, mid + 1, right, rightmid);\ }\ } #define Balance(T) void balance_##T(tree_##T *this){\ queue(T) *queue = newqueue(T)(this->root->height * 2);\ this->balanceBFS(queue, this->root);\ int size = queue->size - 1;\ int mid = size >> 1;\ this->root = *((treenode_##T **) queue->start + mid);\ this->linkBFS(queue, this->root, 0, size, mid);\ deletequeue(T) (queue);\ } #define NewTree(T) tree_##T *newtree_##T(){\ tree_##T *tree = (tree_##T *) malloc(sizeof(tree_##T));\ tree->root = NULL;\ tree->size = 0;\ tree->isempty = isempty_##T;\ tree->getroot = getroot_##T;\ tree->insert = insert_##T;\ tree->heightBFS = heightBFS_##T;\ tree->updateheight = updateheight_##T;\ tree->balanceBFS = balanceBFS_##T;\ tree->linkBFS = linkBFS_##T;\ tree->balance = balance_##T;\ return tree;\ } #define DeleteTree(T) void deletetree_##T(tree_##T *tree){\ deletetreenode_##T(tree->root);\ free(tree);\ } #define newtree(T) newtree_##T #define deletetree(T) deletetree_##T #define DEFINETREE(T) TreeNode(T)\ Queue(T)\ Push(T)\ Pop(T)\ Top(T)\ NewQueue(T)\ DeleteQueue(T)\ NodeDFS(T)\ NodeBFS(T)\ InsertL(T)\ InsertR(T)\ Isleaf(T)\ Isroot(T)\ NewTreeNode(T)\ DeleteTreeNode(T)\ Tree(T)\ Isempty(T)\ Getroot(T)\ HeightBFS(T)\ Updateheight(T)\ Insert(T)\ BalanceBFS(T)\ LinkBFS(T)\ Balance(T)\ NewTree(T)\ DeleteTree(T)
DEFINETREE(int) DEFINETREE(char) DEFINETREE(float) DEFINETREE(double)
int main(){ tree(int) *tree = newtree(int)(); tree->insert(tree, 1); tree->insert(tree, 1); tree->insert(tree, 2); tree->insert(tree, 3); tree->insert(tree, -1); tree->insert(tree, -2); tree->root->nodeDFS(tree->root); tree->root->nodeBFS(tree->root); deletetree(int)(tree); }
|