当前位置: 首页>>代码示例>>C++>>正文


C++ AvlTree::compare方法代码示例

本文整理汇总了C++中AvlTree::compare方法的典型用法代码示例。如果您正苦于以下问题:C++ AvlTree::compare方法的具体用法?C++ AvlTree::compare怎么用?C++ AvlTree::compare使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在AvlTree的用法示例。


在下文中一共展示了AvlTree::compare方法的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。

示例1: lookup

/* --- Function: static int lookup(AvlTree tree, AvlTreeNode node, void **data) --- */
static int lookup(AvlTree tree, AvlTreeNode node, void **data)
{
  int cmpval, retval;

  if (node == NULL)
    {
      /* Return that the data was not found... */
      return -1;
    }

  cmpval = tree->compare(*data, node->data);

  if (cmpval < 0)
    {
      /* Move to the left... */
      retval = lookup(tree, node->left, data);
    }
  else if (cmpval > 0)
    {
      /* Move to the right... */
      retval = lookup(tree, node->right, data);
    }
  else
    {
      /* Node found - or hidden..! */
      if (!(node->hidden) )
        {
          /* Pass back the data from the tree... */
          *data = node->data;
          retval = 0;
        }
      else
        {
          /* Return that the data was not found! */
          return -1;
        }
    }

  return retval;
}
开发者ID:AsamQi,项目名称:levawc,代码行数:41,代码来源:avltree.c

示例2: hide

/* --- Function: static int hide(AvlTree tree, AvlTreeNode node, const void *data) --- */
static int hide(AvlTree tree, AvlTreeNode node, const void *data)
{
  int cmpval, retval;

  if (node == NULL) 
    {
      /* Return that the data was not found... */
      return -1;
    }

  cmpval = tree->compare(data, node->data);

  if (cmpval < 0) 
    {
      /* Move to the left... */
      retval = hide(tree, node->left, data);
    }
  else if (cmpval > 0) 
    {
      /* Move to the right... */
      retval = hide(tree, node->right, data);
    }
  else /* Node found - hidden or not..! */
    {
      if (!(node->hidden))
        {
          /* Mark the node as hidden... */
          node->hidden = 1;
          /* Return success... */
          retval = 0;
        }
      else
        return -1;
    }

  return retval;  
}
开发者ID:AsamQi,项目名称:levawc,代码行数:38,代码来源:avltree.c

示例3: insert

/* --- Function: static int insert(AvlTree tree, AvlTreeNode *node, const void *data, int *balanced) --- */
static int insert(AvlTree tree, AvlTreeNode *node, const void *data, int *balanced)
{
  //  AvlTreeNode avl_data;
  int cmpval, retval;

  /* Insert the data into the tree... */
  if ((*node) == NULL)
    {
      return avltree_insleft(tree, *node, data);
    }
  else
    {
      /* Handle insertion into a tree that is not empty... */
      cmpval = tree->compare(data, (*node)->data);

      if (cmpval < 0)
        {
          /* Move to the left... */
          if ((*node)->left == NULL)
            {
              if (avltree_insleft(tree, *node, data) != 0)
                return -1;

              *balanced = 0;
            }
          else
            {
              if ((retval = insert(tree, &(*node)->left, data, balanced)) != 0) 
                {
                  return retval;
                }
            }

          /* Ensure that the tree remains balanced... */
          if (!(*balanced))
            {
              switch ((*node)->factor) 
                {
                case AVL_LFT_HEAVY:
                  rotate_left(node);
                  *balanced = 1;
                  break;
                case AVL_BALANCED:
                  (*node)->factor = AVL_LFT_HEAVY;
                  break;
                case AVL_RGT_HEAVY:
                  (*node)->factor = AVL_BALANCED;
                  *balanced = 1;
                }
            }
        } /* if (cmpval < 0) - end */
      else if (cmpval > 0) 
        {
          /* Move to the right... */
          if ((*node)->right == NULL)
            {
              if (avltree_insright(tree, *node, data) != 0)
                return -1;

              *balanced = 0;
            }
          else 
            {
              if ((retval = insert(tree, &(*node)->right, data, balanced)) != 0)
                {
                  return retval;
                }
            }
         
          /* Ensure that the tree remains balanced... */
          if (!(*balanced)) 
            {
              switch ((*node)->factor) 
                {
                case AVL_LFT_HEAVY:
                  (*node)->factor = AVL_BALANCED;
                  *balanced = 1;
                  break;
                case AVL_BALANCED:
                  (*node)->factor = AVL_RGT_HEAVY;
                  break;
                case AVL_RGT_HEAVY:
                  rotate_right(node);
                  *balanced = 1;
                }
            }
        } /* if (cmpval > 0) - end */
      else
        {
          /* Handle finding a copy of the data... */
          if (!((*node)->hidden))
            {
              /* Do nothing since the data is in the tree - and not hidden... */
              return 1;
            }
          else
            {
              /* Insert the new data - and mark it as not hidden... */
              if (tree->destroy != NULL) 
//.........这里部分代码省略.........
开发者ID:AsamQi,项目名称:levawc,代码行数:101,代码来源:avltree.c


注:本文中的AvlTree::compare方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。