Download as pdf or txt
Download as pdf or txt
You are on page 1of 101

UNIT – IV

Trees
Tree Data Structure-
Tree is a non-linear data structure which organizes data in a hierarchical
structure and this is a recursive definition.
OR
A tree is a connected graph without any circuits.
OR
If in a graph, there is one and only one path between every pair of vertices,
then graph is called as a tree.
Introduction to trees

• A tree is a non-linear type of data structure that


organizes data hierarchically. It consists of nodes
connected by edges. Each node contains a value and
may or may not have a child node.

4
5
Tree
• A tree is an abstract model of a
hierarchical structure that consists of
nodes with a parent-child relationship.

• Tree is a sequence of nodes

• There is a starting node known as a root


node

• Every node other than the root has a parent


node.
• Nodes may have any number of children
6
A tree is a nonlinear hierarchical
data structure that consists of nodes
connected by edges.
Properties-

 The important properties of tree data structure are-


 There is one and only one path between every pair of vertices in a
tree.
 A tree with n vertices has exactly (n-1) edges.
 A graph is a tree if and only if it is minimally connected.
 Any connected graph with n vertices and (n-1) edges is a tree.
Characteristics of trees
• Non-linear data structure
• Combines advantages of an ordered array
• Searching as fast as in ordered array
• Insertion and deletion as fast as in linked list
• Simple and fast

9
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. Form of a multi-stage decision-making
Tree Terminologies
Tree Terminologies
Node:
 In tree data structure, every individual element is called as Node.

 Node in a tree data structure stores the actual data of that particular
element and link to next element in hierarchical structure.
1. Root
• In a tree data structure, the first node is called as Root Node.
• Every tree must have a root node.
• We can say that the root node is the origin of the tree data
structure. In any tree, there must be only one root node. We
never have multiple root nodes in a tree.
2. Edge
• In a tree data structure, the connecting link between any two
nodes is called as EDGE.
• In a tree with 'N' number of nodes there will be a maximum
of 'N-1' number of edges.
3. Parent
• In a tree data structure, the node which is a predecessor of any node is
called as PARENT NODE.
• In simple words, the node which has a branch from it to any other
node is called a parent node. Parent node can also be defined as "The
node which has child / children".
4. Child
• In a tree data structure, the node which is descendant of any node is
called as CHILD Node.
• In simple words, the node which has a link from its parent node is
called as child node.
• In a tree, any parent node can have any number of child nodes. In a tree,
all the nodes except root are child nodes.
5. Siblings
• In a tree data structure, nodes which belong to same Parent are called
as SIBLINGS.
• In simple words, the nodes with the same parent are called Sibling nodes.
6. Leaf
• In a tree data structure, the node which does not have a child is called
as LEAF Node.
• In simple words, a leaf is a node with no child.
7. Internal Nodes
• In a tree data structure, the node which has atleast one child is called
as INTERNAL Node. In simple words, an internal node is a node with atleast
one child.

• In a tree data structure, nodes other than leaf nodes are called as Internal
Nodes. The root node is also said to be Internal Node if the tree has more
than one node. Internal nodes are also called as 'Non-Terminal' nodes.
8. Degree
• In a tree data structure, the total number of children of a node is
called as DEGREE of that Node.
• In simple words, the Degree of a node is total number of children
it has.
• The highest degree of a node among all the nodes in a tree is
called as 'Degree of Tree'
9. Level
• In a tree data structure, the root node is said to be at Level 0 and
the children of root node are at Level 1 and the children of the
nodes which are at Level 1 will be at Level 2 and so on...
• In simple words, in a tree each step from top to bottom is called as
a Level and the Level count starts with '0' and incremented by one
at each level (Step).
10. Height
• In a tree data structure, the total number of edges from leaf node to
a particular node in the longest path is called as HEIGHT of that
Node.
• In a tree, height of the root node is said to be height of the tree. In
a tree, height of all leaf nodes is '0'.
11. Depth
• In a tree data structure, the total number of egdes from root node to
a particular node is called as DEPTH of that Node.

• In a tree, the total number of edges from root node to a leaf node in
the longest path is said to be Depth of the tree. In simple words, the
highest depth of any leaf node in a tree is said to be depth of that
tree. In a tree, depth of the root node is '0'.
12. Path
• In a tree data structure, the sequence of Nodes and Edges
from one node to another node is called as PATH between
that two Nodes.

• Length of a Path is total number of nodes in that path. In


below example the path A - B - E - J has length 4.
13. Sub Tree
• In a tree data structure, each child from a node forms a subtree
recursively.
• Every child node will form a subtree on its parent node.
Types of Tree
1. Binary Tree
2. Binary Search Tree
3. AVL Tree
4. B-Tree
Binary Tree
 The binary tree is the kind of tree in which most two children can be
found for each parent.

 The kids are known as the left kid and right kid.

 This is more popular than most other trees. When certain constraints
and characteristics are applied in a Binary tree, a number of others such
as AVL tree, BST (Binary Search Tree), RBT tree, etc. are also used.
When we move forward, we will explain all these styles in detail.
Binary Tree

 A binary tree is a classic example of a nonlinear data structure—


compare this to a linear list where we identify a first item, a next item,
and a last item.

 A binary tree is a special case of the more general tree data structure,
but it is the most useful and most widely used kind of tree.

 A binary tree is best defined using the following recursive definition:


A binary tree
(a) is empty
or
(b) consists of a root and two subtrees—a left and a right—
with each subtree being a binary tree
A consequence of this definition is that a node always has two
subtrees, any of which may be empty. Another consequence is
that if a node has one nonempty subtree, it is important to
distinguish whether it is on the left or right
The following figure shows a binary tree with 9 nodes where A is the root

32
Types of BinaryTree
• Complete binary tree
• Strictly binary tree
• Almost complete binary tree

33
Strictly binary tree
• If every non-leaf node in a binary tree has nonempty leftand right sub-trees, then
such a tree is called a strictly binary tree.

• Or, to put it another way, all of the nodes in a strictly binary tree are of degree zero
or two, never degreeone.

• Astrictly binary tree with


N leaves always contains 2N – 1 nodes.

34
Complete binary tree
• Acomplete binary tree is a binary tree in which every level, except possibly the last, is completely
filled, and all nodes are as far left as possible.

• Acomplete binary tree of depth d is called strictly binary tree if all of whose leaves are at level d.

• Acomplete binary tree has 2d nodes at every depthd and 2d -1 non leaf nodes

35
Almost complete binary tree
• An almost complete binary tree is a tree where for a right child, thereis always a
left child, but for a left child there may not be a right child.

36
 Here’s an example:

The first has an empty right subtree, while the second has an empty
left subtree. However, as trees, they are the same.
The following are examples of binary trees.

Here’s a binary tree with one node, the root:

Here are binary trees with two nodes:


Here are binary trees with three nodes:

Here are binary trees with all left subtrees empty and all right subtrees empty:
Here is a binary tree where each node, except the leaves, has
exactly two subtrees; this is called a complete binary tree:

Here is a general binary tree:


Binary Tree Representations
A binary tree data structure is represented using two methods. Those methods are as follows...
 Array Representation
 Linked List Representation
Consider the following binary tree...
1. Array Representation of Binary Tree

 In array representation of a binary tree, we use one-dimensional array (1-D


Array) to represent a binary tree.
 Consider the above example of a binary tree and it is represented as
follows...

To represent a binary tree of depth 'n' using array representation, we need one
dimensional array with a maximum size of 2n + 1.
2. Linked List Representation of Binary Tree

 We use a double linked list to represent a binary tree.


 In a double linked list, every node consists of three fields.
 First field for storing left child address, second for storing actual data and
third for storing right child address.

 In this linked list representation, a node has the following structure...


The data filed holds the value to be given. The left child is a link filed which contains the address of its
left node and the right child contains the address of its right node.

Lchild
data
rchild

Class node
{
Char data;
Node *lchild;
Node * rchild;
};
 Consider a binary tree and its linked list representation is shown in
the figure.
A

B C

D E G
F

B C

D E F G
Operations on Binary trees
The basic operations to be performed on a binary tree are listed below.

 Create: it creates an empty binary tree.

 Lchild: it returns a pointer to the left child of the node. If the node has no left child,
it returns a NULL pointer.

 Rchild: it returns a pointer to the right child of the node. If the node has no right
child, it returns a NULL pointer.

 Data: it returns the content of the node.


Linked Representation of Binary Tree
 Example
Consider the binary tree T in the figure. A schematic diagram of the linked list
representation of T appears in the following figure. Observe that each node is pictured
with its three fields, and that the empty subtree is pictured by using x for null entries.
Linked Representation of the Binary Tree
Sequential representation of Binary Tree

Let us consider that we have a tree T. let our tree T is a binary tree that us
complete binary tree.

Then there is an efficient way of representing T in the memory called the


sequential representation or array representation of T.

This representation uses only a linear array TREE as follows:


1.The root N of T is stored in TREE [1].
2.If a node occupies TREE [k] then its left child is stored in TREE [2 *
k] and its right child is stored into TREE [2 * k + 1].
Binary Tree Representation
 Array representation.
 Linked representation.
The basic components to be represented in a binary tree is a node.
A Binary Tree node consists of three fields such as.
1. Data
2. left child
3. right child
The logical representation of the above node in C is given below.
struct node
{
char data;
 struct node *lchild;
 struct node *rchild;
};
typedef struct node BTNODE;
 Consider a binary tree and its linked list representation is shown in the figure

A
A

B C
B C

D E G D E F G
F
struct node
{
char data;
char father;
 struct node *lchild;
 struct node *rchild;
};
typedef struct node BTNODE;
Array Representation
• An array can be used to store the nodes of a binary tree.
• The nodes stored in an array are accessible sequentially.
• In c, arrays start with index 0 to (MAXSIZE-1).

• Consider a binary tree with only three nodes as shown.


BT

BT [O] A
BT [1] B
BT [2] C
A
0

1 B
2 C
3 D
4 E
5 F
6 G
Array Representation
 Number the nodes using the numbering scheme for a full binary tree. The
node that is numbered i is stored in tree[i].
a1

2 3
b c

4 5 6 7
d e f g
8 9 10
h i j

tree[] a b c d e f g h i j
0 5 10
left-Skewed Binary Tree
a1
3
b 7
C

0 5 10 15
tree[] a b - c - - - - - - - - - -

 An n node binary tree needs an array whose length is


between n+1 and 2n.
Right-Skewed Binary Tree
a1
b 3
7
c
15
d

tree[] a - b - - - c - - - - - - - d
0 5 10 15

 An n node binary tree needs an array whose length is


between n+1 and 2n.
Linked Representation
 Each binary tree node is represented as an object whose data type
isBinaryTreeNode.
 The space required by an n node binary tree is n * (space required
by one node).
Link Representation of Binary Tree

typedef struct node


{
int data;
struct node *lc,*rc;
};
TREE
 A tree is a hierarchical representation of a finite set of one
or more data items suchthat:
• There is a special node called the root of the tree.
• The nodes other than the root node form an ordered pair of disjoint subtrees.

Root Level 0

Level 1

Leaf

Leaf Level 2
Leaf
Binary Tree
Binary Tree
Binary Tree is a rooted tree in which root can have
maximum two children such that each of them is
again a binary tree. That means, there can be 0,1, or 2
children of any node.
Strict Binary Tree
Strict Binary Tree
Strict Binary Tree is a Binary tree in
which root can have exactly two or no
children children at all. That
means, there can be 0 or 2 children of any node.
Complete Binary Tree
Complete Binary Tree
Complete Binary Tree is a Strict Binary tree in which
every leaf node is at same level. That means, there
are equal number of children in right and left subtree
for every node.
Extended Binary Tree
 A binary tree with special nodes replacing everynull subtree. Every
regular node has two children, and every special node has nochildren.
Extended Binary Tree
 An extended binary tree is a transformation of any binary tree into a
complete binary tree.

 This transformation consists of replacing every null subtree of the


original tree with “special nodes.”

 The nodes from the original tree are then called as internal nodes, while
the “special nodes” are called as external nodes.
 Apart from these primitive operations, other operations that can be
applied to the binary tree are
1. Tree traversal
2. Insertion of a node
3. Deletion of a node
4. Searching for a node
5. Copying the binary tree.
Traversal of a binary tree
• The traversal is one of the most common operations performed on tree data structure.
• It is a way in which each node in the tree is visited exactly once in a systematic manner.
• There are 3 popular ways of binary tree traversal. They are

 Preorder traversal
 Inorder traversal
 Postorder traversal
 Preorder traversal
• The preorder traversal of a non-empty binary tree is defined as follows.

1. Visit the root node


2. Traverse the left subtree in preorder
3. Traverse the right subtree in preorder

In a preorder traversal the root node is visited before traversing its left and right
subtrees. After visiting the root node, the left subtree is taken up and it is traversed
recursively, then the right subtree is traversed recursively.
Tree Traversal : Pre order
 Pre order (N L R)
Tree Traversal : Pre order
 Pre order (N L R)
A
Tree Traversal : Pre order
 Pre order (N L R)
A, B
Tree Traversal : Pre order
 Pre order (N L R)
A, B, D
Tree Traversal : Pre order
 Pre order (N L R)
A, B, D, E
Tree Traversal : Pre order
 Pre order (N L R)
A, B, D, E, C
Tree Traversal : Pre order
 Pre order (N L R)
A, B, D, E, C, F
Tree Traversal : Pre order
 Pre order (N L R)
A, B, D, E, C, F, G
 Algorithm for preorder order traversal

Procedure Preorder(root)
1.If (root !=NULL)
Begin A

1.Print root→info B C

2.Preorder(root→left) D
E F G
3.Preorder(root→right)
E E
4.End
5.finished
The preorder traversal of the above binary tree is
ABDEICFGJ
Tree Traversal : In order
 In order (L N R)
Tree Traversal : In order
 In order (L N R)
D
Tree Traversal : In order
 In order (L N R)
D, B
Tree Traversal : In order
 In order (L N R)
D, B, E
Tree Traversal : In order
 In order (L N R)
D, B, E, A
Tree Traversal : In order
 In order (L N R)
D, B, E, A, F
Tree Traversal : In order
 In order (L N R)
D, B, E, A, F, C
Tree Traversal : In order
 In order (L N R)
D, B, E, A, F, C, G
Tree Traversal : Post order
 Post order (L R N)
Tree Traversal : Post order
 Post order (L R N)
D
Tree Traversal : Post order
 Post order (L R N)
D, E
Tree Traversal : Post order
 Post order (L R N)
D, E, B
Tree Traversal : Post order
 Post order (L R N)
D, E, B, F
Tree Traversal : Post order
 Post order (L R N)
D, E, B, F, G
Tree Traversal : Post order
 Post order (L R N)
D, E, B, F, G, C
Tree Traversal : Post order
 Post order (L R N)
D, E, B, F, G, C, A

You might also like