DS Module 3 Trees VTU BCS304 Notes/PPT
DS Module 3 Trees VTU BCS304 Notes/PPT
Trees
Trees
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.
TERMINOLOGY
Figure (E).
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.
or
• 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 T is said to complete if all its levels, except possibly
the last level, have the maximum number node 2i, i ≥ 0 and if all
the nodes at the last level appears as far left as possible.
3. Full Binary Tree
• A full binary tree of depth ‘k’ is a binary tree of depth k
having 2k – 1 nodes, k ≥ 1.
Proof: Let n1 be the number of nodes of degree one and n the total number
of nodes.
• Since all nodes in T are at most of degree two, we have
n = n0 + n1+ n2------------eqn (1)
• Count the number of branches in a binary tree. If B is the number of
branches, then n =B + 1.
• All branches stem from a node of degree one or two. Thus,
B =n 1+ 2n2.
• Hence, we obtain
n = B + 1= n 1+ 2n2 + 1 ------------eqn (2)
• Subtracting Eqn. (2) from Eqn. (1) and rearranging terms, we get
n0 = n2 +1
• Consider the figure:
• Here, For Figure (b) n2=4, n0= n2+1= 4+1=5
Therefore, the total number of leaf node=5
Lemma 5.4
• If a complete binary tree with n nodes in given, then
for any node with index i, 1 ≦ i ≦ n,
1) Parent(i) is at if i > 1. If i=1, node i is the root.
2) LeftChild(i) is at 2i if 2i ≦ n. If 2i > n, node i has no left
child.
3) RightChild(i) is at 2i+1 if 2i+1 ≦ n. If 2i+1 > n, node i
has no right child.
BINARY TREE 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.
Linked representation:
The problems in array representation are:
1. It is good for complete binary trees, but more
memory is wasted for skewed and many other binary
trees.
2. 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 left subtree
• RightChild - which contains the address of right subtree.
• Data - which contains the actual information
Recursion function:
• The inorder traversal of a binary tree can be recursively
defined as
– Traverse the left subtree in inorder.
– Visit the root.
– Traverse the right subtree in inorder.
void inorder(treepointerptr)
{
if (ptr)
{
inorder (ptr→leftchild);
printf (“%d”,ptr→data);
inorder (ptr→rightchild);
}
}
Preorder: Preorder is the procedure of visiting a node, traverse
left and continue. When you cannot continue, move right and
begin again or move back until you can move right and
resume.
Recursion function:
• The Preorder traversal of a binary tree can be recursively
defined as
– Visit the root
– Traverse the left subtree in preorder.
– Traverse the right subtree in preorder
void preorder (treepointerptr)
{
if (ptr)
{
printf (“%d”,ptr→data) ;
preorder (ptr→leftchild);
preorder (ptr→rightchild);
}
}
Postorder: Postorder traversal calls for moving down the tree
towards the left until you can go no further. Then move to the
right node and then visit the node and continue.
Recursion function:
• The Postorder traversal of a binary tree can be recursively
defined as
– Traverse the left subtree in postorder.
– Traverse the right subtree in postorder.
– Visit the root
void postorder(treepointerptr)
{
if (ptr)
{
postorder (ptr→leftchild);
postorder (ptr→rightchild);
printf (“%d”,ptr→data);
}
}
Level-Order traversal:
• Visiting the nodes using the ordering suggested by the
node numbering is called level ordering traversing.
THREADED BINARY TREE
• The limitations of binary tree are:
– In binary tree, there are n+1 null links out of 2n total
links.
– Traversing a tree with binary tree is time consuming.
These limitations can be overcome by threaded
binary tree.