Вы находитесь на странице: 1из 53

Trees and

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

Binary Tree Each node has at most 2 children


Identify the type of binary tree
SKEWED BINARY TREE

Skewed Binary Tree: A binary tree in which


each node has only one child node.
Left Skewed Binary Tree: A binary tree in
which each node has only left child.
Right Skewed Binary Tree: A binary tree in which
each node has only right child.
Similar and Copies

Two Binary Trees T and T’ are said to be


similar if both these trees have the same
structure.

Similar Binary Trees

Two Binary Trees T and T’ are said to be copies


if they have similar structure and if they have
same content at the corresponding nodes.

T’ is a Copy of T
In-degree & Out-degree of a node

In-degree: It is the number of edges that are arriving at a node.

Out-degree: It is the number of edges leaving that node.

In-degree of root node is 0.


Out-degree of A is 3.
In-degree of M is 1.
Out-degree of M is 0.
Binary Tree Representation
1. Sequential representation using arrays
2. List representation using Linked list

Binary Tree
Representation

Array Linked List


Sequential 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

• To represent a binary tree of depth ‘d' using array


representation, we need one dimensional array with a
maximum size of 2d+1 - 1.
• Store root information at index 0.
• If node occupies kth index then left node is at (2*k+1)th and
right node is at (2*k+2)th index.
• Parent (k)= (k-1)/2
Sequential Representation

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

Find the parent of node 25.


Find the left child of node 90.
Find the right child of node 30.
Find the Parent of node 88.

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)

• In the in-order traversal, first, we visit the left


sub-tree recursively then visit root node and
visit right sub-tree recursively.
• This traversal method is also known as left-
root-right traversal.
1. Traverse the left subtree in in-order traversal
2. Visit the root
3. Traverse the right subtree in in-order traversal
Pre-order Traversal(Depth-first order)

• In the pre-order traversal, first, we visit the


root then recursively perform the pre-order
traversal on the left sub-tree and then perform
the preorder traversal on the right subtree.
• This traversal method is also known as root-
left-right traversal.
1. Visit the root
2. Traverse the left subtree in in-order traversal
3. Traverse the right subtree in in-order traversal
Post-order Traversal(Left-right-root)

• In the post-order traversal, first, we visit the


right sub tree recursively then perform the
post-order traversal on the right sub-tree and
then visit the root node.
• This traversal method is also known as left-
right-root traversal.
1. Traverse the left subtree in in-order traversal
2. Traverse the right subtree in in-order traversal
3. Visit the root
Pre-order Traversal(root-left-right)
void inorder(Node *root)
struct Node
{
{
if (root==NULL) return;
char data;
inorder(root->left);
Node *left;
printf(“%c”,root->data);
Node *right;
inorder(root->right);
} }

void preorder(Node *root) void postorder(Node *root)


{ {
if (root==NULL) return; if (root==NULL) return;
printf(“%c”,root->data); postorder(root->left);
preorder(root->left); postorder(root->right);
preorder(root->right); printf(“%c”,root->data);
} }
In-Order Traversal  I - D - J - B - F - A - G - K - C - H
Pre-Order Traversal  A - B - D - I - J - F - C - G - K - H

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

Вам также может понравиться