⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 tree_movers.c

📁 linked list linked list tree struc
💻 C
字号:
/*
 * Wei Cui
 * 981503
 * wec924 */
 
#include <stdio.h>
#include <tree.h>

/* tree movers : */


TITEM __Curr(TREEPTR mytree)
{
  if (!mytree||!mytree->curr_node)
  { 
    return NULL;
  }
  else
  {
    return mytree->curr_node->item;
  } 
}


TITEM TreeCurr(TREEPTR mytree)
{
  return __Curr(mytree);
}

int TreeSize(TREEPTR mytree)
{
  if (mytree) return mytree->size;
  else {
    printf("The tree doesn't exist!!!\n");
    return -1;
  }
}


TITEM TreeFirst(TREEPTR mytree)
{
  if (mytree) mytree->curr_node = mytree->first_node;
  return __Curr(mytree);
}


TITEM TreeLast(TREEPTR mytree)
{
  if (mytree) mytree->curr_node = mytree->last_node;
  return __Curr(mytree);
}


TITEM TreeNext(TREEPTR mytree)
{
  if ( !mytree || mytree->curr_node == mytree->last_node )
  {
    return NULL;
  }
  else
  {
    mytree->curr_node = mytree->curr_node->next;
    return __Curr(mytree);
  }
}


TITEM TreePrev(TREEPTR mytree)
{
  if ( !mytree || mytree->curr_node == mytree->first_node )
  {
    return NULL;
  }
  else
  {
    mytree->curr_node = mytree->curr_node->prev;
    return __Curr(mytree);
  }
}

TITEM TreeKeySearch(TREEPTR mytree, int Comparator(TITEM, void *), void *comparison)
{
  int callresult;
  NODE *temp;
  if (!mytree || !mytree->curr_node )                      /*No tree or the tree is empty. */
  {
    return NULL;
  }
  else
  {
    temp = mytree->curr_node;
    callresult = (*Comparator)(temp->item, comparison);
    /*routine pointer call */
    while ( callresult!=0 && temp )
    {
      if (callresult == -1) temp = mytree->curr_node->left;
      else temp = mytree->curr_node->right;
      if (temp) mytree->curr_node = temp;
      callresult = (*Comparator)(mytree->curr_node->item, comparison);
     /*routine pointer call */
    }
    if ( callresult == 0 )
    {
      return mytree->curr_node->item;
    }
    else
    {
      return NULL;
    }
  }
}

TITEM TreeSearch(TREEPTR mytree, int Comparator(TITEM, void *), void *comparison)
{
  int callresult;
  if (!mytree || !mytree->curr_node )                      /*No tree or the tree is empty. */
  {
    return NULL;
  }
  else
  {
    callresult = (*Comparator)(mytree->curr_node->item, comparison);
    /*routine pointer call */
    while ( !callresult && mytree->curr_node->next )
    {
      mytree->curr_node = mytree->curr_node->next;
      callresult = (*Comparator)(mytree->curr_node->item, comparison);
     /*routine pointer call */
    }
    if ( callresult == 1 )
    {
      return mytree->curr_node->item;
    }
    else
    {
      return NULL;
    }
  }
}




/* For tree structure : */

TITEM TreeRoot(TREEPTR mytree)
{
  if ( !mytree || !mytree->root_node )
  {
    return NULL;
  }
  else
  {
    mytree->curr_node = mytree->root_node;
    return __Curr(mytree);
  }
}

TITEM TreeParent(TREEPTR mytree)
{
  if ( mytree->curr_node == mytree->root_node )
  {
    return NULL;
  }
  else
  {
    mytree->curr_node = mytree->curr_node->parent;
    return __Curr(mytree);
  }
}

TITEM TreeLeft(TREEPTR mytree)
{
  if ( !mytree || !mytree->curr_node || !mytree->curr_node->left)
  {
    return NULL;
  }
  else
  {
    mytree->curr_node = mytree->curr_node->left;
    return __Curr(mytree);
  }
}

TITEM TreeRight(TREEPTR mytree)
{
  if ( !mytree || !mytree->curr_node || !mytree->curr_node->right )
  {
    return NULL;
  }
  else
  {
    mytree->curr_node = mytree->curr_node->right;
    return __Curr(mytree);
  }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -