0% found this document useful (0 votes)
10 views32 pages

Trees

This document provides an overview of tree data structures, defining key concepts such as nodes, edges, parent, child, and various types of trees, including binary trees. It explains different representations of trees, including list representation and array representation, as well as the advantages and disadvantages of each. Additionally, it discusses the hierarchical organization of data within trees and introduces the concept of forests and their relation to trees.

Uploaded by

dhanashree
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views32 pages

Trees

This document provides an overview of tree data structures, defining key concepts such as nodes, edges, parent, child, and various types of trees, including binary trees. It explains different representations of trees, including list representation and array representation, as well as the advantages and disadvantages of each. Additionally, it discusses the hierarchical organization of data within trees and introduces the concept of forests and their relation to trees.

Uploaded by

dhanashree
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 32

MODULE 3: TREES

INTRODUCTION

 In linear data structure, data is organized in


sequential order and in non-linear data
structure, data is organized in random order.

 Tree is a very popular data structure used in


wide range of applications.
INTRODUCTION
 A tree data structure can be defined as follows...
 Tree is a non-linear data structure which organizes

data in hierarchical fashion and the tree structure


follows a recursive pattern of organizing and
storing data.
 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.
 If there are N number of nodes in a tree
structure, then there can be a maximum of
N-1 number of links.
DEFINITION

A tree is a finite set of one or more


nodes such that
 There is a specially designated node
called root.
 The remaining nodes are partitioned into n
>= 0 disjoint set T1,…,Tn, where each of
these sets is a tree. T1,…,Tn are called the
subtrees of the root.
1. Root : In a tree data structure, the first node is called
as Root Node. Every tree must have root node.
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. Ex: Line between two nodes.
3. Parent In a tree data structure, the node which is
predecessor of any node is called as PARENT NODE. In
simple words, the node which has branch from it to
any other node is called as parent node. Parent node
can also be defined as "The node which has child /
children". Ex: A,B,C,E & G are parent nodes
4. Child In a tree data structure, the node which is
descendant of any 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.Ex: B & C
are children of A, G & H are children of C and K child of G
5. Siblings : In a tree data structure, nodes which belong to
same Parent are called as SIBLINGS.Ex: B & C are siblings,
D, E and F are siblings, G & H are siblings, I & J are siblings
6. Leaf In a tree data structure, the node which does not
have a child is called as LEAF Node.In a tree data
structure, the leaf nodes are also called as External Nodes.
External node is also a node with no child. In a tree, leaf
node is also called as 'Terminal' node.Ex: D,I,J,F,K AND H
7. Internal Nodes In a tree data structure, the node
which has atleast one child is called as INTERNAL
Node. In a tree data structure, nodes other than leaf
nodes are called as Internal Nodes.Internal nodes are
also called as 'Non-Terminal' nodes. Ex: A,B,C,E & G
8. Degree of a node 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'.Ex: Degree of B is 3, A is 2 and of F is
9. Level of a node 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).
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 edges 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 the 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.
 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.
 The ancestors of a node are all the nodes along

the path from the root to that node. Ex: ancestor


of j is B & A A forest is a set of n 0 disjoint trees.
 The notion of a forest is very close to that of a tree

because if we remove the root of a tree we get a


forest. For example, in figure 1 if we remove A we
get a forest with three trees.
BINARY TREES
Definition: A binary tree T is defined as a
finite set of nodes such that,
 T is empty or

 T consists of a root and two disjoint binary

trees called the left subtree and the right


subtree.
DIFFERENT KINDS OF BINARY TREE
1. Skewed Tree:
A skewed tree is a tree, skewed to the left or
skews to the right. It is a tree consisting of only
left subtree or only right subtree.

A tree with only left subtrees is called Left


Skewed Binary Tree.
A tree with only right subtrees is called Right
Skewed Binary Tree.
 2. 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.
 A full binary tree is a binary tree in which all
of the nodes have either 0 or 2 children.
In other terms, a full binary tree is a binary
tree in which all nodes, except the leaf
nodes, have two children.
EXTENDED BINARY TREE
REPRESENTATION OF TREES

 List Representation

 Left Child Right Sibling Representation

 Left child Right Child representation/ Binary


tree representation
LIST REPRESENTATION
LEFT CHILD RIGHT SIBLING
REPRESENTATION

 The left child of a node will be represented as


it is shown in the tree and the remaining
siblings of the node are inserted horizontally
to the left child in the representation
BINARY TREE/ LEFT CHILD RIGHT
CHILD REPRESENTATION
 Obtain left child right sibling
representation as shown in previous
example
 Rotate the horizontal lines clockwise

by 45 degrees
BINARY TREE/ LEFT CHILD RIGHT
CHILD REPRESENTATION
STORAGE REPRESENTATION OF
BINARY TREES
 The storage representation of binary trees
can be classified as
1. Array representation
2. Linked representation.
ARRAY REPRESENTATION
 A tree can be represented using an array,
which is called sequential representation.
ARRAY REPRESENTATION
 A tree can be represented using an array,
which is called sequential representation.
 The nodes are numbered from 1 to n, and

one dimensional array can be used to store


the nodes.
 Position 0 of this array is left empty and the

node numbered i is mapped to position i of


the array.
 Write the Array representation for the following
 Below figure shows the array representation for
both the trees of figure (a).

For complete binary tree the array representation is


ideal, as no space is wasted. • For the skewed tree
less than half the array is utilized.
LINKED REPRESENTATION
 The problems in array representation are:
 It is good for complete binary trees, but more
memory is wasted for skewed and many other binary
trees.
 The insertion and deletion of nodes from the middle

of a tree require the movement of many nodes to


reflect the change in level number of these nodes.
These problems can be easily overcome by linked
representation
 Each node has three fields,

 LeftChild - which contains the address of leftsubtree.


 RightChild - which contains the address of right subtree
 Data which contains the actual information
typedef struct node
{
int data;
Struct node* left;
Struct node *right
};

You might also like