FORESTS

3,924 views 10 slides Dec 06, 2021
Slide 1
Slide 1 of 10
Slide 1
1
Slide 2
2
Slide 3
3
Slide 4
4
Slide 5
5
Slide 6
6
Slide 7
7
Slide 8
8
Slide 9
9
Slide 10
10

About This Presentation

FORESTS in Data Structures


Slide Content

DATA STRUCTURES FOREST By, M.SHARMILA DEVI M.Sc(IT)

FORESTS Definition: A forest is a set of n>=0 disjoint trees. The concept of a forest is very close to that of a tree because if we remove the root of a tree, we obtain a forest. For example, removing the root of any binary tree produces a forest of two trees.

THREE-TREE FOREST

TRANSFORMING A FOREST INTO A BINARY TREE To transform a forest into a single binary tree, we first obtain the binary tree representation of each of the trees in the forest and then link these binary trees together through the rightChild field of the root nodes.

BINARY TREE REPRESENTATION OF FOREST

We can define this transformation in a formal way as follows: Definition: If T 1 , . . . , T n is a forest of trees, then the binary tree corresponding to this forest, denoted by B(T 1 , . . . , T n ), 1. is empty if n=0 2, has root equal to root (T1); has left subtree equal to B ( T 11 , T 12, . . . , T 1m ), where T 11 , . . . , T 1m are the subtrees of root(T 1 ); and has right subtree B(T 2 , . . . , T n ).

FOREST TRAVERSALS Preorder and inorder traversals of the corresponding binary tree T of a forest F have a natural correspondence to traversals on F.

Preorder Traversal Preorder traversal of T is equivalent to visiting the nodes of F in forest preorder, which is defined as follows: If F is empty then return. Visit the root of the first tree of F. Traverse the subtrees of the first tree in forest preorder Traverse the remaining trees of F in forest preorder.

InOrder Traversal InOrder Traversal of T is equivalent to visiting the nodes of F in forest inorder, which is defined as follows: If F is empty then return Traverse the subtrees of the first tree in forest inorder Visit the root of the first tree Traverse the remaining trees in forest inorder

P ostorder Traversal We can define the postorder traversal of a forest as follows: If F is empty then return Traverse the subtrees of the first tree of F in forest postorder. Traverse the remaining trees of F in forest postorder. Visit the root of the first tree of F.