Wednesday, September 30, 2009

Circular Doubly Link List out of a Binary Search Tree

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;
}
}

1 comment:

  1. You are given a dictionary of all valid words. You have the following 3 operations permitted on a word: delete a character, insert a character, replace a character. Now given two words - word1 and word2 - find the minimum number of steps required to convert word1 to word2. (one operation counts as 1 step.)

    ReplyDelete