
- C++ Basics
- C++ Home
- C++ Overview
- C++ Environment Setup
- C++ Basic Syntax
- C++ Comments
- C++ Data Types
- C++ Variable Types
- C++ Variable Scope
- C++ Constants/Literals
- C++ Modifier Types
- C++ Storage Classes
- C++ Operators
- C++ Loop Types
- C++ Decision Making
- C++ Functions
- C++ Numbers
- C++ Arrays
- C++ Strings
- C++ Pointers
- C++ References
- C++ Date & Time
- C++ Basic Input/Output
- C++ Data Structures
- C++ Object Oriented
- C++ Classes & Objects
- C++ Inheritance
- C++ Overloading
- C++ Polymorphism
- C++ Abstraction
- C++ Encapsulation
- C++ Interfaces
C++ Program to Check Whether an Input Binary Tree is the Sub Tree of the Binary Tree
A binary tree is a tree data structure in which each node has at most two children, which are defined as left child and right child.
Algorithm
Begin function identical(): Take two nodes r1 and r2 as parameter. If r1 and r2 is NULL then Return true. If r1 or r2 is NULL then Return false. Return (r1->d is equal to r2->d and Call function Identical(r1->l, r2->l) and Call functions Identical(r1->r, r2->r) ); function Subtree(node *T, node *S) : if (S == NULL) then return true; if (T == NULL) then return false; if (call function Identical(T, S)) return true; return Subtree(T->l, S) or Subtree(T->r, S); End.
Example Code
#include <iostream> using namespace std; struct n { int d; struct n* l; struct n* r; }; bool Identical(struct n * r1, struct n *r2) { if (r1 == NULL && r2 == NULL) return true; if (r1 == NULL || r2 == NULL) return false; return (r1->d == r2->d && Identical(r1->l, r2->l) && Identical(r1->r, r2->r) ); } bool Subtree(struct n *T, struct n *S) { if (S == NULL) return true; if (T == NULL) return false; if (Identical(T, S)) return true; return Subtree(T->l, S) || Subtree(T->r, S); } struct n* newN(int d) { struct n* nod = (struct n*)malloc(sizeof(struct n)); nod->d = d; nod->l = NULL; nod->r = NULL; return(nod); } int main() { struct n *T = newN(24); T->r = newN(2); T->r->r = newN(5); T->l = newN(7); T->l->l = newN(3); T->l->l->r = newN(40); T->l->r = newN(6); struct n *S = newN(20); S->r = newN(5); S->l = newN(3); S->l->r = newN(50); if (Subtree(T, S)) cout<<"given tree is subtree of Binary tree"<<"\n"; else cout<<"given tree is not subtree of Binary tree"<<"\n"; struct n *T1 = newN(30); T1->r = newN(20); T1->r->r = newN(19); T1->l = newN(17); T1->l->l = newN(4); T1->l->l->r = newN(40); T1->l->r = newN(15); struct n *S1 = newN(17); S1->r = newN(15); S1->l = newN(4); S1->l->r = newN(40); if (Subtree(T1, S1)) cout<<"given tree is subtree of Binary tree"; else cout<<"given tree is not subtree of Binary tree"; getchar(); return 0; }
Output
given tree is not subtree of Binary tree given tree is subtree of Binary tree
- Related Articles
- C++ Program to Check Whether a Given Tree is Binary Search Tree
- C++ Program to Check Whether a given Binary Tree is a Full Binary Tree or not
- Maximum sub-tree sum in a Binary Tree such that the sub-tree is also a BST in C++ program
- How to check whether a binary tree is a valid binary search tree using recursion in C#?
- Check if a binary tree is subtree of another binary tree in C++
- Maximum sub-tree sum in a Binary Tree such that the sub-tree is also a BST in C++
- C++ program to Check if a Given Binary Tree is an AVL Tree or Not
- Program to check whether a binary tree is complete or not in Python
- Program to check whether a binary tree is BST or not in Python
- Binary Tree to Binary Search Tree Conversion in C++
- Difference between Binary Tree and Binary Search Tree
- Binary Tree to Binary Search Tree Conversion using STL set C++?
- C++ Program to Check if a Binary Tree is a BST
- Create a mirror tree from the given binary tree in C++ Program
- Difference Between B-tree and Binary tree

Advertisements