-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathBinaryTree.h
81 lines (66 loc) · 2.7 KB
/
BinaryTree.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
#ifndef BINARYTREE_INCLUDED
#define BINARYTREE_INCLUDED
/*
Reusable implementation of an AVL balanced binarytree, which serves well as a general
collection datastructure, as both insertion and searching scale as ln(n), while size
of the tree scales linear with the amount of data stored (n).
I will use this tree structure most of the time if i need a collection. At some points a
well-designed hash-table would perform better. But a hashtable only works if you can
predict the required size: too small and it will be slow, too large and it will waste
memory. A linked implementation of a balanced binary tree, on the other hand, dynamically
sizes, 'so never really wastes memory', and, though slower than a suitably large hashtable,
the AVL balancing algorithm reduces the worst case performance to logarithmic in the tree's
height, so it is practically always fast enough.
-- Wim Decelle
*/
#include "Features.h"//synchronization of datastructures is considered a feature
#include "LinkedList.h"//required to implement the tolist conversions
#ifdef SYNCHRONIZED//synchronization is platform specific here :: so porting requires changes here
#include <windows.h>
#endif
//we need max macro
#ifndef getmax
#define getmax(a,b) (((a)>(b))?(a):(b))
#endif
typedef int (*BTCompare)(void *,void *);
typedef struct BTNodeStruct
{
void * key;
void * data;
struct BTNodeStruct * left;
struct BTNodeStruct * right;
struct BTNodeStruct * parent;
unsigned int height;
} BinaryTreeNode;//5*sizeof(pointer) + 1*sizeof(unsigned int) = 24 on 32bit platform and 44 on a 64bit platform
typedef struct
{
unsigned int itemcount;
BinaryTreeNode * root;
BTCompare compare;
#ifdef SYNCHRONIZED
LPCRITICAL_SECTION btsync;
#endif
} BinaryTree;
typedef struct
{
BinaryTree * tree;
BinaryTreeNode * curnode;
} BinaryTreeEnum;
BinaryTree * BT_create(BTCompare comparisonfunction);
int BT_insert(BinaryTree * pThis,void * key, void * data);
void * BT_find(BinaryTree * pThis,void * tofind);
void * BT_remove(BinaryTree * pThis,void * toremove);
void BT_delete(BinaryTree * todelete);
BinaryTreeEnum * BT_newenum(BinaryTree * oftree);
void * BT_next(BinaryTreeEnum * btenum);
void * BT_previous(BinaryTreeEnum * btenum);
void * BT_reset(BinaryTreeEnum * btenum);
void * BT_end(BinaryTreeEnum * btenum);
void BT_enumdelete(BinaryTreeEnum * todelete);
void * BT_leftmost(BinaryTree * tree);
void * BT_rightmost(BinaryTree * tree);
LinkedList * BT_values(BinaryTree * tree);
LinkedList * BT_keys(BinaryTree * tree);
LinkedList * BT_pairs(BinaryTree * tree);//PAIRS NEED TO BE CLEANED UP LATER ON!!
void ** BT_toarray(BinaryTree * tree, unsigned int * count);
#endif