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


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

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


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

示例1: destroy_right

/* --- Function: static void destroy_right(AvlTree tree, AvlTreeNode node) --- */
static void destroy_right(AvlTree tree, AvlTreeNode node)
{
  AvlTreeNode *position;

  /* Destruction of an empty tree is not allowed.. */
  if (tree->size == 0)
    return;

  /* Determine where to destroy nodes... */
  if (node == NULL)
    position = &tree->root;
  else
    position = &node->right;

  /* Destroy the nodes... */
  if (*position != NULL)
    {
      destroy_left(tree, *position);
      destroy_right(tree, *position);

      if (tree->destroy != NULL)
        {
          /* Call a user-defined function to free dynamically allocated data */
          tree->destroy((*position)->data);
        }
      /* Now, free the node itself... */
      free(*position);
      *position = NULL;

      /* Adjust the size of the tree to account for the destroyed node... */
      tree->size--;
    }
}
开发者ID:AsamQi,项目名称:levawc,代码行数:34,代码来源:avltree.c

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