Sunteți pe pagina 1din 5

#include

#include
#include
#include
#include

<stdio.h>
<stdlib.h>
<conio.h>
<iostream.h>
<windows.h>


/* A binary tree node has data, pointer to left child


and a pointer to right child */
struct node
{
char data;
struct node* left;
struct node* right;
};
/*Function protoypes*/
void printGivenLevel(struct node* root, int level);
int height(struct node* node);
struct node* newNode(char data);
int getMaxWidth(struct node* root);
int getWidth(struct node* root, int level);
void printLevelOrder(struct node* root)
{
int h = height(root);
int i;
for(i=1; i<=h; i++)
printGivenLevel(root, i);
}
/* Print nodes at a given level */
void printGivenLevel(struct node* root, int level)
{
if(root == NULL)
return;
if(level == 1)
printf(" %c ", root->data);
else if (level > 1)
{
printGivenLevel(root->left, level-1);
printGivenLevel(root->right, level-1);
}
}
//////////////////////////////////
/* Compute the "maxDepth" of a tree -- the number of
nodes along the longest path from the root node
down to the farthest leaf node.*/
int maxDepth(struct node* node)
{
if (node==NULL)
return 0;
else
{
/* compute the depth of each subtree */
int lDepth = maxDepth(node->left);
int rDepth = maxDepth(node->right);
/* use the larger one */
if (lDepth > rDepth)

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

/* now recur on right child */


printInorder(node->right);
}
/* Given a binary tree, print its nodes in inorder*/
void printPreorder(struct node* node)
{
if (node == NULL)
return;
/* first print data of node */
printf("%c ", node->data);
/* then recur on left sutree */
printPreorder(node->left);
/* now recur on right subtree */
printPreorder(node->right);
}
/*void gotoxy(short x, short y) {
COORD pos = {x, y};
SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), pos);
} */
/* Driver program to test above functions*/
int main()
{
char n1,n2,n3,n4,n5,n6,n7,n8,n9,n10,n11;
int jml;
struct node *root
= newNode('A');
root->left
= newNode('B');
root->right
= newNode('C');
root->left->left
= newNode('D');
root->left->right
= newNode('E');
root->left->right->left
= newNode('H');
root->left->right->right = newNode('I');
root->right->left
= newNode('F');
root->right->right
= newNode('G');
root->right->left->left
= newNode('J');
root->right->left->right = newNode('K');
jml=count(root);
clrscr();
cout<<"1. Input : "<<endl;
cout<<" ";printLevelOrder(root);
cout<<endl;
cout<<"2. Jumlah node : "<<jml<<endl;
cout<<" ketinggian (height) : " << height(root)<<endl;
cout<<" Level (degree) : "<<getMaxWidth(root)<<endl;
cout<<"3. Preorder : "<<endl;
cout<<" ";printPreorder(root);
cout<<endl;
cout<<"4. Postorder : "<<endl;
cout<<" ";printPostorder(root);
cout<<endl;
cout<<"5. Inorder : "<<endl;
cout<<" ";printInorder(root);
cout<<endl<<endl;
cout<<"Tekan sembarang tombol untuk close";

getch();
_exit(0);
return 0;
}

S-ar putea să vă placă și