#include <stdio.h>
typedef struct node_s {
int data;
struct node_s *left, *right;
} NODE;
// Convert BST to a sorted list connected by ->right pointers.
NODE *to_list(NODE *tree, NODE *tail)
{
if (tree == NULL) return tail;
tree->right = to_list(tree->right, tail);
return to_list(tree->left, tree);
}
void print_tree(NODE *tree)
{
if (tree == NULL) return;
print_tree(tree->left);
printf(" %d", tree->data);
print_tree(tree->right);
}
void print_list(NODE *list)
{
while (list) {
printf(" %d", list->data);
list = list->right;
}
}
int main(void)
{
NODE n1[] = {{ 1, NULL, NULL }};
NODE n3[] = {{ 3, NULL, NULL }};
NODE n5[] = {{ 5, NULL, NULL }};
NODE n2[] = {{ 2, n1, n3 }};
NODE n4[] = {{ 4, n2, n5 }};
NODE *tree = n4;
NODE *list;
printf("as tree:");
print_tree(n4);
printf("\n");
list = to_list(n4, NULL);
printf("as list:");
print_list(list);
printf("\n");
return 0;
}
On Sep 21, 3:20 pm, prasanth n <[email protected]> wrote:
> anyone give an algorithm of how to convert a binary search tree into a
> linkedlist
>
> --
> *prasanth*
--
You received this message because you are subscribed to the Google Groups
"Algorithm Geeks" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to
[email protected].
For more options, visit this group at
http://groups.google.com/group/algogeeks?hl=en.