The function convert converts the given binary search tree into a circular doubly link list and gives the pointer of the first node.
node convert(root)
{
if(root == null) return null;
else if(root.left == null && root.right == null)
{
root.left = root;
root.right = root;
return root;
}
else if(root.left == null && root.right != null)
{
rightnode = convert(root.right);
root.left = rightnode.left;
rightnode.left.right = root;
return root;
}
else if(root.right== null && root.left!= null)
{
leftnode = convert(root.left);
leftnode.left.right = root;
root.left = leftnode.left;
return leftnode;
}
else
{
leftnode = convert(root.left);
leftnode.left.right = root;
root.left = leftnode.left;
rightnode = convert(root.right);
rightnode.left.right = leftnode;
leftnode.left = rightnode.left;
rightnode.left = root;
root.right = rightnode;
}
}
Wednesday, September 30, 2009
Subscribe to:
Posts (Atom)