Skip to content

Commit

Permalink
Binary Tree Bottom View, Top View and Right View in C jainaman224#1577 (
Browse files Browse the repository at this point in the history
jainaman224#2396)

* Binary Tree Bottom View in C

* Update BinaryTree_Bottom_View.c

* Update BinaryTree_Bottom_View.c

* Create Binary_Tree_Right_View.c

* Create Binary_Tree_Top_View.c

* Rename BinaryTree_Bottom_View.c to Binary_Tree_Bottom_View.c

* Update Binary_Tree_Bottom_View.c

* Update Binary_Tree_Right_View.c

* Update Binary_Tree_Top_View.c
  • Loading branch information
AkanshaKamboj authored and Mrunal committed Apr 8, 2020
1 parent 3f72426 commit abae014
Show file tree
Hide file tree
Showing 3 changed files with 353 additions and 0 deletions.
124 changes: 124 additions & 0 deletions Binary_Tree_Bottom_View/Binary_Tree_Bottom_View.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
/* C program to print bottom view of the binary tree*/

#include<stdio.h>
#include<stdlib.h>
#include<limits.h>

// A binary tree node has data, pointer to left child and a pointer to right child
struct Node
{
int data;
struct Node* left;
struct Node* right;
};

/* Inserting elements into Binary Tree*/
struct Node *create()
{
Node *ptr;
int x;
printf("Enter data(-1 for no node):");
scanf("%d", &x);

if(x == -1)
return NULL;

ptr = (struct Node*)malloc(sizeof(struct Node));
ptr -> data = x;

printf("Enter left child of %d:\n", x);
ptr -> left = create();

printf("Enter right child of %d:\n", x);
ptr -> right = create();

return ptr;
}

// Calculating the level and the vertical distance of the node and finding the nodes that appear as the top view
void findBottomView(struct Node *root, int *a, int *b, int level, int dist)
{
if(root == NULL)
return;

if(level > a[50 + dist])
{
a[50 + dist] = level;
b[50 + dist] = root -> data;
}

# Recursively calling the right and the left child to get the bottom view

findBottomView(root -> right, a, b, level + 1, dist + 1);
findBottomView(root -> left, a, b, level + 1, dist - 1);
}

int *bottomView(struct Node* root, int *len) {

int i, o = 0;

// a stores level of the node
// b stores vertical distance of the node from root

int *a = (int *)calloc(100, sizeof(int));
int *b = (int *)calloc(100, sizeof(int));
int *c = (int *)calloc(100, sizeof(int));

// Initialising a and b as INT_MIN as we need to print bottom most elements
for(i = 0; i < 100; i++)
{
a[i] = INT_MIN;
b[i] = INT_MIN;
}

findBottomView(root, a, b, 0, 0);

for(i = 0; i < 100; i++)
{
if(b[i] != INT_MIN)
c[o++] = b[i];
}

*len = o;
return c;
}

int main()
{
struct Node *root = create();
int len = 0;
int *res = bottomView(root, &len);

for(int i = 0; i < len; i++)
printf("%d ", res[i]);
}


/* Sample Input
Enter data(-1 for no node: 1
Enter left child of 1:
Enter data(-1 for no node: 2
Enter left child of 2:
Enter data(-1 for no node: 6
Enter left child of 6:
Enter data(-1 for no node: -1
Enter right child of 6:
Enter data(-1 for no node: -1
Enter right child of 2:
Enter data(-1 for no node: -1
Enter right child of 1:
Enter data(-1 for no node: 3
Enter left child of 3:
Enter data(-1 for no node: -1
Enter right child of 3:
Enter data(-1 for no node: -1
1
/ \
2 3
/
6
Output :- 6 2 1 3
*/

105 changes: 105 additions & 0 deletions Binary_Tree_Right_View/Binary_Tree_Right_View.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
/* C program to print right view of the binary tree*/

#include<stdio.h>
#include<stdlib.h>

/* A binary tree node has data, pointer to left child and a pointer to right child */
struct Node
{
int data;
struct Node* left;
struct Node* right;
};

/* Inserting elements into Binary Tree*/
struct Node *create()
{
Node *ptr;
int x;

printf("Enter data(-1 for no node):");
scanf("%d", &x);

if(x == -1)
return NULL;

ptr = (struct Node*)malloc(sizeof(struct Node));
ptr -> data = x;

printf("Enter left child of %d:\n",x);
ptr -> left = create();

printf("Enter right child of %d:\n",x);
ptr -> right = create();

return ptr;
}

/*Recursive solution for priniting right view of Binary Tree*/
void findRightView(struct Node *root, int *ans, int *size, int index)
{
if(root == NULL)
return;

index++;

// Increasing the level of the tree every time a node with the same level appears
if(index > *size)
ans[(*size)++] = root -> data;

// Recursively calling the nodes down the tree
findRightView(root -> right, ans, size, index);

findRightView(root -> left, and, size, index);
}

// Function for intializing the level of the root node and size of the array
int *rightView(struct Node *root, int *size)
{
int *ans = malloc(sizeof(int) * 100);
*size = 0;

findRightView(root, ans, size, 0);

return ans;
}

int main()
{
struct Node *root = create();

int size = 0;
int *res = rightView(root, &size);

for(int i = 0; i < size; i++)
printf("%d", res[i]);
}

/* Sample Input
Enter data(-1 for no node: 1
Enter left child of 1:
Enter data(-1 for no node: 2
Enter left child of 2:
Enter data(-1 for no node: 6
Enter left child of 6:
Enter data(-1 for no node: -1
Enter right child of 6:
Enter data(-1 for no node: -1
Enter right child of 2:
Enter data(-1 for no node: -1
Enter right child of 1:
Enter data(-1 for no node: 3
Enter left child of 3:
Enter data(-1 for no node: -1
Enter right child of 3:
Enter data(-1 for no node: -1
1
/ \
2 3
/
6
Output :- 1 3 6
*/

124 changes: 124 additions & 0 deletions Binary_Tree_Top_View/Binary_Tree_Top_View.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@

/* C program to print top view of Binary tree*/

#include<stdio.h>
#include<stdlib.h>
#include<limits.h>

/* A binary tree node has data, pointer to left child and a pointer to right child */
struct Node
{
int data;
struct Node* left;
struct Node* right;
};

// Inserting elements into Binary Tree
struct Node *create()
{
Node *ptr;
int x;

printf("Enter data(-1 for no node):");
scanf("%d",&x);

if(x == -1)
return NULL;

ptr = (struct Node*)malloc(sizeof(struct Node));
ptr -> data = x;

printf("Enter left child of %d:\n",x);
ptr -> left = create();

printf("Enter right child of %d:\n",x);
ptr -> right = create();

return ptr;
}

// Calculating the level and the vertical distance of the node and finding the nodes that appear as the top view
void findTopView(struct Node *root, int *a, int *b, int level, int dist)
{
if(root == NULL)
return;

if(level < a[50 + dist])
{
a[50 + dist] = level;
b[50 + dist] = root->data;
}

# Recursively calling the left and the right child to get the bottom view
findTopView(root->left, a, b, level+1, dist-1);

findTopView(root->right, a, b, level+1, dist+1);
}

int *topView(struct Node* root, int *len) {

int i,o = 0;

// a stores the level of the node
// b stores the Vertical distance of the node from root
int *a = (int *)calloc(100, sizeof(int));
int *b = (int *)calloc(100, sizeof(int));
int *c = (int *)calloc(100, sizeof(int));

// Initialising a and b as INT_MAX as we need to print top most elements
for(i = 0; i < 100; i++)
{
a[i] = INT_MAX;
b[i] = INT_MAX;
}

findTopView(root, a, b, 0, 0);

for(i = 0; i < 100; i++)
{
if(b[i] != INT_MAX)
c[o++] = b[i];
}

*len = o;
return c;
}

int main()
{
struct Node *root = create();
int len = 0;
int *res = topView(root, &len);

for(int i = 0; i < len; i++)
printf("%d ", res[i]);
}

/* Sample Input
Enter data(-1 for no node: 1
Enter left child of 1:
Enter data(-1 for no node: 2
Enter left child of 2:
Enter data(-1 for no node: 6
Enter left child of 6:
Enter data(-1 for no node: -1
Enter right child of 6:
Enter data(-1 for no node: -1
Enter right child of 2:
Enter data(-1 for no node: -1
Enter right child of 1:
Enter data(-1 for no node: 3
Enter left child of 3:
Enter data(-1 for no node: -1
Enter right child of 3:
Enter data(-1 for no node: -1
1
/ \
2 3
/
6
Output :- 6 2 1 3
*/

0 comments on commit abae014

Please sign in to comment.