This C Program print border of given tree in anticlockwise direction.
Here is source code of the C Program to print border of given tree in anticlockwise direction. The C program is successfully compiled and run on a Linux system. The program output is also shown below.
/*
* C Program to Print Border of given Tree in Anticlockwise Direction
* 50
* / \
* 20 30
* / \
* 70 80
* / \ \
* 10 40 60
*/
#include <stdio.h>
#include <stdlib.h>
struct btnode {
int value;
struct btnode *l;
struct btnode *r;
};
struct btnode *root;
typedef struct btnode bt;
bt *new, *ptr, *ptr1, *ptr2;
void print();
void print_leaf_nodes(bt*);
void print_right_recursive(bt*);
bt* create();
void construct_binary_tree();
void main()
{
construct_binary_tree();
printf("\nprinting the border elements anticlockwise direction:\n");
print();
printf("\n");
}
bt* create()
{
new = (bt *)malloc(sizeof(bt));
new->l = NULL;
new->r = NULL;
return new;
}
void construct_binary_tree()
{
root = create();
root->value = 50;
ptr = create();
root->l = ptr;
ptr->value = 20;
ptr1 = create();
ptr->l = ptr1;
ptr1->value = 70;
ptr2 = create();
ptr1->l = ptr2;
ptr2->value = 10;
ptr2 = create();
ptr1->r = ptr2;
ptr2->value = 40;
ptr1 = create();
ptr->r = ptr1;
ptr1->value = 80;
ptr2 = create();
ptr1->r = ptr2;
ptr2->value = 60;
ptr = create();
root->r = ptr;
ptr->value = 30;
}
void print()
{
ptr = root;
while (ptr->l != NULL)
{
printf("->%d", ptr->value);
ptr = ptr->l;
}
ptr = root;
print_leaf_nodes(ptr);
ptr = root;
print_right_recursive(ptr);
}
void print_leaf_nodes(bt* ptr)
{
if (ptr != NULL)
{
if (ptr->l == NULL && ptr->r == NULL)
{
printf("->%d", ptr->value);
}
else
{
print_leaf_nodes(ptr->l);
print_leaf_nodes(ptr->r);
}
}
else
return;
}
void print_right_recursive(bt* ptr)
{
int val;
ptr = ptr->r;
if (ptr->r != NULL)
{
print_right_recursive(ptr->r);
printf("->%d", ptr->value);
}
else
{
return;
}
}
50 / \ 20 30 / \ 70 80 / \ \ 10 40 60 $ cc tree30.c $ a.out printing the border elements anticlockwise direction: ->50->20->70->10->40->60->30
Sanfoundry Global Education & Learning Series – 1000 C Programs.
advertisement
advertisement
Here’s the list of Best Books in C Programming, Data-Structures and Algorithms
If you wish to look at programming examples on all topics, go to C Programming Examples.
Related Posts:
- Practice Computer Science MCQs
- Check Computer Science Books
- Practice Design & Analysis of Algorithms MCQ
- Apply for Computer Science Internship
- Check Data Structure Books