예제 #1
0
/*
 * 销毁AVL树
 *
 * 递归方法
 */
void destroy_avltree(AVLTree tree)
{
    if (tree==NULL)
        return ;

    if (tree->left != NULL)
        destroy_avltree(tree->left);
    if (tree->right != NULL)
        destroy_avltree(tree->right);

    free(tree);
}
예제 #2
0
파일: ktype.c 프로젝트: gitj/katcp_devel
void destroy_type_list_katcp(struct katcp_dispatch *d)
{
  struct katcp_shared *s;
  struct katcp_type **ts;
  struct katcp_type *t;
  int size, i;

  sane_shared_katcp(d);

  s = d->d_shared;
  if (s == NULL)
    return;

  ts = s->s_type;
  size = s->s_type_count;
  
  if (ts == NULL)
    return;
  
  for (i=0; i<size; i++){
    t = ts[i];
    if (t != NULL && t->t_dep == 0){
      destroy_avltree(t->t_tree, t->t_free); 
      t->t_tree = NULL;
    }
  }

  for (i=0; i<size; i++){
    t = ts[i];
    if (t != NULL && t->t_dep > 0){
      destroy_avltree(t->t_tree, t->t_free); 
      t->t_tree = NULL;
    }
  }

  for (i=0; i<size; i++){
    destroy_type_katcp(ts[i]);
  }
  
  free(ts);
  ts = NULL;

  s->s_type = ts;
  s->s_type_count = 0;
}
예제 #3
0
파일: ktype.c 프로젝트: gitj/katcp_devel
void flush_type_katcp(struct katcp_type *t)
{
  if (t && t->t_tree && t->t_free) {
#ifdef DEBUG
    fprintf(stderr, "%s: about to destroy type tree\n", __func__);
#endif
    destroy_avltree(t->t_tree, t->t_free);
  }
}
예제 #4
0
파일: ktype.c 프로젝트: gitj/katcp_devel
void destroy_type_katcp(struct katcp_type *t)
{
  if (t != NULL){
#if DEBUG > 1
    fprintf(stderr, "katcp_type: destroy type <%s>\n", t->t_name);
#endif
    if (t->t_name != NULL) { free(t->t_name); t->t_name = NULL; }
    t->t_dep = 0;
    if (t->t_tree != NULL) { 
      destroy_avltree(t->t_tree, t->t_free); 
      t->t_tree = NULL; 
    }
    t->t_print   = NULL;
    t->t_free    = NULL;
    t->t_copy    = NULL;
    t->t_compare = NULL;
    t->t_parse   = NULL;
    free(t);
  }
}
예제 #5
0
파일: avltree.c 프로젝트: BlastTNG/flight
int main(int argc, char *argv[])
{
  struct avl_tree *tree;
#if 0 
  int fd, fsize;
  struct stat file_stats;
  char *buffer;
#endif

#if 1 
  struct avl_node *a, *b, *c, *d, *e, *f, *g, *h, *i, *j, *k, *l, *m, *n, *o, *p, *q;
#endif

#if 0
  fd = open("/usr/share/dict/words", O_RDONLY);
  if (fd < 0){
#if DEBUG >0
    fprintf(stderr,"avl_tree: cannot open file\n");
#endif
    return 0;
  }
  
  if (fstat(fd, &file_stats) < 0){
#if DEBUG >0
    fprintf(stderr,"avl_tree: cannot stat file\n");
#endif
    close(fd);
    return 0;
  }
  
  fsize = file_stats.st_size;

  buffer = mmap(NULL, fsize, PROT_READ, MAP_SHARED, fd, 0);
  
  if (buffer == MAP_FAILED){
#if DEBUG >0
    fprintf(stderr,"avl_tree: cannot mmap file\n");
#endif
    close(fd);
    return 0;
  }

  tree = create_avltree();
  
  if (add_file_words_to_avltree(tree, buffer, fsize) < 0){
#if DEBUG >0
    fprintf(stderr,"avl_tree: cannot populate file into tree\n");
#endif
    munmap(buffer, fsize);
    close(fd);
    destroy_avltree(tree);
    tree = NULL;
    return 0;
  }
  
 // print_avltree(tree->t_root, 0);
  check_balances_avltree(tree->t_root, 0);

#endif 

#if 1 
  tree = create_avltree();
  
  a = create_node_avltree("adam", NULL);
  b = create_node_avltree("ben", NULL);
  c = create_node_avltree("charlie", NULL);
  d = create_node_avltree("doug", NULL);
  e = create_node_avltree("eric", NULL);
  f = create_node_avltree("fred", NULL);
  g = create_node_avltree("gareth", NULL);
  h = create_node_avltree("dennis", NULL);
  i = create_node_avltree("thomas", NULL);
  j = create_node_avltree("mark", NULL);
  k = create_node_avltree("megan", NULL);
  l = create_node_avltree("zack", NULL);
  m = create_node_avltree("zack1", NULL);
  n = create_node_avltree("zack2", NULL);
  o = create_node_avltree("test", NULL);
  p = create_node_avltree("amy", NULL);
  q = create_node_avltree("alex", NULL);
  
  if (add_node_avltree(tree, h) < 0)
    fprintf(stderr,"avl_tree: couldn't add\n");
  if (add_node_avltree(tree, i) < 0)
    fprintf(stderr,"avl_tree: couldn't add\n");
  if (add_node_avltree(tree, j) < 0)
    fprintf(stderr,"avl_tree: couldn't add\n");
  if (add_node_avltree(tree, c) < 0)
    fprintf(stderr,"avl_tree: couldn't add\n");
  if (add_node_avltree(tree, a) < 0)
    fprintf(stderr,"avl_tree: couldn't add\n");
  if (add_node_avltree(tree, b) < 0)
    fprintf(stderr,"avl_tree: couldn't add\n");
  if (add_node_avltree(tree, d) < 0)
    fprintf(stderr,"avl_tree: couldn't add\n");
  if (add_node_avltree(tree, e) < 0)
    fprintf(stderr,"avl_tree: couldn't add\n");
  if (add_node_avltree(tree, f) < 0)
    fprintf(stderr,"avl_tree: couldn't add\n");
  if (add_node_avltree(tree, g) < 0)
    fprintf(stderr,"avl_tree: couldn't add\n");
  if (add_node_avltree(tree, k) < 0)
    fprintf(stderr,"avl_tree: couldn't add\n");
  if (add_node_avltree(tree, l) < 0)
    fprintf(stderr,"avl_tree: couldn't add\n");
  if (add_node_avltree(tree, m) < 0)
    fprintf(stderr,"avl_tree: couldn't add\n");
  if (add_node_avltree(tree, n) < 0)
    fprintf(stderr,"avl_tree: couldn't add\n");
  if (add_node_avltree(tree, o) < 0)
    fprintf(stderr,"avl_tree: couldn't add\n");
  if (add_node_avltree(tree, p) < 0)
    fprintf(stderr,"avl_tree: couldn't add\n");
  if (add_node_avltree(tree, q) < 0)
    fprintf(stderr,"avl_tree: couldn't add\n");
  if (add_node_avltree(tree, create_node_avltree("andrea", NULL)) < 0)
    fprintf(stderr,"avl_tree: couldn't add\n");
  if (add_node_avltree(tree, create_node_avltree("nicole", NULL)) < 0)
    fprintf(stderr,"avl_tree: couldn't add\n");
 /* 
  if (add_node_avltree(tree, create_node_avltree("fred", NULL)) < 0)
    fprintf(stderr,"avl_tree: couldn't add\n");
  if (add_node_avltree(tree, create_node_avltree("adam", NULL)) < 0)
    fprintf(stderr,"avl_tree: couldn't add\n");
  if (add_node_avltree(tree, create_node_avltree("eric", NULL)) < 0)
    fprintf(stderr,"avl_tree: couldn't add\n");
  if (add_node_avltree(tree, create_node_avltree("alan", NULL)) < 0)
    fprintf(stderr,"avl_tree: couldn't add\n");
  if (add_node_avltree(tree, create_node_avltree("abe", NULL)) < 0)
    fprintf(stderr,"avl_tree: couldn't add\n");
  if (add_node_avltree(tree, create_node_avltree("ben", NULL)) < 0)
    fprintf(stderr,"avl_tree: couldn't add\n");
  if (add_node_avltree(tree, create_node_avltree("basil", NULL)) < 0)
    fprintf(stderr,"avl_tree: couldn't add\n");
  if (add_node_avltree(tree, create_node_avltree("barry", NULL)) < 0)
    fprintf(stderr,"avl_tree: couldn't add\n");
  
  if (del_name_node_avltree(tree, "abe") < 0)
    fprintf(stderr,"avl_tree: couldn't delete\n");
  
  if (del_name_node_avltree(tree, "fred") < 0)
    fprintf(stderr,"avl_tree: couldn't delete\n");
  
  if (del_name_node_avltree(tree, "eric") < 0)
    fprintf(stderr,"avl_tree: couldn't delete\n");
  
  if (del_name_node_avltree(tree, "ben") < 0)
    fprintf(stderr,"avl_tree: couldn't delete\n");
  */

  print_avltree(NULL, tree->t_root, 0, NULL);
  //check_balances_avltree(tree->t_root, 0);
  
  
  while (0){
    if (del_node_avltree(tree, tree->t_root, NULL) < 0)
      break;
    else {
      print_avltree(NULL, tree->t_root, 0, NULL);
      check_balances_avltree(tree->t_root, 0);
    }
  }

#if 1 
  print_inorder_avltree(NULL, tree->t_root, NULL, 0);
  
  while ((a = walk_inorder_avltree(tree->t_root)) != NULL){
    
    if (a != NULL){
#ifdef DEBUG
      fprintf(stderr, "walk: <%s>\n", a->n_key);
#endif
    }

  }
#ifdef DEBUG
 fprintf(stderr, "walk round 2\n");
#endif
  
  while ((a = walk_inorder_avltree(tree->t_root)) != NULL){
    
    if (a != NULL){
#ifdef DEBUG
      fprintf(stderr, "walk: <%s>\n", a->n_key);
#endif
    }

  }


#endif

#if 0 

  if (del_name_node_avltree(tree, "test") < 0)
    fprintf(stderr,"avl_tree: couldn't delete\n");
  print_avltree(tree->t_root, 0);
  check_balances_avltree(tree->t_root, 0);
  
  if (del_name_node_avltree(tree, "charlie") < 0)
    fprintf(stderr,"avl_tree: couldn't delete\n");
  print_avltree(tree->t_root, 0);
  check_balances_avltree(tree->t_root, 0);
  
  if (del_name_node_avltree(tree, "ben") < 0)
    fprintf(stderr,"avl_tree: couldn't delete\n");
  print_avltree(tree->t_root, 0);
  check_balances_avltree(tree->t_root, 0);
  
  if (del_name_node_avltree(tree, "adam") < 0)
    fprintf(stderr,"avl_tree: couldn't delete\n");
  print_avltree(tree->t_root, 0);
  check_balances_avltree(tree->t_root, 0);
  
  if (del_name_node_avltree(tree, "mark") < 0)
    fprintf(stderr,"avl_tree: couldn't delete\n");
  print_avltree(tree->t_root, 0);
  check_balances_avltree(tree->t_root, 0);
  
  if (del_name_node_avltree(tree, "doug") < 0)
    fprintf(stderr,"avl_tree: couldn't delete\n");
  print_avltree(tree->t_root, 0);
  check_balances_avltree(tree->t_root, 0);
  
  if (del_name_node_avltree(tree, "nicole") < 0)
    fprintf(stderr,"avl_tree: couldn't delete\n");
  print_avltree(tree->t_root, 0);
  check_balances_avltree(tree->t_root, 0);
  
  if (del_name_node_avltree(tree, "eric") < 0)
    fprintf(stderr,"avl_tree: couldn't delete\n");
  print_avltree(tree->t_root, 0);
  check_balances_avltree(tree->t_root, 0);
  
  if (del_name_node_avltree(tree, "fred") < 0)
    fprintf(stderr,"avl_tree: couldn't delete\n");
  print_avltree(tree->t_root, 0);
  check_balances_avltree(tree->t_root, 0);
  
  if (del_name_node_avltree(tree, "andrea") < 0)
    fprintf(stderr,"avl_tree: couldn't delete\n");
  print_avltree(tree->t_root, 0);
  check_balances_avltree(tree->t_root, 0);
  
  if (del_name_node_avltree(tree, "thomas") < 0)
    fprintf(stderr,"avl_tree: couldn't delete\n");
  print_avltree(tree->t_root, 0);
  check_balances_avltree(tree->t_root, 0);
  
  if (del_name_node_avltree(tree, "dennis") < 0)
    fprintf(stderr,"avl_tree: couldn't delete\n");
  print_avltree(tree->t_root, 0);
  check_balances_avltree(tree->t_root, 0);
  
#endif

#endif
  
  destroy_avltree(tree, NULL);
  tree = NULL;
  
  return 0;
}