0% found this document useful (0 votes)
11 views

i II Ds Unit IV Final

Uploaded by

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

i II Ds Unit IV Final

Uploaded by

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

Prepared by P.

Sangeeta - CSE
Prepared by P.Sangeeta - CSE
Prepared by P.Sangeeta - CSE
Prepared by P.Sangeeta - CSE
Prepared by P.Sangeeta - CSE
Prepared by P.Sangeeta - CSE
Prepared by P.Sangeeta - CSE
Prepared by P.Sangeeta - CSE
Types of trees:

Trees are of following 6 types:


1. General trees
2. Forests
3. Binary trees
4. Binary search trees
5. Expression trees
6. Tournament trees

1. General Trees:
General trees are data structures that store elements hierarchically. The top node of a tree is
the root node and each node, except the root, has a parent. A node in a general tree (except the leaf
nodes) may have zero or more sub-trees. General trees which have 3 sub-trees per node are called
ternary trees.

2. Forests:
A forest is a disjoint union of trees. A set of disjoint trees (or forests) is obtained by deleting
the root and the edges connecting the root node to nodes at level 1. All the sub-trees immediately
below a node form a forest. A forest can also be defined as an ordered set of zero or more general
trees.
We can convert a forest into a tree by adding a single node as the root node of the tree.
For example, Fig. 2 (a) shows a forest and Fig. 2(b) shows the corresponding tree.

3. Binary Trees:
A binary tree is a data structure that is defined as a collection of elements called nodes. In a
binary tree, the topmost element is called the root node, and each node has 0, 1, or at the most 2
children.
A node that has zero children is called a leaf node or a terminal node. Every node contains a data
element, a left pointer which points to the left child, and a right pointer which points to the right
child. The root element is pointed by a 'root' pointer. If root = NULL, then it means the tree is empty.

Fig.a: Binary Tree

Above diagram shows a binary tree where R is the root node and the two trees T1 and T2 are called
the left and right sub-trees of R. T1 is said to be the left successor of R. Likewise, T2 is called the
right successor of R.

Prepared by P.Sangeeta - CSE


4. Binary Search Trees:
A binary search tree, also known as an ordered binary tree, is a variant of binary tree in which
the nodes are arranged in an order.
5. Expression Trees:
Binary trees are widely used to store algebraic expressions. For example,
consider the algebraic expression given as:
Exp = (a – b) + (c * d)
This expression can be represented using a binary tree as

Eg: Given an expression, Exp = ((a + b) – (c * d)) % ((e ^f) / (g – h)), construct
the corresponding binary tree.

6. Tournament Trees:
In a tournament tree (also called a selection tree), each external node represents a player and
each internal node represents the winner of the match played between the players represented by its
children nodes. These tournament trees are also called winner trees because they are being used to
record the winner at each level. We can also have a loser tree that records the loser at each level.

Consider the above tournament tree, there are 8 players in total whose names are represented using a,
b, c, d, e, f, g, and h. In round 1, a and b; c and d; e and f; and finally g and h play against each other.
In round 2, the winners of round 1, that is, a, d, e, and g play against each other. In round 3, the
winners of round 2, a and e play against each other.
Binary Tree:
A binary tree is a data structure that is defined as collection of elements called nodes. A binary tree is
a tree in which no node can have more than two subtrees; the maximum outdegree for a node is two.
A binary tree can be represented as:

Prepared by P.Sangeeta - CSE


Prepared by P.Sangeeta - CSE
Prepared by P.Sangeeta - CSE
Properties of Binary Tree:
• Given that we need to store N nodes in a binary tree, the maximum height, Hmax, is:
Hmax=N
Eg: If three nodes are to be stored in binary tree, then maximum height of tree is 3.
It occurs when al nodes in tree have only one successor.

• The minimum height of the tree, Hmin, is determined by the following formula:
Hmin =[ log2 N] +1

Eg: Given a tree of 3nodes, minimum number height 2 can be formed using above formula.

Prepared by P.Sangeeta - CSE


• Given a height of the binary tree, H, the minimum number of nodes in the tree are given as:
Nmin=H
Eg: When a tree of height 3 is given, minimum number of nodes formed is 3.

• Given a height of the binary tree, H, the maximum number of nodes in the tree is given as:
Nmax=2H-1
Eg: When a tree of height 3 is given, maximum number of nodes formed is 7.

B C

D E F G

Prepared by P.Sangeeta - CSE


Prepared by P.Sangeeta - CSE
Prepared by P.Sangeeta - CSE
Prepared by P.Sangeeta - CSE
Prepared by P.Sangeeta - CSE
Level-order Traversal:

In level-order traversal of a binary tree, all the nodes at a level are accessed before going to the next
level. This algorithm is also called as the breadth-first traversal algorithm.
Consider the binary trees given below and note the level order of these trees.

Fig: Binary Trees and its Level Order Traversal

void levelordertraversal(struct node *ptr,int level)


{
int i;
if ( ptr!=NULL )
{
display(ptr->rchild, level+1);
for (i = 0; i < level; i++)
printf("%d", ptr->data);
levelordertraversal (ptr->lchild, level+1);
}
}

Binary Heaps:
A binary heap is a complete binary tree in which every node satisfies the heap property which
states that: If B is a child of A, then key(A) ≥ key(B)
This implies that elements at every node will be either greater than or equal to the element at its left
and right child. Thus, the root node has the highest key value in the heap. Such a heap is commonly
known as a max-heap.
Elements at every node will be either less than or equal to the element at its left and right child. Thus,
the root has the lowest key value. Such a heap is called a min-heap.

The properties of binary heaps are given as follows:

Prepared by P.Sangeeta - CSE


• In a heap, elements can be stored sequentially in an array. If an element is at position i in the
array, then its left child is stored at position 2i and its right child at position 2i+1. Conversely,
an element at position i has its parent stored at position i/2.
• Being a complete binary tree, all the levels of the tree except the last level are completely
filled.
• The height of a binary tree is given as log2n, where n is the number of elements.

A binary heap is a useful data structure in which elements can be added randomly but only the
element with the highest value is removed in case of max heap and lowest value in case of min heap.

Applications of Binary Heaps:

Binary heaps are mainly applied for


1. Sorting an array using heapsort algorithm.
2. Implementing priority queues.

Prepared by P.Sangeeta - CSE

You might also like