Double Tree
PROBLEM :
Write a program that converts a given tree to its Double tree. To create Double tree of the given tree, create a new duplicate for each node, and insert the duplicate as the left child of the original node.
So the tree…
2
/ \
1 3
is changed to…
2
/ \
2 3
/ /
1 3
/
1
And the tree
1
/ \
2 3
/ \
4 5
is changed to
1
/ \
1 3
/ /
2 3
/ \
2 5
/ /
4 5
/
4
--------------------------------------------------------------------------------
SIMPLE c++ IMPLEMENTATION :
--------------------------------------------------------------------------------
/*
typedef struct BST
{
int info ;
struct BST *left ;
struct BST *right ;
}tree ;------------------------------*/
void double_tree(tree *root)
{
if(root==NULL)
return ;
double_tree(root->left) ;
double_tree(root->right) ;
tree *old_left,*node ;
old_left=root->left ;
node=(tree*)malloc(sizeof(tree)) ;
node->info=root->info ;
node->left=NULL ;
node->right=NULL ;
root->left=node ;
node->left=old_left ;
}
---------------------------------------------------------------------------------
Write a program that converts a given tree to its Double tree. To create Double tree of the given tree, create a new duplicate for each node, and insert the duplicate as the left child of the original node.
So the tree…
2
/ \
1 3
is changed to…
2
/ \
2 3
/ /
1 3
/
1
And the tree
1
/ \
2 3
/ \
4 5
is changed to
1
/ \
1 3
/ /
2 3
/ \
2 5
/ /
4 5
/
4
--------------------------------------------------------------------------------
SIMPLE c++ IMPLEMENTATION :
--------------------------------------------------------------------------------
/*
typedef struct BST
{
int info ;
struct BST *left ;
struct BST *right ;
}tree ;------------------------------*/
void double_tree(tree *root)
{
if(root==NULL)
return ;
double_tree(root->left) ;
double_tree(root->right) ;
tree *old_left,*node ;
old_left=root->left ;
node=(tree*)malloc(sizeof(tree)) ;
node->info=root->info ;
node->left=NULL ;
node->right=NULL ;
root->left=node ;
node->left=old_left ;
}
---------------------------------------------------------------------------------
Comments
Post a Comment