Example #1
0
/*
 *	PostOrder
 */
static int WalkNodePostOrder(rbnode_t *X,
			     int (*callback)(void *, void*), void *context)
{
	int rcode;

	if (X->Left != NIL) {
		rcode = WalkNodeInOrder(X->Left, callback, context);
		if (rcode != 0) return rcode;
	}

	if (X->Right != NIL) {
		rcode = WalkNodeInOrder(X->Right, callback, context);
		if (rcode != 0) return rcode;
	}

	rcode = callback(context, X->Data);
	if (rcode != 0) return rcode;

	return 0;		/* we know everything returned zero */
}
/*
 *	Walk the entire tree.  The callback function CANNOT modify
 *	the tree.
 *
 *	The callback function should return 0 to continue walking.
 *	Any other value stops the walk, and is returned.
 */
int rbtree_walk(rbtree_t *tree, RBTREE_ORDER order,
		int (*callback)(void *, void *), void *context)
{
	if (tree->Root == NIL) return 0;

	switch (order) {
	case PreOrder:
		return WalkNodePreOrder(tree->Root, callback, context);
	case InOrder:
		return WalkNodeInOrder(tree->Root, callback, context);
	case PostOrder:
		return WalkNodePostOrder(tree->Root, callback, context);

	default:
		break;
	}

	return -1;
}