Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

Trees_part1

Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 55

Introduction

Tree Terminologies

Binary Tree and Types of Binary Tree,


Binary Tree Traversals
Binary Search Tree, Operations on
CONTENT Binary Search Tree
Applications of Binary Tree-Expression
Tree, Huffman Encoding
AVL Tree & operations on AVL Tree

Introduction of B-Tree & B+ Tree


TREES
oTree is a non-linear data structure which organizes data in a hierarchical
structure.
oIt consists of nodes with parent-child relationship.
PROPERTIES OF
TREES
oThere is one and only one path between every pair of
vertices in a tree.
oA tree with n vertices has exactly (n-1) edges.
oA graph is a tree if and only if it is minimally
connected.
oAny connected graph with n vertices and (n-1) edges
is a tree.
TREE TERMINOLOGY
ROOT
oThe first node from where the
tree originates is called as a
root node.
oIn any tree, there must be
only one root node.
oWe can never have multiple
root nodes in a tree data
structure.
EDGE
oThe connecting link between
any two nodes is called as an
edge.
oIn a tree with n number of
nodes, there are exactly (n-1)
number of edges.
PARENT
oThe node which has a branch
from it to any other node is
called as a parent node.
oIn other words, the node
which has one or more
children is called as a parent
node.
oIn a tree, a parent node can
have any number of child
nodes.
CHILD
oThe node which is a
descendant of some node is
called as a child node.
oAll the nodes except root
node are child nodes.
SIBLINGS
oNodes which belong to the
same parent are called as
siblings.
oIn other words, nodes with
the same parent are sibling
nodes.
DEGREE
oDegree of a node is the total
number of children of that
node.
oDegree of a tree is the
highest degree of a node
among all the nodes in the
tree.

Degree of node A = 2, node B = 3, node


C = 2, node D = 0, node E = 2. node F =
0, node G = 1, node H = 0, node I = 0,
node J = 0, node K = 0
INTERNAL
NODE
oThe node which has at least
one child is called as an
internal node.
oInternal nodes are also called
as non-terminal nodes.
oEvery non-leaf node is an
internal node.
nodes A, B, C, E and G are internal nodes
LEAF NODE
oThe node which does not
have any child is called as a
leaf node.
oLeaf nodes are also called as
external nodes or terminal
nodes.
LEVEL
oIn a tree, each step from top
to bottom is called as level of
a tree.
oThe level count starts with 0
and increments by 1 at each
level or step.
HEIGHT
oTotal number of edges that
lies on the longest path from
any leaf node to a particular
node is called as height of
that node.
oHeight of a tree is the height
of root node.
oHeight of all leaf nodes = 0
Height of node A = 3, node B = 2, node C
= 2, node D = 0, node E = 1, node F = 0,
node G =1 , node H = 0, node I = 0,
node J = 0. node K = 0
DEPTH
oTotal number of edges from
root node to a particular node
is called as depth of that node.
oDepth of a tree is the total
number of edges from root
node to a leaf node in the
longest path.
oDepth of the root node = 0
oThe terms “level” and “depth”
are used interchangeably.
SUBTREE
oIn a tree, each child from a
node forms a subtree
recursively.
oEvery child node forms a
subtree on its parent node.
FOREST
oA forest is a set of disjoint
trees.
ANCESTOR & DESCENDENT
BINARY TREE
BINARY TREE
The Binary tree means that the node can have maximum two
children. Here, binary name itself suggests that 'two'; therefore,
each node can have either 0, 1 or 2 children.

struct node
{
struct node
*left;
int data;
struct node
*right;
}
PROPERTIES OF
BINARY TREE
PROPERTY-
01
Minimum number of nodes in a
binary tree of height H
=H+1

Example:
To construct a binary tree of
height = 4, we need at least 4
+ 1 = 5 nodes.
PROPERTY-
02
Maximum number of nodes in a binary
tree of height H
= -1

Example:
Maximum number of nodes in a binary
tree of height 3
= 23+1 – 1
= 16 – 1
= 15 nodes
PROPERTY-
03
Maximum number of nodes at
any level ‘L’ in a binary tree
= 2L

Example:
Maximum number of nodes at
level-2 in a binary tree
= 22
=4
PROPERTY-04
Total Number of leaf nodes in a Binary Tree
= Total Number of nodes with 2 children + 1

Here,

Number of nodes with 2 children = 2


Number of leaf nodes = 2+1 = 3
TYPES OF
BINARY TREE
STRICT BINARY
TREE
oIt is a binary Tree in which
every node has 0 or 2
children.
oIt is also called as proper
binary tree.
oFor Full Binary Tree, following
equation is always true.
Number of Leaf nodes = Valid strict Invalid strict
Number of Internal nodes + 1 binary tree binary tree
COMPLETE
BINARY TREE

oAll levels completely filled


with nodes except the last
level and in the last level, all
the nodes are as left side as
possible.

Valid Invalid
complete complete
binary tree binary tree
PERFECT
BINARY TREE
oIt is a Binary Tree in which all
internal nodes have 2
children, and all the leaf
nodes are at the same depth
or same level.
oTotal number of nodes in a
Perfect Binary Tree with
height H is 2^H - 1.
Valid perfect Invalid
binary tree perfect
binary tree
BALANCED
BINARY TREE
oIt is a Binary tree in which height of
the left and the right sub-trees of
every node may differ by at most 1.
oDiff=|ht. of left subtree- ht. of right
subtree|
oAVL Tree is well-known data structure
to generate/maintain Balanced Binary
Search Tree.
Valid Invalid
balanced perfect
binary tree binary tree
BINARY TREE
REPRESENTATI
ON
REPRESENTATION OF BINARY
TREE
There are two different methods for representing:
a) using array

oThe index 1 is holding the root, it has two children 5 and 16, they
are placed at location 2 and 3.
REPRESENTATION OF BINARY
TREE
oSome children are missing, so their place in array is left as blank.
oThis approach is good, and easily we can find the index of parent
and child, but it is not memory efficient.
oIt will occupy many spaces that has no use.
oThis representation is good for complete binary tree or full binary
tree.
REPRESENTATION OF BINARY
TREE
b) using linked list
BINARY TREE
TRAVERSAL
BINARY TREE TRAVERSAL
oTraversal is a process to visit all the nodes of a tree and may print
their values too. Because, all nodes are connected via edges (links)
we always start from the root (head) node.
oIn tree, relative order of traversing left subtree, right subtree &
visiting root can be different.
oTypes of traversals:
1. Preorder – VLR <visit root> <left subtree> <right subtree>
2. Inorder – LVR <left subtree> <visit root> <right subtree>
3. Postorder – LRV <left subtree> <right subtree> <visit root>
PREORDER TRAVERSAL
In this traversal method, the root node
is visited first, then the left subtree
and finally the right subtree.
Algorithm:
Until all nodes are traversed −
Step 1 − Visit root node.
Step 2 − Recursively traverse left
subtree.
Step 3 − Recursively traverse right
subtree.
PREORDER TRAVERSAL

O/P:
A→B→D→E→C→F→
G
PREORDER TRAVERSAL

void preorder(struct
Code snippet node
*root)
{
if(root==null)
return;
printf(“%c”,root ->
data);
preorder(root ->
left);
preorder(root ->
O/P:
right); {30 , 20 , 15 , 5 , 18 , 25 , 40 , 35 , 50
} , 45 , 60}
PREORDER TRAVERSAL

O/P:
ABDCEGFHI
INORDER TRAVERSAL
In this traversal method, the left
subtree is visited first, then the root
and later the right sub-tree.
Algorithm:
Until all nodes are traversed −
Step 1 − Recursively traverse left
subtree.
Step 2 − Visit root node.
Step 3 − Recursively traverse right
subtree.
INORDER TRAVERSAL

O/P:
D→B→E→A→F→C→G
INORDER TRAVERSAL

void inorder(struct
Code snippet node
*root)
{
if(root==null)
return;
inorder(root ->
left);
printf(“%c”,root ->
data);
O/P:
inorder(root ->
{5 , 15 , 18 , 20 , 25 , 30 , 35 , 40 , 45 , 50 ,
right);
60}
}
INORDER TRAVERSAL

O/P:
BDAGECHFI
POSTORDER TRAVERSAL
In this traversal method, the root node
is visited last, hence the name. First, we
traverse the left subtree, then the right
subtree and finally the root node.
Algorithm:
Until all nodes are traversed −
Step 1 − Recursively traverse left
subtree.
Step 2 − Recursively traverse right
subtree.
Step 3 − Visit root node.
POSTORDER TRAVERSAL

O/P:
D→E→B→F→G→C→A
POSTORDER TRAVERSAL

void postorder(struct
Code snippet node
*root)
{
if(root==null)
return;
postorder(root ->
left);
postorder(root ->
right);
O/P:
printf(“%c”,root ->
{5 , 18 , 15 , 25 , 20 , 35 , 45 , 60 , 50 , 40 ,
data);
30}
}
POSTORDER TRAVERSAL

O/P:
DBGEHIFCA
EXAMPLE
Preorder Traversal-

100 , 20 , 10 , 30 , 200 , 150 , 300

Inorder Traversal-

10 , 20 , 30 , 100 , 150 , 200 , 300

Postorder Traversal-

10 , 30 , 20 , 150 , 300 , 200 , 100


EXAMPLE
Preorder Traversal-
52, 40, 24, 32, 62, 58, 69

Inorder Traversal-
24, 32, 40, 52, 58, 62, 69

Postorder Traversal-
32, 24, 40, 58, 69, 62, 52
CONSTRUCTING BINARY
TREE FROM TRAVERSAL
RESULTS
oWe can construct a binary tree if we are given atleast two traversal
results:
oIn pre-order, root is always displayed first
oIn post-order, root is always displayed last
oIn in-order, root is always displayed between left & right subtree
oApplying this logic recursively we can form a tree
EXAMPLE
In-oder: DBEAFCG
Pre-order: ABDECFG
EXAMPLE
In-oder: DBHEIAFJCG
Post-order: DHIEBJFGCA

You might also like