Limited Period Offer : 20% Discount on online/offline courses, for more details call/whatsapp

Binary Tree

1 min read
1 year ago By Aniket Prajapati

Binary Tree:

A Binary tree is represented by a pointer to the topmost node (commonly known as the “root”) of the tree. If the tree is empty, then the value of the root is NULL. Each node of a Binary Tree contains the following parts: (1) Data. (2) Pointer to left child. (3) Pointer to right child.

Properties of Binary Tree:

  1. The maximum number of nodes at level ‘l’ of a binary tree is 2l:
  2. The Maximum number of nodes in a binary tree of height ‘h’ is 2h – 1:
  3. In a Binary Tree with N nodes, the minimum possible height or the minimum number of levels is Log2(N+1):
  4. A Binary Tree with L leaves has at least | Log2L |+ 1 levels:
  5. In a Binary tree where every node has 0 or 2 children, the number of leaf nodes is always one more than nodes with two children:
  6. In a non-empty binary tree, if n is the total number of nodes and e is the total number of edges, then e = n-1:

Lets create a binary tree with new class .

//creating trees class 


#include<bits/stdc++.h>
using namespace std;

class node{
public:
    int data;
    node* left;
    node* right;
    
    node(int d)
    {
        this->data=d;
        this->left=NULL;
        this->right=NULL;
    }
};

node* buildtree(node* root)
{
    int data;
    cout<<"enter val"<<endl ;
    cin>>data;
    root=new node(data);
    if(data ==-1)
    {
        return NULL;
    }
    
    cout<<"left of"<<data<<endl; 
    root->left=buildtree(root->left);
    cout<<"right of"<<data<<endl; 
    root->right=buildtree(root->right);
    return root;
}

int main(){
    node* root=NULL;
    //creating tree;
    root=buildtree(root);
    return 0 ;
}

To Create a new Tree the complexities are :

Time complexity : O(N) . Space Complexity : O(N) . ,where N is the number of nodes in a binary tree

Jun 24, 2023 21:13 Back to Articles

Other Articles

Establishing Validation by Converting TypeScript Code to Zod Schemas Establishing Validation by Converting TypeScript Code to Zod Schemas

In this article, we'll explore how to establish validation by converting TypeScript code to Zod schemas. Zod is a TypeScript-first schema validation library that ensures data validity at runtime.

1 year ago By Mitali Gupta
linked list

A linked list is a linear data structure, in which the elements are not stored at contiguous memory locations. The elements in a linked list are linked using pointers . This elements can be accessed by pointer traversing.

1 year ago By Aniket Prajapati
For Laravel Setting UP CI/CD Pipeline using GitHub, CodeDeploy, S3 and EC2

Setting ci/cd pipeline for laravel application on aws using github actions then uploading to s3 and using codedeploy application gets installled on EC2 instance

1 year ago By Santosh Kshirsagar
React State Management Made Easy: Zustand and Redux Explained React State Management Made Easy: Zustand and Redux Explained

In this article, we will discover two powerful state management libraries for React: Zustand and Redux. Also explore their unique approaches, advantages, and use cases, to simplify and optimize your application's state handling. Choose the best fit for your project's needs and boost development efficiency with these popular solutions.

1 year ago By Mitali Gupta