Binary Tree | Programming and Data Structures - Computer Science Engineering (CSE) PDF Download

Introduction

Trees: Unlike Arrays, Linked Lists, Stack and queues, which are linear data structures, trees are hierarchical data structures.

Tree Vocabulary: The topmost node is called root of the tree. The elements that are directly under an element are called its children. The element directly above something is called its parent. For example, ‘a’ is a child of ‘f’, and ‘f’ is the parent of ‘a’. Finally, elements with no children are called leaves.
Binary Tree | Programming and Data Structures - Computer Science Engineering (CSE)

Why Trees? 

  1. One reason to use trees might be because you want to store information that naturally forms a hierarchy. For example, the file system on a computer:
    file system
    Binary Tree | Programming and Data Structures - Computer Science Engineering (CSE)
  2. Trees (with some ordering e.g., BST) provide moderate access/search (quicker than Linked List and slower than arrays). 
  3. Trees provide moderate insertion/deletion (quicker than Arrays and slower than Unordered Linked Lists). 
  4. Like Linked Lists and unlike Arrays, Trees don’t have an upper limit on number of nodes as nodes are linked using pointers.

Main applications of trees include: 

  1. Manipulate hierarchical data.
  2. Make information easy to search (see tree traversal).
  3. Manipulate sorted lists of data.
  4. As a workflow for compositing digital images for visual effects.
  5. Router algorithms
  6. Form of a multi-stage decision-making (see business chess). 

Binary Tree: A tree whose elements have at most 2 children is called a binary tree. Since each element in a binary tree can have only 2 children, we typically name them the left and right child.

Binary Tree Representation in C: A tree is represented by a pointer to the topmost node in tree. If the tree is empty, then value of root is NULL. 

A Tree node contains following parts:

  1. Data 
  2. Pointer to left child 
  3. Pointer to right child

In C, we can represent a tree node using structures. Below is an example of a tree node with an integer data.

  • C++
    Binary Tree | Programming and Data Structures - Computer Science Engineering (CSE)
  • Python
    Binary Tree | Programming and Data Structures - Computer Science Engineering (CSE)
  • Java
    Binary Tree | Programming and Data Structures - Computer Science Engineering (CSE)
  • C#
    Binary Tree | Programming and Data Structures - Computer Science Engineering (CSE)
  • Javascript
    Binary Tree | Programming and Data Structures - Computer Science Engineering (CSE)

First Simple Tree in C

Let us create a simple tree with 4 nodes in C. The created tree would be as following.
Binary Tree | Programming and Data Structures - Computer Science Engineering (CSE)

  • C++
    Binary Tree | Programming and Data Structures - Computer Science Engineering (CSE)
    Binary Tree | Programming and Data Structures - Computer Science Engineering (CSE)
  • C
    Binary Tree | Programming and Data Structures - Computer Science Engineering (CSE)
    Binary Tree | Programming and Data Structures - Computer Science Engineering (CSE)
    Binary Tree | Programming and Data Structures - Computer Science Engineering (CSE)
  • Python
    Binary Tree | Programming and Data Structures - Computer Science Engineering (CSE)
    Binary Tree | Programming and Data Structures - Computer Science Engineering (CSE)
  • Java
    Binary Tree | Programming and Data Structures - Computer Science Engineering (CSE)
    Binary Tree | Programming and Data Structures - Computer Science Engineering (CSE)
    Binary Tree | Programming and Data Structures - Computer Science Engineering (CSE)
  • C#
    Binary Tree | Programming and Data Structures - Computer Science Engineering (CSE)
    Binary Tree | Programming and Data Structures - Computer Science Engineering (CSE)
    Binary Tree | Programming and Data Structures - Computer Science Engineering (CSE)

Summary: Tree is a hierarchical data structure. Main uses of trees include maintaining hierarchical data, providing moderate access and insert/delete operations. Binary trees are special cases of tree where every node has at most two children.

Binary Tree Properties

  1. Maximum Nodes at Level 'l' in a Binary Tree:

    • Formula: 2l
    • Proof by induction:
      • For root l = 0, nodes = (2= 1)
      • Assume maximum nodes at level  l  is 2l
      • Since every node has at most 2 children, the next level would have 2*2l = 2l+1 nodes
  2. Maximum Number of Nodes in a Binary Tree of Height 'h':

    • Formula: 2h-1
    • Derivation:
      • A tree has maximum nodes if all levels have maximum nodes.
      • The sum of a geometric series with h terms is (2h+1- 1).
  3. Minimum Possible Height in a Binary Tree with N Nodes:

    • Formula: (log2(N+1)  - 1)
    • Derived from the fact that the minimum possible height is when all levels have maximum nodes.
  4. Minimum Number of Levels in a Binary Tree with L Leaves:

    • Formula: (log2(L)+ 1)
    • The minimum number of levels is achieved when all leaves are at the same level.
  5. Relationship Between Leaf Nodes (L) and Internal Nodes with Two Children (T):

    • In Binary tree where every node has 0 or 2 children, the number of leaf nodes is always one more than nodes with two children.
    • Formula: (L = T + 1)
    • Proof:
      • No. of leaf nodes (L) i.e. total elements present at the bottom of tree =
        2h-1 (h is height of tree)
        No. of internal nodes = {total no. of nodes} - {leaf nodes} =
        { 2h - 1 } - {2h-1} = 2h-1 (2-1) - 1 = 2h-1 - 1
        So , L = 2h-1
        T = 2h-1 - 1
        Therefore L = T + 1
The document Binary Tree | Programming and Data Structures - Computer Science Engineering (CSE) is a part of the Computer Science Engineering (CSE) Course Programming and Data Structures.
All you need of Computer Science Engineering (CSE) at this link: Computer Science Engineering (CSE)
119 docs|30 tests

Top Courses for Computer Science Engineering (CSE)

FAQs on Binary Tree - Programming and Data Structures - Computer Science Engineering (CSE)

1. What is a binary tree?
Ans. A binary tree is a type of tree data structure in which each node can have at most two children, referred to as the left child and the right child. The left child node is smaller than the parent node, and the right child node is larger. This hierarchical structure allows efficient storage and retrieval of data.
2. What are the properties of a binary tree?
Ans. The properties of a binary tree include: - Each node can have at most two children. - The left child node is smaller than the parent node, while the right child node is larger. - The binary tree can be empty, having no nodes. - The height of the binary tree is the maximum number of edges from the root to a leaf. - The depth of a node is the number of edges from the root to that node. - The maximum number of nodes at any level i in a binary tree is 2^i.
3. How do binary trees differ from other tree data structures?
Ans. Binary trees differ from other tree data structures in the sense that each node can have at most two children, whereas other tree structures may allow any number of children per node. This restriction on the number of children simplifies the implementation and operations on binary trees, making them more efficient for certain applications.
4. What is the time complexity of searching for a specific element in a binary tree?
Ans. The time complexity of searching for a specific element in a binary tree depends on the height of the tree. In the worst case scenario, where the tree is skewed and resembles a linked list, the time complexity would be O(n), where n is the number of nodes in the tree. However, for a balanced binary tree, the time complexity is O(log n), making it efficient for searching operations.
5. Can a binary tree have duplicate values?
Ans. Yes, a binary tree can have duplicate values. In such cases, the left child node would contain a value smaller than or equal to the parent node, while the right child node would contain a value greater than or equal to the parent node. The order of insertion and the position of the duplicate values would determine the overall structure of the binary tree.
119 docs|30 tests
Download as PDF
Explore Courses for Computer Science Engineering (CSE) exam

Top Courses for Computer Science Engineering (CSE)

Signup for Free!
Signup to see your scores go up within 7 days! Learn & Practice with 1000+ FREE Notes, Videos & Tests.
10M+ students study on EduRev
Related Searches

Extra Questions

,

Binary Tree | Programming and Data Structures - Computer Science Engineering (CSE)

,

practice quizzes

,

Sample Paper

,

Binary Tree | Programming and Data Structures - Computer Science Engineering (CSE)

,

Previous Year Questions with Solutions

,

Viva Questions

,

mock tests for examination

,

pdf

,

study material

,

ppt

,

Semester Notes

,

past year papers

,

Important questions

,

Objective type Questions

,

Binary Tree | Programming and Data Structures - Computer Science Engineering (CSE)

,

Summary

,

shortcuts and tricks

,

Free

,

video lectures

,

Exam

,

MCQs

;