-
Notifications
You must be signed in to change notification settings - Fork 2.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
* 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
1 parent
32a2237
commit db7a66f
Showing
3 changed files
with
353 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
*/ | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
*/ | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
*/ | ||
|