@Surender: sorry, i had missed a case. We need a 3 way comparison.
heres the correct version
btree* max_tree(btree *root)
{
if(root == NULL)
return root;
btree * current = root;
while(current->right != NULL)
{
current = current->right;
}
return current;
}
btree * min_tree(btree *root)
{
if(root == NULL)
return root;
btree * current = root;
while(current->left != NULL)
{
current = current->left;
}
return current;
}
void binarytreetobst(btree *root)
{
//base-case tree is empty
if(root == NULL)
return;
else if(root->left == NULL && root->right == NULL) //base-case
tree
of size 1
return;
else
{
binarytreetobst(root->left);
binarytreetobst(root->right);
btree* max = max_tree(root->left);
if(max && max->data > root->data)
{
int temp = root->data;
root->data = max->data;
max->data = temp;
binarytreetobst(root->left);
}
btree* min = min_tree(root->right);
if(min && min->data < root->data)
{
int temp = root->data;
root->data = min->data;
min->data = temp;
binarytreetobst(root->right);
max = max_tree(root->left);
if(max && max->data > root->data)
{
int temp = root->data;
root->data = max->data;
max->data = temp;
binarytreetobst(root->left);
}
}
}
}
On Jul 19, 8:38 am, surender sanke <[email protected]> wrote:
> @Damanshu for
> 1
> / \
> 2 3
> / \
> 4 5
> / \
> 6 7
>
> im ending up at some non BST
>
> surender
>
>
>
>
>
>
>
> On Tue, Jul 19, 2011 at 4:06 AM, Dumanshu <[email protected]> wrote:
> > @Gaurav: The best solution would be to manipulate the given BTree in
> > place and get the BST. We don't need a separate tree but convert the
> > existing one using the same space occupied by nodes of BT already in
> > BST.
>
> > On Jul 19, 2:06 am, Gaurav Popli <[email protected]> wrote:
> > > cant we just do....traverse using recursion and instead of printing it
> > > just pass to function which is making BST....??
> > > and is this right as someone above said first sort it and then make
> > BST...
> > > dont we want that root of both Tree to be same or something like
> > that...??
>
> > > On Tue, Jul 19, 2011 at 2:17 AM, Dumanshu <[email protected]> wrote:
> > > > @Balaji: for third question, were u asked to write the code?
>
> > > > On Jul 18, 10:04 pm, Balaji S <[email protected]> wrote:
> > > >> wats the mistake..
>
> > > > --
> > > > 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 athttp://
> > groups.google.com/group/algogeeks?hl=en.- Hide quoted text -
>
> > > - Show quoted text -
>
> > --
> > 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.
--
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.