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 T1 , . . . , Tn is a forest of trees, then the
binary tree corresponding to this forest, denoted by B(T1
, . . . , Tn),
⮚ 1. is empty if n=0
⮚ 2, has root equal to root (T1); has left subtree equal to
B(T11 , T12, . . . , T1m), where T11, . . . , T1m are the
subtrees of root(T1); and has right subtree B(T2 , . . . ,
Tn).
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:
1. If F is empty then return.
2. Visit the root of the first tree of F.
3. Traverse the subtrees of the first tree in forest
preorder
4. 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:
1. If F is empty then return
2. Traverse the subtrees of the first tree in forest
inorder
3. Visit the root of the first tree
4. Traverse the remaining trees in forest inorder
Postorder Traversal
We can define the postorder traversal of a forest as
follows:
1. If F is empty then return
2. Traverse the subtrees of the first tree of F in
forest postorder.
3. Traverse the remaining trees of F in forest
postorder.
4. Visit the root of the first tree of F.

FORESTS

  • 1.
  • 2.
    FORESTS ⮚ Definition: Aforest 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.
  • 3.
  • 4.
    TRANSFORMING A FORESTINTO 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.
  • 5.
  • 6.
    We can definethis transformation in a formal way as follows: ⮚ Definition: If T1 , . . . , Tn is a forest of trees, then the binary tree corresponding to this forest, denoted by B(T1 , . . . , Tn), ⮚ 1. is empty if n=0 ⮚ 2, has root equal to root (T1); has left subtree equal to B(T11 , T12, . . . , T1m), where T11, . . . , T1m are the subtrees of root(T1); and has right subtree B(T2 , . . . , Tn).
  • 7.
    FOREST TRAVERSALS ⮚ Preorderand inorder traversals of the corresponding binary tree T of a forest F have a natural correspondence to traversals on F.
  • 8.
    ⮚ Preorder Traversal ⮚Preorder traversal of T is equivalent to visiting the nodes of F in forest preorder, which is defined as follows: 1. If F is empty then return. 2. Visit the root of the first tree of F. 3. Traverse the subtrees of the first tree in forest preorder 4. Traverse the remaining trees of F in forest preorder.
  • 9.
    InOrder Traversal InOrder Traversalof T is equivalent to visiting the nodes of F in forest inorder, which is defined as follows: 1. If F is empty then return 2. Traverse the subtrees of the first tree in forest inorder 3. Visit the root of the first tree 4. Traverse the remaining trees in forest inorder
  • 10.
    Postorder Traversal We candefine the postorder traversal of a forest as follows: 1. If F is empty then return 2. Traverse the subtrees of the first tree of F in forest postorder. 3. Traverse the remaining trees of F in forest postorder. 4. Visit the root of the first tree of F.