You are on page 1of 12

Data Structures & Algorithms

Tree
Session IV

Dr. V.Umadevi M.Sc(CS &IT). M.Tech


(IT)., M.Phil., PhD., D.Litt.,
Director, Department of Computer
Science, Jairams Arts and Science
College, Karur.

1
Data Structures & Algorithms

Introduction to Tree
• Trees
• Basic Structure of Tree
• Application of Tree
• Types of Tree
• Binary Trees
• Binary Tree Representation
• Conversion of General Tree to Binary Tree

2
Data Structures & Algorithms
Tree

• Tree is a non-linear data structure.

• Tree is often used to represent a hierarchy (the relationship


between the items in the hierarchy is represented as the
branches of a typical botanical tree).

• Example record, family trees.

• Def: - A Tree is a data structure used to represent data containing


an hierarchical relation between its elements.
A

B C
3
Basic Structure of Tree Data Structures & Algorithms
• The first node in the tree is called a root.

• In tree each item is called a node.

• A node that has no subtree is called a terminal node or Leaf.

• Each tree under a node is called a subtree.

• A node under which has a descendant either subtree or a leaf is called a


parent node.

• Each item contains data and a pointer that contains the address of another
node or a leaf, one level down.

A Root
Node

B C D Parent Node

Subtree

G H I J Leaf 4
Data Structures & Algorithms

Applications of Tree

• A tree can be used to represent the Unix file system in which files
and subdirectories are stored under directories.

• To represent the records in a file in which elementary items are


stored under group items.

Types of Trees

• Unbalanced binary tree - In the level degree the numbers of sub trees
every node have differ by one or more.

• Balanced binary tree - In the level degree the number of two sub trees
every node have never differ by more than one.

5
Data Structures & Algorithms
Binary Trees

• Binary trees are special cases of general trees.

Def - A Binary tree is a finite set of nodes that is either empty or


consists of a root and two disjoint binary trees called left and
right subtrees.
Level
A 1 A

B 2 B C

C 3 D E F G

D
4 H I

E
5

Two Sample Binary Trees


6
Data Structures & Algorithms
Properties of Binary Tree
Properties of Binary Tree T is
– Either the set is empty, T=0; or
– The set consists of a root, r, and exactly two distinct binary trees
TL (left subtree ) and TR (right subtree).

• Binary trees are almost always considered to be ordered trees.

• Both trees have a root with a single non-empty subtree.

• A binary tree with N internal nodes has maximum of (N+1) nodes. This
result is true regardless of the shape of the tree.

• Consequently, the storage overhead associated with the empty


trees will be O (n).

• A binary tree of height n≥0 has at most 2n+1-1 internal nodes.


7
Data Structures & Algorithms
Binary Tree Representation
A

B C

D E F G

H I

• A full binary tree of depth k is a binary tree of depth having 2k -1 nodes.

• A very elegant sequential representation for such binary trees results from
sequential numbering the nodes, starting with nodes on the level1, then those
on level2 and so on.

• Nodes on any level can be numbered from left to right( this scheme give
definition of a complete binary tree).

• A binary tree with n nodes and of depth k is complete iff its nodes corresponds
to the nodes which are numbered one to n in the full binary tree of depth
8
k
Data Structures & Algorithms
• The nodes may now be stored in a one dimensional array, TREE with the
nodes i being stored in TREE(i).
• This representation can clearly be used for all binary trees though in most
cases there will be a lot of utilized space.
• For a complete binary trees the representation is ideal as no space is
wasted
• The above skewed binary tree however a less than the half array is utilized.
• In the worst case a skewed tree of depth k will require 2K-1 spaces.
TREE TREE
• Of these only k will be occupied.
(1) A A
(2) B B
(3) - C
(4) C D
. - E
. - F
- G
(8) D H
I
• The above representation appears to be good for complete binary trees it is 9
wasteful for many other binary trees.
Data Structures & Algorithms
• In addition, the representation suffers from the general inadequate of sequential
representations.

• Insertions or Deletions of nodes from the middle of a tree requires the


movement of potentially many nodes to reflect the change in level number
of these nodes

• These problem can be easily overcome through the use of linked representation

• Each node will have three Fields LCHILD, DATA and RCHILD as below:

DATA
LCHILD DATA RCHILD
LCHILD RCHILD

• While this node structure will make it difficult to determine the parent of a node,
See that for most applications, it is adequate.

• In case it is necessary to be able to determine the parent of random nodes then


a fourth field parent may be included .

• The representation of binary trees using nodes structures is given below: 10


Data Structures & Algorithms
T
T
A 0
A
B 0
B C
C 0

D E F
D 0
G
I
E 0 H
a b

Linked Representation of a Binary Trees

11
Data Structures & Algorithms
Conversion of General Tree to a Binary Tree
• Conversion first all the branches are A General Tree
originating in every node except the
leftmost branch are deleted.

• Branches from a node to the node to


the right, are drawn, which are
situated at the same level.

• For any given particular node, its left


node is chosen and right child is
placed in the following manner.

• Left child is the node which is


immediately below the given node,
and a right child is the node to the
immediate right of the given node on
the same horizontal line.

Conversion of General Tree to Binary Tree 12

You might also like