1 /*-------------------------------------------------------------------------
4 * interface for PostgreSQL generic Red-Black binary tree package
6 * Copyright (c) 2009-2021, PostgreSQL Global Development Group
9 * src/include/lib/rbtree.h
11 *-------------------------------------------------------------------------
17 * RBTNode is intended to be used as the first field of a larger struct,
18 * whose additional fields carry whatever payload data the caller needs
19 * for a tree entry. (The total size of that larger struct is passed to
20 * rbt_create.) RBTNode is declared here to support this usage, but
21 * callers must treat it as an opaque struct.
23 typedef struct RBTNode
25 char color
; /* node's current color, red or black */
26 struct RBTNode
*left
; /* left child, or RBTNIL if none */
27 struct RBTNode
*right
; /* right child, or RBTNIL if none */
28 struct RBTNode
*parent
; /* parent, or NULL (not RBTNIL!) if none */
31 /* Opaque struct representing a whole tree */
32 typedef struct RBTree RBTree
;
34 /* Available tree iteration orderings */
35 typedef enum RBTOrderControl
37 LeftRightWalk
, /* inorder: left child, node, right child */
38 RightLeftWalk
/* reverse inorder: right, node, left */
42 * RBTreeIterator holds state while traversing a tree. This is declared
43 * here so that callers can stack-allocate this, but must otherwise be
44 * treated as an opaque struct.
46 typedef struct RBTreeIterator RBTreeIterator
;
51 RBTNode
*(*iterate
) (RBTreeIterator
*iter
);
52 RBTNode
*last_visited
;
56 /* Support functions to be provided by caller */
57 typedef int (*rbt_comparator
) (const RBTNode
*a
, const RBTNode
*b
, void *arg
);
58 typedef void (*rbt_combiner
) (RBTNode
*existing
, const RBTNode
*newdata
, void *arg
);
59 typedef RBTNode
*(*rbt_allocfunc
) (void *arg
);
60 typedef void (*rbt_freefunc
) (RBTNode
*x
, void *arg
);
62 extern RBTree
*rbt_create(Size node_size
,
63 rbt_comparator comparator
,
64 rbt_combiner combiner
,
65 rbt_allocfunc allocfunc
,
66 rbt_freefunc freefunc
,
69 extern RBTNode
*rbt_find(RBTree
*rbt
, const RBTNode
*data
);
70 extern RBTNode
*rbt_leftmost(RBTree
*rbt
);
72 extern RBTNode
*rbt_insert(RBTree
*rbt
, const RBTNode
*data
, bool *isNew
);
73 extern void rbt_delete(RBTree
*rbt
, RBTNode
*node
);
75 extern void rbt_begin_iterate(RBTree
*rbt
, RBTOrderControl ctrl
,
76 RBTreeIterator
*iter
);
77 extern RBTNode
*rbt_iterate(RBTreeIterator
*iter
);