0% found this document useful (0 votes)
17 views

Binary Tree Data Structure

Uploaded by

tajimauchiha007
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
17 views

Binary Tree Data Structure

Uploaded by

tajimauchiha007
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 21

BINARY TREE

DATA STRUCTURE
CONTENTS
1. BINARY TREE - INTRODUCTION
2. REPRESENTATION OF BINARY TREE
3. TYPES OF BINARY TREE
4. TRAVERSING OF BINARY TREE
5. OPERATIONS IN BINARY TREE
TREE DATA STRUCTURE

● A tree data structure is a non-linear data structure


● It does not store in a sequential manner.
● It is a hierarchical structure as elements in a Tree are arranged in
multiple levels
REAL-LIFE APPLICATION OF TREE DATA STRUCTURE

1. Databases make use of the tree data structure for indexing


purposes

2. Tree structures are utilized by Domain Name Server (DNS)

3. XML Parser also makes use of tree structures

4. File Explorer or My Computer of any mobile phone or computer

5.The comments on any of the questions posted on websites have


comments as the child of those questions.

6. The decision-based algorithms being used in machine learning


work upon the principle of the algorithm of a tree structure.
BINARY TREE
> A tree whose elements have at most 2 children is called a binary tree.

> Each element in a binary tree can have only 2 children, named left and right
child.
REPRESENTATION OF BINARY TREE
1) Sequential Representation

● An array is used to store the tree nodes.


● The number of nodes in a tree defines the size of the array.
● The root node of the tree is stored at the first index in the array.
● If a node is stored at the ith location then it’s left and right child is stored at 2i
and 2i+1 location respectively.
Linked list representation
All nodes in a binary tree have three primary

components –

● a data element
● a right reference
● a left reference
❏ The node that lies at the top of the tree is referred to as the root node.
❏ Parent nodes are those that have children.
❏ Children nodes and parent nodes are connected to each other through
references
Storing binary tree using linked list
Maximum number of nodes in a binary tree
TYPES OF BINARY TREE
1.Full Binary Tree:
● It is a special kind of a binary tree that has either zero children or
two children.
● It means that all the nodes in that binary tree should either have
two child nodes of its parent node or the parent node is itself the
leaf node or the external node
2. Complete Binary Tree
● A complete binary tree is another specific type of binary tree
where all the tree levels are filled entirely with nodes, except the
lowest level of the tree.
● In the last or the lowest level of this binary tree, every node
should possibly reside on the left side.
3. Perfect Binary Tree
● A binary tree is said to be ‘perfect’ if all the internal nodes have
strictly two children, and every external or leaf node is at the same
level or same depth within a tree.
● A perfect binary tree having height ‘h’ has 2h – 1 node.
4.Balanced Binary Tree
● A binary tree is said to be ‘balanced’ if the tree height is O(logN),
where ‘N’ is the number of nodes. In a balanced binary tree, the
height of the left and the right subtrees of each node should vary by
at most one

5.Degenerate Binary Tree


● A binary tree is said to be a degenerate binary tree or pathological
binary tree if every internal node has only a single child.
● Such trees are similar to a linked list performance-wise.
BINARY TREE TRAVERSAL

Tree traversal means traversing or visiting each node of a tree.


The three different ways of traversal are:
● Inorder traversal - LEFT - ROOT - RIGHT
● Preorder traversal - ROOT - LEFT - RIGHT
● Postorder traversal - LEFT - RIGHT - ROOT
INORDER - Left Root Right. Here, Left Root Right means that the left subtree of the
root node is traversed first, then the root node, and then the right subtree of the root node is
traversed.
PREORDER - Root Left Right. Here, Root Left Right means root node of the
tree is traversed first, then the left subtree and finally the right subtree is traversed
POSTORDER - Left Right Root. Here, Left Right Root means the left subtree of
the root node is traversed first, then the right subtree, and finally, the root node is traversed
PSEUDOCODE FOR TRAVERSAL
pseudocode
THANK YOU

You might also like