/* Approach * generate list for left tree call it as l1 * generate list for right tree call it as l2 * sandwitch node between l1 and l2 = l1 + node + l2 * keep doing it recursively. */ void bst2dll(Node *node, Node **head, Node **tail) { Node *h1,*t1,*h2,*t2; // If this is the NULL node , then do not return any list if (node == NULL) { if (head) *head = NULL; if (tail) *tail = NULL; return; } // recursively generate list for left and right tree bst2dll(node->left, &h1, &t1); bst2dll(node->right, &h2, &t2); // add the node between two lists. node->left = t1; if(t1) t1->right = node; node->right = h2; if(h2) h2->left = node; // If right and left tree are empty then // return tree with one element only // else return head or left list as new head // and tail of right list as new tail. if (head) *head = h1? h1 : node; if (tail) *tail = t2? t2 : node; }
struct BSTNode* bst2dll(struct BSTNode* root, struct BSTNode* prev){ if(root==NULL) return prev; prev = bst2dll(root->left,prev); root->left = prev; if(prev) prev->right = root; prev = root; prev = bst2dll(root->right,prev); return prev; }
/* binary search tree to double linked list */ void bst2dll(BSTreeNode * root, BSTreeNode ** prev) { if (root == NULL) return; bst2dll(root->left, prev); if (*prev) (*prev)->right = root; root->left = *prev; *prev = root; bst2dll(root->right, prev); }
int main(int argc, char **argv) { Node *root = NULL; Node *dl, *tl; int v; for(int i = 0; i < 10; i++) { v = rand() % 100; printf("%d ", v); insert_bst(&root, v); } printf("\n"); print_bst(root); printf("\n"); bst2dll(root, &dl, &tl); printf("%p %p\n", dl, tl); print_list(dl); return 0; };
int main(){ printf("Creating a binary search tree\n"); struct BSTNode* root =NULL; root = createBST(root); struct BSTNode* prev = NULL; struct BSTNode* temp=bst2dll(root,prev); struct BSTNode* temp1; while(temp!= NULL){ printf("%d\n",temp->value); temp1 = temp; temp = temp->left; } while(temp1!=NULL){ printf("%d\n",temp1->value); temp1 = temp1->right; } return 0; }
int main(int argc, char** argv) { random_bst(); struct node* bst_root = generate_bst(20); printf("Pre-order\n"); pre_order_bst(bst_root); printf("\n"); printf("level-order\n"); level_order_bst(bst_root); printf("\n"); printf("Is a bst? %d\n", is_bst(bst_root)); in_order_bst(bst_root); printf("\n"); struct dll list; bst2dll(bst_root, &list); print_dll(list); destruct_dll(list); return 0; }
int main() { BSTreeNode * nodes = (BSTreeNode*)malloc(sizeof(BSTreeNode)*7); nodes[0].value = 10; nodes[0].left = &nodes[1]; nodes[0].right = &nodes[2]; nodes[1].value = 6; nodes[1].left = &nodes[3]; nodes[1].right = &nodes[4]; nodes[3].value = 4; nodes[3].left = NULL; nodes[3].right = NULL; nodes[4].value = 8; nodes[4].left = NULL; nodes[4].right = NULL; nodes[2].value = 14; nodes[2].left = &nodes[5]; nodes[2].right = &nodes[6]; nodes[5].value = 12; nodes[5].left = NULL; nodes[5].right = NULL; nodes[6].value = 16; nodes[6].left = NULL; nodes[6].right = NULL; BSTreeNode * prev = NULL; bst2dll(&nodes[0], &prev); while (prev) { printf("%d ", prev->value); prev = prev->left; } free(nodes); return 0; }