Binary tree. C++ Programming code


class node

{

public:
char data;
class node* left;
class node* right;
};

node* build1plus2a()

{
node* root = newNode(+);
node* lChild = newNode(1);
node* rChild = newNode(2);

root->left = lChild;
root->right= rChild;

return root;
}

node* build1plus2b()

{
node* root = newNode(+);
root->left = newNode(1);
root->right = newNode(2);

return root;

}

int size2( node* node)

{
if (node==NULL)

{
return 0;
}

else

{
return(size(node->left) + 1 + size(node->right));
}
}

int maxDepth( node* node)

{
if (node==NULL)

{
return 0;
}
else

{
int lDepth = maxDepth(node->left);
int rDepth = maxDepth(node->right);

if (lDepth > rDepth) return(lDepth+1);
else return(rDepth+1);
}
}

void printExpressionTreePostfix ( node* node)

{
if (node == NULL) return;

printExpressionTreePostfix (node->left);
printExpressionTreePostfix (node->right);

cout<<node->data;
}

void printExpressionTreeInfix(node* node)

{

if (node == NULL) return;

printExpressionTreeInfix(node->left);

cout<<node->data;

printExpressionTreeInfix(node->right);

}

void printExpressionTreePrefix(node* node)

{

if (node == NULL) return;

cout<<node->data;

printExpressionTreePrefix(node->left);

printExpressionTreePrefix(node->right);

}

void mirror( node* node)

{
if (node==NULL)

{
return;
}
else

{
node* temp;

mirror(node->left);
mirror(node->right);

temp = node->left;
node->left = node->right;
node->right = temp;
}
}

void doubleTree( node* node)
… you write that !

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: