Chinaunix首页 | 论坛 | 博客
  • 博客访问: 3120251
  • 博文数量: 396
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 4209
  • 用 户 组: 普通用户
  • 注册时间: 2016-07-04 13:04
文章分类

全部博文(396)

文章存档

2022年(1)

2021年(2)

2020年(8)

2019年(24)

2018年(135)

2017年(158)

2016年(68)

我的朋友

分类: 嵌入式

2018-11-19 17:16:38

1:概述
   平衡二叉树:对有序序列的一种优化,可以用来高效的查找和遍历等的一种树形结构。

2:原型

  1. GTree* g_tree_new (GCompareFunc key_compare_func);
  2. GTree* g_tree_new_with_data (GCompareDataFunc key_compare_func,
  3.                                                          gpointer key_compare_data);
  4. GTree* g_tree_new_full (GCompareDataFunc key_compare_func,
  5.                                                          gpointer key_compare_data,
  6.                                                          GDestroyNotify key_destroy_func,
  7.                                                          GDestroyNotify value_destroy_func);
  8. void g_tree_insert (GTree *tree,
  9.                                                          gpointer key,
  10.                                                          gpointer value);
  11. void g_tree_replace (GTree *tree,
  12.                                                          gpointer key,
  13.                                                          gpointer value);
  14. gint g_tree_nnodes (GTree *tree);
  15. gint g_tree_height (GTree *tree);
  16. gpointer g_tree_lookup (GTree *tree,
  17.                                                          gconstpointer key);
  18. gboolean g_tree_lookup_extended (GTree *tree,
  19.                                                          gconstpointer lookup_key,
  20.                                                          gpointer *orig_key,
  21.                                                          gpointer *value);
  22. void g_tree_foreach (GTree *tree,
  23.                                                          GTraverseFunc func,
  24.                                                          gpointer user_data);
  25. gpointer g_tree_search (GTree *tree,
  26.                                                          GCompareFunc search_func,
  27.                                                          gconstpointer user_data);
  28. gboolean g_tree_remove (GTree *tree,
  29.                                                          gconstpointer key);
  30. gboolean g_tree_steal (GTree *tree,
  31.                                                          gconstpointer key);
  32. void g_tree_destroy (GTree *tree);

3:实例

  1. #include <stdio.h>
  2. #include <glib.h>
  3. #include <glib/gprintf.h>

  4. struct map {
  5.     int key;
  6.     char *value;
  7. } m[10] = {
  8.     {0,"zero"},
  9.     {1,"one"},
  10.     {2,"two"},
  11.     {3,"three"},
  12.     {4,"four"},
  13.     {5,"five"},
  14.     {6,"six"},
  15.     {7,"seven"},
  16.     {8,"eight"},
  17.     {9,"nine"},
  18. };
  19. typedef struct map map;

  20. static gint
  21. myCompare(gconstpointer p1, gconstpointer p2)
  22. {
  23.     const char *= p1;
  24.     const char *= p2;

  25.     return *- *b;
  26. }

  27. static gint
  28. mySearch(gconstpointer p1, gconstpointer p2)
  29. {
  30.     return myCompare(p1, p2);
  31. }

  32. static gint
  33. myTraverse(gpointer key, gpointer value, gpointer fmt)
  34. {
  35.     g_printf(fmt, *(gint*)key, (gchar*)value);
  36.     
  37.     return FALSE;
  38. }

  39. static void
  40. test_avl_tree(void)
  41. {
  42.     GTree *tree;
  43.     gint i;

  44. // GTree* g_tree_new(GCompareFunc key_compare_func);
  45.     tree = g_tree_new(myCompare);
  46.     
  47. // void g_tree_insert(GTree *tree, gpointer key, gpointer value);
  48.     for (= 0; i < sizeof(m)/sizeof(m[0]); i++)
  49.         g_tree_insert(tree, &m[i].key, m[i].value);

  50. // void g_tree_foreach(GTree *tree, GTraverseFunc func, gpointer user_data);
  51.     g_printf("Now the tree:\n");
  52.     g_tree_foreach(tree, myTraverse, "Key:\t%d\t\tVaule:\t%s\n");
  53. // gint g_tree_nnodes(GTree *tree);
  54.     g_printf("The tree should have '%d' items now.\t\tResult: %d.\n", 10, g_tree_nnodes(tree));
  55. // gint g_tree_height(GTree *tree);
  56.     g_printf("The height of tree is '%d' now.\n", g_tree_height(tree));

  57. // void g_tree_replace(GTree *tree, gpointer key, gpointer value);
  58.     g_tree_replace(tree, &m[3].key, "3333");
  59.     g_printf("Now the vaule of '%d' should be '3333' now.\n", m[3].key);
  60.     g_tree_foreach(tree, myTraverse, "Key:\t%d\t\tVaule:\t%s\n");

  61.     gchar *tmp = NULL;
  62. // gpointer g_tree_lookup(GTree *tree, gconstpointer key);
  63.     g_printf("Now the vaule of '%d' should be '%s' now[lookup].\n", 
  64.             m[3].key, 
  65.             (tmp = (gchar *)g_tree_lookup(tree, &m[3].key)) != NULL ? tmp : NULL);

  66. // gboolean g_tree_remove(GTree *tree, gconstpointer key);
  67.     gboolean b = g_tree_remove(tree, &m[3].key);
  68.     g_printf("The key '%d' has %sbeen found and removed now.\n", m[3].key, b ? "" : "NOT");

  69. // gpointer g_tree_search(GTree *tree, GCompareFunc search_func, gconstpointer user_data);
  70.     g_printf("Now the vaule which should be removed of '%d' should be '%s' now[search].\n", 
  71.             m[3].key, 
  72.             (tmp = (gchar *)g_tree_search(tree, mySearch, &m[3].key)) != NULL ? tmp : NULL);

  73.     g_printf("Now the tree look like:\n");
  74.     g_tree_foreach(tree, myTraverse, "Key:\t%d\t\tVaule:\t%s\n");

  75. // void g_tree_destroy(GTree *tree);
  76.     g_tree_destroy(tree);
  77. }

  78. int
  79. main(void)
  80. {
  81.     printf("BEGIN:\n************************************************************\n");

  82.     test_avl_tree();

  83.     printf("\n************************************************************\nDONE\n");

  84.     return 0;
  85. }

4:结果
  1. BEGIN:
  2. ************************************************************
  3. Now the tree:
  4. Key: 0 Vaule: zero
  5. Key: 1 Vaule: one
  6. Key: 2 Vaule: two
  7. Key: 3 Vaule: three
  8. Key: 4 Vaule: four
  9. Key: 5 Vaule: five
  10. Key: 6 Vaule: six
  11. Key: 7 Vaule: seven
  12. Key: 8 Vaule: eight
  13. Key: 9 Vaule: nine
  14. The tree should have '10' items now. Result: 10.
  15. The height of tree is '4' now.
  16. Now the vaule of '3' should be '3333' now.
  17. Key: 0 Vaule: zero
  18. Key: 1 Vaule: one
  19. Key: 2 Vaule: two
  20. Key: 3 Vaule: 3333
  21. Key: 4 Vaule: four
  22. Key: 5 Vaule: five
  23. Key: 6 Vaule: six
  24. Key: 7 Vaule: seven
  25. Key: 8 Vaule: eight
  26. Key: 9 Vaule: nine
  27. Now the vaule of '3' should be '3333' now[lookup].
  28. The key '3' has been found and removed now.
  29. Now the vaule which should be removed of '3' should be '(null)' now[search].
  30. Now the tree look like:
  31. Key: 0 Vaule: zero
  32. Key: 1 Vaule: one
  33. Key: 2 Vaule: two
  34. Key: 4 Vaule: four
  35. Key: 5 Vaule: five
  36. Key: 6 Vaule: six
  37. Key: 7 Vaule: seven
  38. Key: 8 Vaule: eight
  39. Key: 9 Vaule: nine

  40. ************************************************************
  41. DONE

5:小结
  • 创建: g_tree_new()
  • 插入: g_tree_insert()
  • 查找: g_tree_lookup() g_tree_search()
  • 删除: g_tree_remove()
  • 属性: g_tree_nnodes() g_tree_height()
  • 遍历: g_tree_foreach()
  • 销毁: g_tree_destroy()
阅读(2297) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~