bst* tree_to_list(bst *b) { if(!b) return NULL; if(!b->left && !b->right){ b->left = b->right = b; return b; } bst *left = tree_to_list(b->left); bst *right = tree_to_list(b->right); if(left){ left->prev->next = b; b->prev = left; left->prev = b; b = left; } if(right){ bst *last_a = b->prev; bst *last_b = right->prev; last_a->next = right; right->prev = last_a; last_b->next = b; b->prev = last_b; } return b; }
/* tree_to_list: in-order print of tree p */ void tree_to_list(struct tnode *p) { if (p != NULL) { tree_to_list(p->left); //printf("%4d %s\n", p->count, p->word); add_list(p); tree_to_list(p->right); } }
/* * Write a binary tree node to disk. */ int pdb_write_tree_node_cb(struct pdb* dbptr, FILE* fptr, struct pdb_node_t* nptr, int tabs) { struct binaryTree* tptr; struct linkList* lptr; struct linkNode* lnptr; /* * Create a list from the tree so we can iterate through it. */ tptr = nptr->data; lptr = list_create(); tree_to_list(tptr, lptr); lnptr = lptr->root; /* * Shuffle tree (if set). */ if (pdb_is_set(dbptr, PDB_WRITE_SHUFFLE)) list_shuffle(lptr); /* * Set PDB_WRITE_NODE_FIRST_ID as the first in the list * if PDB_WRITE_NODE_FIRST is set. * Only if root node. */ if (!tabs) { if (pdb_is_set(dbptr, PDB_WRITE_NODE_FIRST)) { while (lnptr) { nptr = lnptr->data; if (!strcmp(nptr->id, PDB_WRITE_NODE_FIRST_ID)) { list_free_node(lptr, lnptr, 0, NULL); list_add_node_front(lptr, nptr); break; } lnptr = lnptr->next; } } } /* * Write all children to disk. */ lnptr = lptr->root; while (lnptr) { nptr = lnptr->data; if (!pdb_standard_write_node(dbptr, fptr, nptr, tabs)) { list_free(lptr, 0, NULL); return 0; } lnptr = lnptr->next; } /* * Free temp list (but not data). */ list_free(lptr, 0, NULL); return 1; }
int main() { //struct node* plist; //plist = build_cll123(); //print_cir_link_list(plist); struct node* ptree = build_bstree(); print_tree(ptree, in_order); struct node* plist = tree_to_list(ptree); print_cir_link_list(plist); return 0; }
// This method shuould be locked // It makes an array pointing to all the values in the map. // You can modify the values of this map. list map_values(map m) { pthread_mutex_lock(m->mutex); int i = 0; list datas = tree_to_list(m->t); list l = list_init(); for (i=0; i < tree_size(m->t); i++) { list_add(l, ((data*)list_get(datas, i))->value); } list_free(datas); pthread_mutex_unlock(m->mutex); return l; }
void test() { node_t* root = build_bst(30); node_t* head = tree_to_list(root); while (head != NULL) { printf("%d ", head->data); head = head->right; } printf("\n"); }
int tree_compare(tree t1, tree t2) { if (t1->comp != t2->comp) return 1; if (t1->size != t2->size) return 1; list values1 = tree_to_list(t1); list values2 = tree_to_list(t2); int len = t1->size; int i = 0; for (i = 0; i < len; ++i) { void * p1 = list_get(values1, i); void * p2 = list_get(values2, i); if (t1->comp(p1,p2) != 0) { return 1; } } list_free(values1); list_free(values2); return 0; }
/* word frequency count */ int main(int argc, char *argv[]) { struct tnode *root; char word[MAXWORD]; root = NULL; while (getword(word, MAXWORD) != EOF) { if (isalpha(word[0])) root = addtree(root, word); } /* add list */ tree_to_list(root); /* list sort */ qsort(tnode_list, list_index, sizeof(tnode_list[0]), node_cmp); /* print */ print_list(); return 0; }