Tree algo Flashcards
applications of Trees:
To represent hierarchical data.
Binary Search Trees.
Binary heap
B and B+ Trees in DBMS
Spanning and Shortest path in computer networks
Parse Tree and Expression Tree in Compilers.
implementation to represent a Binary Tree.
In Binary Tree every node has at most two children.
#include (bits/stdc++.h>
using namespace std;
struct Node { int key; struct Node *left; struct Node *right; Node(int k){ key=k; left=right=NULL; } };
int main() {
Node *root=new Node(10); root->left=new Node(20); root->right=new Node(30); root->left->left=new Node(40); }
Implementation of Inorder Traversal
#include (bits/stdc++.h> using namespace std;
struct Node { int key; struct Node *left; struct Node *right; Node(int k){ key=k; left=right=NULL; } };
void inorder(Node *root){ if(root!=NULL){ inorder(root->left); cout((root->key((" "; inorder(root->right); } }
int main() {
Node *root=new Node(10); root->left=new Node(20); root->right=new Node(30); root->right->left=new Node(40); root->right->right=new Node(50);
inorder(root); } 20 10 40 30 50
Implementation of Preorder Traversal
#include (bits/stdc++.h> using namespace std;
struct Node { int key; struct Node *left; struct Node *right; Node(int k){ key=k; left=right=NULL; } };
void preorder(Node *root){ if(root!=NULL){ cout((root->key((" "; preorder(root->left); preorder(root->right); } }
int main() {
Node *root=new Node(10); root->left=new Node(20); root->right=new Node(30); root->right->left=new Node(40); root->right->right=new Node(50);
preorder(root); } 10 20 30 40 50
Implementation of Postorder Traversal
#include (bits/stdc++.h> using namespace std;
struct Node { int key; struct Node *left; struct Node *right; Node(int k){ key=k; left=right=NULL; } };
void postorder(Node *root){ if(root!=NULL){ postorder(root->left); postorder(root->right); cout((root->key((" "; } }
int main() {
Node *root=new Node(10); root->left=new Node(20); root->right=new Node(30); root->right->left=new Node(40); root->right->right=new Node(50);
postorder(root); } 20 40 50 30 10
Implementation of function to calculate Height of Binary Tree
Height of Binary Tree is the number of nodes between the longest path from root to leaf node(including the root and leaf node).
#include (bits/stdc++.h> using namespace std;
struct Node { int key; struct Node *left; struct Node *right; Node(int k){ key=k; left=right=NULL; } };
int height(Node *root){ if(root==NULL) return 0; else return (1+max(height(root->left),height(root->right))); }
int main() {
Node *root=new Node(10); root->left=new Node(20); root->right=new Node(30); root->right->left=new Node(40); root->right->right=new Node(50);
cout((height(root); } output 3
Print nodes at k distance in a tree
Nodes at distance k from the root are basically the nodes at (k+1)th level of the Binary Tree.
#include (bits/stdc++.h> using namespace std;
struct Node { int key; struct Node *left; struct Node *right; Node(int k){ key=k; left=right=NULL; } };
void printKDist(Node *root,int k){ if(root==NULL)return;
if(k==0){cout((root->key((" ";} else{ printKDist(root->left,k-1); printKDist(root->right,k-1); } }
int main() {
Node *root=new Node(10); root->left=new Node(20); root->right=new Node(30); root->left->left=new Node(40); root->left->right=new Node(50); root->right->right=new Node(70); root->right->right->right=new Node(80); int k=2;
printKDist(root,k); } 40 50 70
Implement level order traversal for a binary tree
Level order traversal of a tree is breadth first traversal of binary tree
#include (bits/stdc++.h> using namespace std;
struct Node { int key; struct Node *left; struct Node *right; Node(int k){ key=k; left=right=NULL; } };
void printLevel(Node *root){ if(root==NULL)return; queue(Node *>q; q.push(root); while(q.empty()==false){ Node *curr=q.front(); q.pop(); cout((curr->key((" "; if(curr->left!=NULL) q.push(curr->left); if(curr->right!=NULL) q.push(curr->right); } }
int main() {
Node *root=new Node(10); root->left=new Node(20); root->right=new Node(30); root->left->left=new Node(40); root->left->right=new Node(50); root->right->left=new Node(60); root->right->right=new Node(70);
printLevel(root); } 10 20 30 40 50 60 70
Calculate size of binary tree
Size of Binary Tree is the total numbers of nodes present in that Tree.
#include (bits/stdc++.h> using namespace std;
struct Node { int key; struct Node *left; struct Node *right; Node(int k){ key=k; left=right=NULL; } };
int getSize(Node *root){ if(root==NULL) return 0; else return 1+getSize(root->left)+getSize(root->right); }
int main() {
Node *root=new Node(10); root->left=new Node(20); root->right=new Node(30); root->left->left=new Node(40); root->left->right=new Node(50); root->right->right=new Node(60);
cout((getSize(root); } 6
Maximum node in a tree
#include (bits/stdc++.h> using namespace std;
struct Node { int key; struct Node *left; struct Node *right; Node(int k){ key=k; left=right=NULL; } };
int getMax(Node *root){ if(root==NULL) return INT_MIN; else return max(root->key,max(getMax(root->left),getMax(root->right))); }
int main() {
Node *root=new Node(20); root->left=new Node(80); root->right=new Node(30); root->right->left=new Node(40); root->right->right=new Node(50);
cout((getMax(root); } 80
Iterative Inorder traversal
TC = theta(n)
SC - O(h)
Iterative Preorder Traversal (Simple)
A O(n) extra space and O(n) time solution
Iterative Preorder Traversal (Space Optimized)
O(h) extra space and O(n) time solution
Children Sum Parent
Given a Binary Tree. Check whether all of its nodes have the value equal to the sum of their child nodes.
Example 1:
Input: 10 / 10 Output: 1 Explanation: Here, every node is sum of its left and right child. Example 2:
Input: 1 / \ 4 3 / \ 5 N Output: 0 Explanation: Here, 1 is the root node and 4, 3 are its child nodes. 4 + 3 = 7 which is not equal to the value of root node. Hence, this tree does not satisfy the given conditions.
Your Task:
You don’t need to read input or print anything. Your task is to complete the function isSumProperty() that takes the root Node of the Binary Tree as input and returns 1 if all the nodes in the tree satisfy the following properties. Else, it returns 0.
For every node, data value must be equal to the sum of data values in left and right children. Consider data value as 0 for NULL child. Also, leaves are considered to follow the property.
Expected Time Complexiy: O(N).
Expected Auxiliary Space: O(Height of the Tree).
Algorithm:
For each node, if node is null or both child nodes are null, return true.
Else if left and right child are not null then store their value.
If sum of stored data of left and right child is equal to the current node data and recursively for the left and right subtree, parent data is equal to sum of child data then return true else return false.
int isSumProperty(struct Node *node) { int left_data = 0, right_data = 0;
//if node is null or both child nodes are null, we return true. if (node == NULL ||(node->left == NULL && node->right == NULL)) return 1;
else {
//if left child is not null then we store its value. if (node->left != NULL) left_data = node->left->data;
//if right child is not null then we store its value. if (node->right != NULL) right_data = node->right->data;
//if sum of stored data of left and right child is equal to the current //node data and recursively for the left and right subtree, parent data //is equal to sum of child data then we return true. if ((node->data == left_data + right_data) && isSumProperty(node->left) && isSumProperty(node->right)) return 1;
//else we return false. else return 0; }
Determine if Two Trees are Identical
Given two binary trees, the task is to find if both of them are identical or not.
Example 2:
Input: 1 1 / \ / \ 2 3 2 3 Output: Yes Explanation: There are two trees both having 3 nodes and 2 edges, both trees are identical having the root as 1, left child of 1 is 2 and right child of 1 is 3. Example 2:
Input: 1 1 / \ / \ 2 3 3 2 Output: No Explanation: There are two trees both having 3 nodes and 2 edges, but both trees are not identical.
Your task:
Since this is a functional problem you don’t have to worry about input, you just have to complete the function isIdentical() that takes two roots as parameters and returns true or false. The printing is done by the driver code.
Expected Time Complexity: O(N).
Expected Auxiliary Space: O(Height of the Tree).
Two trees are identical when they have same data and arrangement of data is also same. To identify if two trees are identical, we need to traverse both trees simultaneously, and while traversing we need to compare data and children of the trees.
Algorithm:
1. If both nodes are empty then return 1.
2. Else If both nodes are non-empty
(a) Check data of the root nodes (tree1->data == tree2->data)
(b) Check left subtrees recursively.
(c) Check right subtrees recursively.
(d) If a,b and c are true then return 1.
3 Else return 0 (one is empty and other is not).
class Solution { public: //Function to check if two trees are identical. bool isIdentical(Node* a, Node* b) { //if both nodes are null then we return true. if (a == NULL && b == NULL) return true;
if (a != NULL && b != NULL) { //we check if data at both nodes and left and right subtree of //both the nodes are equal then we return true else return false. return ( a->data == b->data && isIdentical(a->left, b->left) && isIdentical(a->right, b->right) ); }
//returning false if one of the nodes is null and other isn't. return false; }
};