Documente Academic
Documente Profesional
Documente Cultură
#include
#include
#include
#include
<stdio.h>
<stdlib.h>
<conio.h>
<iostream.h>
<windows.h>
return(lDepth+1);
else return(rDepth+1);
}
}
///
int count(node *n)
{
int c1 = 1;
if (n == NULL)
return 0;
else
{
c1 += count(n->left);
c1 += count(n->right);
return c1;
}
}
///
/* Helper function that allocates a new node with the
given data and NULL left and right pointers. */
struct node* newNode(char data)
{
struct node* node = (struct node*)
malloc(sizeof(struct node));
node->data = data;
node->left = NULL;
node->right = NULL;
return(node);
}
/* Function to get the maximum width of a binary tree*/
int getMaxWidth(struct node* root)
{
int maxWidth = 0;
int width;
int h = height(root);
int i;
/* Get width of each level and compare
the width with maximum width so far */
for(i=1; i<=h; i++)
{
width = getWidth(root, i);
if(width > maxWidth)
maxWidth = width;
}
return maxWidth;
}
/* Get width of a given level */
int getWidth(struct node* root, int level)
{
if(root == NULL){
return 0;
}
if(level == 1){
return 1;
}
else if (level > 1){
return getWidth(root->left, level-1) +
getWidth(root->right, level-1);
}else{
return 0;
}
}
/* UTILITY FUNCTIONS */
/* Compute the "height" of a tree -- the number of
nodes along the longest path from the root node
down to the farthest leaf node.*/
int height(struct node* node)
{
if (node==NULL)
return 0;
else
{
/* compute the height of each subtree */
int lHeight = height(node->left);
int rHeight = height(node->right);
/* use the larger one */
return (lHeight > rHeight)? (lHeight+1): (rHeight+1);
}
}
///////////////////////////////////////////////////
/* Given a binary tree, print its nodes according to the
"bottom-up" postorder traversal. */
void printPostorder(struct node* node)
{
if (node == NULL)
return;
// first recur on left subtree
printPostorder(node->left);
// then recur on right subtree
printPostorder(node->right);
// now deal with the node
printf("%c ", node->data);
}
/* Given a binary tree, print its nodes in inorder*/
void printInorder(struct node* node)
{
if (node == NULL)
return;
/* first recur on left child */
printInorder(node->left);
/* then print the data of node */
printf("%c ", node->data);
getch();
_exit(0);
return 0;
}