Jun 4, 2009

Create a Circular Link List from a Given Binary Tree.

Create a Circular Link List from a Given Binary Tree.


struct node
{
struct node* left;
struct node* right;
int data;
}
typedef struct node* Node;

Node* B2L(node* root)
{
if(!root) return NULL;

Node* listA=B2L(root->left);
Node* listB=B2L(root->right);
return append(listA,root,listB);
}

Node* append(Node* listA, Node* root, Node* listB)
{
if(listA)
{
root->next=listA;
root->prev=listA->prev;
listA->prev=root;
root=listA;
}

if(listB)
{
Node* tail=listB->prev;
root->prev->next=listB;//tail
listB->prev->next=root;//tail
listB->prev=root->prev;//head
root->prev=tail;
}
return root;
}

No comments:

Post a Comment