Trees and Binary Trees
Trees and Binary Trees
Binary Trees
Objective of the sesson
• Tree
– Basic Terminologies
• Binary Tree
– Binary Tree – Representation
• Linear Data Structure and Non- Linear Data
Structure
Nature View of a Tree
leaves
branches
root
Computer Scientist’s View
root
leaves
branches
nodes
TREES
• Trees are one of the important non- Linear data
structure.
• A tree is a Multilevel data structure that represent a
hierarchical relationship between the Set of
individual elements called nodes.
• Each tree structure starts with a node Which is called
the root node of the Tree.
Tree Terminology
• Root: node without parent (A)
• Degree of a tree: the
• Siblings: nodes share the same
parent maximum number of its node.
• Internal node: node with at least • Subtree: tree consisting of a
one child (A, B, C, F) node and its descendants
• External node (leaf ): node
without children (E, I, J, K, G, H, D)
A
• Ancestors of a node: parent,
grandparent, grand-grandparent,
etc.
• Descendant of a node: child, B C D
grandchild, grand-grandchild, etc.
• Depth of a node: number of
ancestors E F G H
• Height of a tree: maximum depth
of any node (3)
• Degree of a node: the number of I J K
its children subtree
Binary Tree
• A tree in which every node can have a maximum of
two children is called as Binary Tree.
• In a binary tree, every node can have either 0
children or 1 child or 2 children but not more than 2
children.
Binary Tree
• A binary tree can also be defined as a finite set of
nodes, such that
a) T is empty
b) T contains a specially designated node called the
root of T, and remaining nodes of T form two
disjoint binary trees T1 & T2 which are called left
subtree and right subtree.
Full Binary Tree/ Strictly Binary Tree
• A binary tree in which every node has either
two or zero number of children is called
Full/Strictly Binary Tree.
• It is also called proper binary tree or 2-tree.
Expression Trees
• Strictly Binary Tree data structure is used to
represent mathematical expressions.
Complete Binary Tree
• A binary tree is said to be a complete binary tree if all its
levels, except possibly the last level, have the maximum
number of possible nodes, and all the nodes in the last level
appear as far left as possible.
Perfect Binary Tree
• A binary tree in which every internal node has exactly two
children and all leaf nodes are at same level is called Perfect
Binary Tree.
• Number of nodes = 2d+1 – 1
• Number of leaf nodes = 2d
• Number of external nodes=No. of internal nodes+1
• Where, d – Depth of the tree
Extended Binary Tree
• A binary tree can be converted into Full Binary tree by adding
dummy nodes to existing nodes wherever required.
• The full binary tree obtained by adding dummy nodes to a
binary tree is called as Extended Binary Tree.
Perfect
Binary Tree All levels are full
Complete All levels are full except
Binary Tree last level (from left to right)
Full Binary Each node has either 0 or 2 children
Tree
T’ is a Copy of T
In-degree & Out-degree of a node
Binary Tree
Representation
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14
A B C D F G H I J K
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14
A B C D G H N O
Find array representation of given
binary tree
T A B C D E F G H I J
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14
T 45 22 77 11 30 90 15 25 88
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14
1. 30
2. No left child
3. No right child
4. 90
Sequential representation
• Advantages:
– Direct access to all nodes (Random access)
• Disadvantages:
– Height of tree should be known
– Memory may be wasted
– Insertion and deletion of a node is difficult
List representation
struct node
{
int data;
struct node *left;
struct node *rifgt;
}
Linked List Representation of Binary Tree
Linked List Representation of Binary Tree
List representation
• Advantages:
– Height of tree need not be known
– No memory wastage
– Insertion and deletion of a node is done without
affecting other nodes
• Disadvantages:
– Direct access to node is difficult
– Additional memory required for storing address of
left and right node
Non-Linear Data Structure
• In a non linear data structure , the Elements are not
arranged in sequence.
• The data members are arranged in any Manner. The
data items are not processed one after another.
• Trees and graphs are examples of non linear data
structure.
Linear Data Structure
Vs
Non-Linear Data Structure
• In linear data structures, data elements are
organized sequentially and therefore they are
easy to implement in the computer’s memory.
• In nonlinear data structures, a data element
can be attached to several other data
elements to represent specific relationships
that exist among them.
Operations on Binary Trees
1. Traversing the binary tree: Traversing is the
process of visiting each node in the tree
exactly once.
2. Finding the No. of internal & External nodes
in Binary Tree.
Binary Tree Traversals
There are three types of binary tree traversals.
1. In - Order Traversal (Left-Root-Right)
2. Pre - Order Traversal (Root-Left-Right)
3. Post - Order Traversal (Left-Right-Root)
In-order Traversal(Symmetric order)
Post-Order Traversal I - J - D - F - B - K - G - H - C – A
In-order
Pre-order
Post-order
A
DBHEI FJCG
A
DBHEI C
FJ G
A
DBHEI C
F G
J
A
B C
D HEI F G
J
A
B C
D E F G
H I J
In-order Traversal g d b h e i a f c
Pre-order Traversal a b d g e h I c f
In-order Traversal 5 10 12 15 18 20 25 30 35 40 50
Post-order Traversal 5 12 18 15 10 25 35 50 40 30 20