Skip to content

Added Tree Questions #46

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

Merged
merged 2 commits into from
Oct 1, 2022
Merged
Show file tree
Hide file tree
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
60 changes: 60 additions & 0 deletions Trees/Diameter_of_tree.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
#include <bits/stdc++.h>
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;
}
84 changes: 84 additions & 0 deletions Trees/TopView_of_tree.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
// C++ program to print top
// view of binary tree

#include <bits/stdc++.h>
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<Node *> q;
map<int, int> 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;
}