Beispiel #1
0
static int check_list_order(struct list *list, int sort_criteria)
{
	struct list *item;

	item = list_head(list);
	while (item && item->next) {
		if (sort_criteria > 0) {
			if (data_compare(item->data, item->next->data) > 0) {
				return EXIT_FAILURE;
			}
		} else {
			if (data_compare(item->data, item->next->data) < 0) {
				return EXIT_FAILURE;
			}
		}
		item = item->next;
	}

	return 0;
}
/* ---------------------------------------------------------------------- */
int btree_Insert(BTREE tree, void *data)
{
  btnode parent, node, newnode;

  newnode = node_Make(tree, data);
  if (!newnode) {
    return 1;
  }

  parent = NULL;
  node = root(tree);

  while (node) {
    parent = node;
    if (data_compare(tree, data, data(tree, node)) < 0) {
      node = left(node);
    }
    else {
      node = right(node);
    }
  }

  parent(newnode) = parent;

  if (!parent) {
    root(tree) = newnode;
  }
  else {
    if (data_compare(tree, data, data(tree, parent)) < 0) {
      left(parent) = newnode;
    }
    else {
      right(parent) = newnode;
    }
  }
  return 0;
}
/* ---------------------------------------------------------------------- */
static btnode node_Search(BTREE tree, btnode node, void *key)
{
  int result;

  if (node) {
    while (node && (result = data_compare(tree, data(tree, node), key))) {
      if (result > 0) {
        node = left(node);
      }
      else {
        node = right(node);
      }
    }
  }
  return node;
}