Professional Documents
Culture Documents
Tree
Session IV
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
B C
3
Basic Structure of Tree Data Structures & Algorithms
• The first node in the tree is called a root.
• 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.
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
B 2 B C
C 3 D E F G
D
4 H I
E
5
• A binary tree with N internal nodes has maximum of (N+1) nodes. This
result is true regardless of the shape of the tree.
B C
D E F G
H I
• 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.
• 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.
D E F
D 0
G
I
E 0 H
a b
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.