From 0349792a5a6bafd9b869eb81ff1f4adf34a282e6 Mon Sep 17 00:00:00 2001 From: Solaris <91217295+RohanVolety@users.noreply.github.com> Date: Sat, 1 Oct 2022 21:08:46 +0530 Subject: [PATCH 1/2] Create Diameter_of_tree.cpp --- Trees/Diameter_of_tree.cpp | 60 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 60 insertions(+) create mode 100644 Trees/Diameter_of_tree.cpp diff --git a/Trees/Diameter_of_tree.cpp b/Trees/Diameter_of_tree.cpp new file mode 100644 index 00000000..b6be6048 --- /dev/null +++ b/Trees/Diameter_of_tree.cpp @@ -0,0 +1,60 @@ +#include +using namespace std; + +/* Tree node structure used in the program */ +struct Node +{ + int data; + Node *left, *right; +}; + +/* Function to find height of a tree */ +int height(Node *root, int &ans) +{ + if (root == NULL) + return 0; + + int left_height = height(root->left, ans); + + int right_height = height(root->right, ans); + + // update the answer, because diameter of a + // tree is nothing but maximum value of + // (left_height + right_height + 1) for each node + ans = max(ans, 1 + left_height + right_height); + + return 1 + max(left_height, right_height); +} + +/* Computes the diameter of binary tree with given root. */ +int diameter(Node *root) +{ + if (root == NULL) + return 0; + int ans = INT_MIN; // This will store the final answer + height(root, ans); + return ans; +} + +struct Node *newNode(int data) +{ + struct Node *node = new Node; + node->data = data; + node->left = node->right = NULL; + + return (node); +} + +// Driver code +int main() +{ + struct Node *root = newNode(1); + root->left = newNode(2); + root->right = newNode(3); + root->left->left = newNode(4); + root->left->right = newNode(5); + + printf("Diameter is %d\n", diameter(root)); + + return 0; +} From 2e63d9c884c191f3361605264c35b6cf93adea0a Mon Sep 17 00:00:00 2001 From: Solaris <91217295+RohanVolety@users.noreply.github.com> Date: Sat, 1 Oct 2022 21:09:39 +0530 Subject: [PATCH 2/2] Create TopView_of_tree.cpp --- Trees/TopView_of_tree.cpp | 84 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 84 insertions(+) create mode 100644 Trees/TopView_of_tree.cpp diff --git a/Trees/TopView_of_tree.cpp b/Trees/TopView_of_tree.cpp new file mode 100644 index 00000000..3454565c --- /dev/null +++ b/Trees/TopView_of_tree.cpp @@ -0,0 +1,84 @@ +// C++ program to print top +// view of binary tree + +#include +using namespace std; + +// Structure of binary tree +struct Node +{ + Node *left; + Node *right; + int hd; + int data; +}; + +// function to create a new node +Node *newNode(int key) +{ + Node *node = new Node(); + node->left = node->right = NULL; + node->data = key; + return node; +} + +// function should print the topView of +// the binary tree +void topview(Node *root) +{ + if (root == NULL) + return; + queue q; + map m; + int hd = 0; + root->hd = hd; + + // push node and horizontal distance to queue + q.push(root); + + cout << "The top view of the tree is : \n"; + + while (q.size()) + { + hd = root->hd; + + // count function returns 1 if the container + // contains an element whose key is equivalent + // to hd, or returns zero otherwise. + if (m.count(hd) == 0) + m[hd] = root->data; + if (root->left) + { + root->left->hd = hd - 1; + q.push(root->left); + } + if (root->right) + { + root->right->hd = hd + 1; + q.push(root->right); + } + q.pop(); + root = q.front(); + } + + for (auto i = m.begin(); i != m.end(); i++) + { + cout << i->second << " "; + } +} + + +int main() +{ + + Node *root = newNode(1); + root->left = newNode(2); + root->right = newNode(3); + root->left->right = newNode(4); + root->left->right->right = newNode(5); + root->left->right->right->right = newNode(6); + cout << "Following are nodes in top view of Binary " + "Tree\n"; + topview(root); + return 0; +}