Saturday, 23 Oct 2021

# Tutorial on Binary Tree

The tree is a hierarchical Data Structure. A binary tree is a tree that has at most two children. The node which is on the left of the Binary Tree is called “Left-Child” and the node which is the right is called “Right-Child”. Also, the smaller tree or the subtree in the left of the root node is called the “Left sub-tree” and that is on the right is called “Right sub-tree”. Below are the various operations that can be performed on a Binary Tree:Creation of Binary Tree:The idea is to first create the root node of the given tree, then recursively create the left and the right child for each parent node. Below is the program to illustrate the same:C++#include using namespace std;  struct treenode {    int info;    struct treenode *left,        *right;};  struct treenode* create(){    int data;    struct treenode* tree;              tree = new treenode;      cout data;          if (data == -1)        return 0;          tree->info = data;              cout info = data;              cout right == previous) {                                cout info right;        }      } while (!s3.empty());    cout info = data;              cout right) {            cout right->info                 right);        }    }    cout info = data;              cout info)            max = temp->info;          if (root->left) {            q.push(root->left);        }        if (root->right) {            q.push(root->right);        }    }          return max;}  int main(){        struct treenode* root = NULL;          root = create();          FindMax(root);      return 0;}                                                         Output: Time Complexity: O(N)Auxiliary Space: O(N)Search for an element:The approach to search for any particular element in the tree node is to perform any tree traversal on the given tree and check if there exists any node with the given searched value or not. If found to be true, then print “Element is Found”. Otherwise, print “Element  Not  Found”. Below is the program to illustrate the same:C++#include “bits/stdc++.h”using namespace std;  struct treenode {    int info;    struct treenode *left, *right;};  struct treenode* create(){    int data;    struct treenode* tree;              tree = new treenode;      cout data;          if (data == -1)        return 0;          tree->info = data;              cout left) {                q.push(root->left);            }            if (root->right) {                q.push(root->right);            }        }                  return 0;    }}  int main(){    int data;          struct treenode* root = NULL;          root = create();      cout > data;          if (FindElement(root, data) == 1)        cout left);                  if (temp->right)            q.push(temp->right);    }              while (!s.empty()) {        cout right);    }              return (root->info);}  int main(){        struct treenode* root = NULL;          root = create();      cout right, level + 1);              HashMap[level] = root->info;      return;}  int main(){        struct treenode* root = NULL;          root = create();      topview(root, 0);    cout left, level – 1,               height + 1);    bottomview(root->right, level + 1,               height + 1);      return;}  int main(){        struct treenode* root = NULL;          root = create();      bottomview(root, 0, 0);    cout info = data;              cout right);          struct treenode* temp;          temp = root->left;        root->left = root->right;        root->right = temp;    }    return;}  void inorder(struct treenode* root){    if (root == NULL)        return;      inorder(root->left);    cout info right);}  int main(){            struct treenode* root = NULL;              root = create();      mirrortree(root);    cout data;          if (data == -1)        return 0;              tree->info = data;              cout left, v);    serialize(root->right, v);}  int main(){        struct treenode* root = NULL;          root = create();      vector v;      serialize(root, v);    cout