Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Create Tree_Diagonal_Traversal.cpp #3129

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
84 changes: 84 additions & 0 deletions Tree_Diagonal_Traversal/Tree_Diagonal_Traversal.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
/*
* Diagonal Traversal of Binary tree
*/

#include <iostream>
#include <vector>
#include <unordered_map>

using namespace std;

// Data Structure to store a Binary Tree node
struct Node {
int data;
Node *left, *right;

Node(int data) {
this->data = data;
this->left = this->right = NULL;
}
};

// Recursive function to perform preorder traversal on the tree and
// Fill the mao with diagonal elements
void printDiagonal(Node* node, int diagonal, unordered_map<int, vector<int> >& map)
{
// Base case: empty tree
if (node == NULL) {
return;
}

// insert the current node into the current diagonal
map[diagonal].push_back(node->data);

// recur for the left subtree by increasing diagonal by 1
printDiagonal(node->left, diagonal + 1, map);

// Recur for the right subtree with the same diagonal
printDiagonal(node->right, diagonal, map);
}


// Function to print the diagonal elemtns of a given binary tree
void printDiagonal(Node* root) {
// create an empty map to store the diagonal element by every slope
unordered_map<int, vector<int> > map;

// perform preorder traversal on the tree and fill the map
printDiagonal(root, 0, map);

// traverse the map and print diagonal elements
for (int i = 0; i < map.size(); i++) {
for (int j = 0; j < map[i].size(); j++) {
cout << map[i][j] << " ";
}
cout << endl;
}
cout << endl;
}
int main() {
/* construct the following tree
1
/ \
/ \
2 3
/ / \
/ / \
4 5 6
/ \
/ \
7 8
*/
Node* root = new Node(1);
root->left = new Node(2);
root->right = new Node(3);
root->left->left = new Node(4);
root->right->left = new Node(5);
root->right->right = new Node(6);
root->right->right->left = new Node(7);
root->right->right->right = new Node(8);

printDiagonal(root);
return 0;
}