Sfoglia il codice sorgente

r.li.daemon: add avl_destroy()

git-svn-id: https://svn.osgeo.org/grass/grass/trunk@59025 15284696-431f-4ddb-bdfa-cd5b030d7da7
Markus Metz 11 anni fa
parent
commit
8356b009ef
2 ha cambiato i file con 28 aggiunte e 1 eliminazioni
  1. 27 1
      raster/r.li/r.li.daemon/avl.c
  2. 1 0
      raster/r.li/r.li.daemon/avl.h

+ 27 - 1
raster/r.li/r.li.daemon/avl.c

@@ -33,7 +33,7 @@ void avl_rotation_rr(avl_node * critical);
 void printAVL(avl_node * r);
 
 
-/* define function declsred in avl.h */
+/* define function declared in avl.h */
 
 avl_tree avl_make(const generic_cell k, const long n)
 {
@@ -57,6 +57,32 @@ avl_tree avl_make(const generic_cell k, const long n)
     return root;
 }
 
+void avl_destroy(avl_tree root)
+{
+    struct avl_node *it;
+    struct avl_node *save = root;
+
+    /*
+    Rotate away the left links so that
+    we can treat this like the destruction
+    of a linked list
+    */
+    while((it = save) != NULL) {
+	if (it->left_child == NULL) {
+	    /* No left links, just kill the node and move on */
+	    save = it->right_child;
+	    G_free(it);
+	    it = NULL;
+	}
+	else {
+	    /* Rotate away the left link and check again */
+	    save = it->left_child;
+	    it->left_child = save->right_child;
+	    save->right_child = it;
+	}
+    }
+    
+}
 
 long howManyCell(const avl_tree root, const generic_cell k)
 {

+ 1 - 0
raster/r.li/r.li.daemon/avl.h

@@ -34,6 +34,7 @@ typedef AVL_tableRow *AVL_table;
 
 /* prototype of functions */
 avl_tree avl_make(const generic_cell k, const long n);
+void avl_destroy(avl_tree root);
 avl_node *avl_find(const avl_tree root, const generic_cell k);
 int avl_add(avl_tree * root, const generic_cell k, const long n);
 long avl_to_array(avl_node * root, long i, AVL_table * a);