i II Ds Unit IV Final
i II Ds Unit IV Final
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:
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.
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.
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:
• 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.
• 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
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.
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.
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.